Skip to content

Commit 08efaa0

Browse files
committed
Removed Transactor template (not needed), fixed documentation, additional tests
1 parent 60c8b01 commit 08efaa0

3 files changed

Lines changed: 73 additions & 63 deletions

File tree

‎Data/ODBC/testsuite/src/SQLExecutor.cpp‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,8 +3688,7 @@ void SQLExecutor::transactor()
36883688
session().setTransactionIsolation(Session::TRANSACTION_READ_COMMITTED);
36893689

36903690
TestCommitTransactor ct;
3691-
Transaction t1(session());
3692-
t1.transact(ct);
3691+
Transaction t1(session(), ct);
36933692

36943693
try { session() << "SELECT count(*) FROM PERSON", into(count), now; }
36953694
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
@@ -3708,8 +3707,46 @@ void SQLExecutor::transactor()
37083707
try
37093708
{
37103709
TestRollbackTransactor rt;
3711-
Transaction t2(session());
3712-
t2.transact(rt);
3710+
Transaction t(session(), rt);
3711+
fail ("must fail");
3712+
} catch (Poco::Exception&) { }
3713+
3714+
try { session() << "SELECT count(*) FROM PERSON", into(count), now; }
3715+
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
3716+
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
3717+
assert (0 == count);
3718+
3719+
try
3720+
{
3721+
TestRollbackTransactor rt;
3722+
Transaction t(session());
3723+
t.transact(rt);
3724+
fail ("must fail");
3725+
} catch (Poco::Exception&) { }
3726+
3727+
try { session() << "SELECT count(*) FROM PERSON", into(count), now; }
3728+
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
3729+
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
3730+
assert (0 == count);
3731+
3732+
try
3733+
{
3734+
TestRollbackTransactor rt;
3735+
Transaction t(session(), false);
3736+
t.transact(rt);
3737+
fail ("must fail");
3738+
} catch (Poco::Exception&) { }
3739+
3740+
try { session() << "SELECT count(*) FROM PERSON", into(count), now; }
3741+
catch(ConnectionException& ce){ std::cout << ce.toString() << std::endl; fail (funct); }
3742+
catch(StatementException& se){ std::cout << se.toString() << std::endl; fail (funct); }
3743+
assert (0 == count);
3744+
3745+
try
3746+
{
3747+
TestRollbackTransactor rt;
3748+
Transaction t(session(), true);
3749+
t.transact(rt);
37133750
fail ("must fail");
37143751
} catch (Poco::Exception&) { }
37153752

‎Data/include/Poco/Data/Transaction.h‎

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -59,52 +59,6 @@ class Data_API Transaction
5959
/// See Transaction for more detaisl nad purpose of this template.
6060
{
6161
public:
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

‎Data/src/Transaction.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ Transaction::~Transaction()
6969

7070
_rSession.rollback();
7171
}
72-
catch (Poco::Exception& exc)
72+
catch (...)
7373
{
7474
if (_pLogger)
75-
_pLogger->fatal("Error while rolling back database transaction: " + exc.displayText());
75+
_pLogger->error("Error while rolling back database transaction.");
7676
}
7777
}
7878
}

0 commit comments

Comments
 (0)