Date: 2004-06-03T07:16:47
Editor: 194.175.229.106 <>
Wiki: DB Torque Wiki
Page: FrequentlyAskedQuestions
URL: http://wiki.apache.org/db-torque/FrequentlyAskedQuestions
no comment
Change Log:
------------------------------------------------------------------------------
@@ -231,22 +231,24 @@
someObject.save(connection);
someOtherObject.save(connection);
org.apache.torque.util.Transaction.commit(connection);
+ connection = null;
}
catch (Exception e) {
- try {
- org.apache.torque.util.Transaction.rollback(connection);
- }
- catch (TorqueException ee) {
- // do nothing
+ // error-handling code goes here
+ }
+ finally {
+ // no rollback if the transaction succeeded,
+ // because in this case connection is null here
+ if (connection != null) {
+ org.apache.torque.util.Transaction.safeRollback(connection);
}
- // other error-handling code goes here
}
}
}}}
torques_name_of_db must be replaced with the database's name from torque's
configuration files. The save(connection) methods are implemented in Torque's
autogenerated base classes (where also the "usual" save() - Methods are), so you don't
have to implement these by yourself.
-While coding your own transactions, make sure that the Database connection is
released at the end. This is ensured in the above code because either
Transaction.commit() or Transaction.rollback() are called, both of which release the
database connection. Note that Transaction.safeRollback() does not release the
database connection, so replacing Transaction.rollback() by transaction.safeRollback()
in the above code would lead to the leakage of database connections if the transaction
fails.
+One important point in using the Transaction class is to make sure that the Database
connection is released at the end of the transaction, even if the transaction failed.
If database connections are not released, you will run out of available connections
sooner or later, leading to errors which are not easy to find. Releasing the
connection is ensured in the above code because either Transaction.commit() or
Transaction.safeRollback() are called (or both if the commit fails). Both methods
release the database connection. Note that connection.commit() does NOT release the
database connection, so connection.commit() should not be used unless you know what
you are doing.
-- Thomas Fischer
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]