@@ -59,52 +59,6 @@ class Data_API Transaction
5959 // / See Transaction for more detaisl nad purpose of this template.
6060{
6161public:
62- template <typename T>
63- class Transactor
64- // / Transactor is a helper functor template.
65- // / It is used to consolidate the C++ code that participates in
66- // / the transaction.
67- // /
68- // / Example usage:
69- // /
70- // / struct ATransaction
71- // / {
72- // / void operator () (Session& session) const
73- // / {
74- // / // do something ...
75- // / }
76- // / };
77- // /
78- // / ATransaction t;
79- // / Transaction at(session);
80- // / at.transact(t); // commits, if successful
81- // /
82- // / See Transaction for more details on how to use Transactor.
83- {
84- public:
85- Transactor (T& transactor): _transactor(transactor)
86- // / Creates the Transactor
87- {
88- }
89-
90- inline void operator () (Poco::Data::Session& session)
91- {
92- _transactor (session);
93- }
94-
95- inline void operator () (Poco::Data::Session& session) const
96- {
97- _transactor (session);
98- }
99-
100- private:
101- Transactor ();
102- Transactor (const Transactor&);
103- Transactor& operator = (const Transactor&);
104-
105- T& _transactor;
106- };
107-
10862 Transaction (Poco::Data::Session& session, Poco::Logger* pLogger = 0 );
10963 // / Creates the Transaction and starts it, using the given database session and logger.
11064
@@ -117,15 +71,35 @@ class Data_API Transaction
11771 Transaction (Poco::Data::Session& rSession, T& t, Poco::Logger* pLogger = 0 ):
11872 _rSession (rSession),
11973 _pLogger (pLogger)
120- // / Creates the Transaction, using the given database session and logger.
121- // / The type for the second argument must be Transactor-compatible, i.e.
122- // / provide the overload for the operator ().
74+ // / Creates the Transaction, using the given database session, transactor and logger.
75+ // / The transactor type must provide operator () overload taking non-const Session
76+ // / reference as an argument.
77+ // /
12378 // / When transaction is created using this constructor, it is executed and
12479 // / commited automatically. If no error occurs, rollback is disabled and does
125- // / not occur at destruction time.
80+ // / not occur at destruction time. If an error occurs resulting in exception being
81+ // / thrown, the transaction is rolled back and exception propagated to calling code.
82+ // /
83+ // / Example usage:
84+ // /
85+ // / struct Transactor
86+ // / {
87+ // / void operator () (Session& session) const
88+ // / {
89+ // / // do something ...
90+ // / }
91+ // / };
92+ // /
93+ // / Transactor tr;
94+ // / Transaction tn(session, tr);
12695 {
127- begin ();
128- execute (t);
96+ try { transact (t); }
97+ catch (...)
98+ {
99+ if (_pLogger) _pLogger->error (" Error executing transaction." );
100+ rollback ();
101+ throw ;
102+ }
129103 }
130104
131105 ~Transaction ();
@@ -164,12 +138,11 @@ class Data_API Transaction
164138
165139 template <typename T>
166140 void transact (T& t)
167- // / Executes the transactor and, if doCommit is true, commits the transaction.
168- // / Passing true value for commit disables rollback during destruction
169- // / of this Transaction object.
141+ // / Executes the transactor and, unless transactor throws an exception,
142+ // / commits the transaction.
170143 {
171- Transactor<T> transactor (t );
172- transactor (_rSession);
144+ if (! isActive ()) begin ( );
145+ t (_rSession);
173146 commit ();
174147 }
175148
0 commit comments