Date: 2004-04-20T05:10:25
   Editor: 194.175.229.106 <>
   Wiki: DB Torque Wiki
   Page: FrequentlyAskedQuestions
   URL: http://wiki.apache.org/db-torque/FrequentlyAskedQuestions

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -206,18 +206,29 @@
 
 Answer: Use Transaction yourself:
 {{{
-java.sql.Connection connection = null;
-try {
-  connection = org.apache.torque.util.Transaction.begin("torques_name_of_db");
-  someObject.save(connection);
-  someOtherObject.save(connection);
-  org.apache.torque.util.Transaction.commit(connection);
-}
-catch (Exception e) {
-  org.apache.torque.util.Transaction.safeRollback(connection);
+{
+  java.sql.Connection connection = null;
+  try {
+    connection = org.apache.torque.util.Transaction.begin("torques_name_of_db");
+    someObject.save(connection);
+    someOtherObject.save(connection);
+    org.apache.torque.util.Transaction.commit(connection);
+  }
+  catch (Exception e) {
+    try {
+      org.apache.torque.util.Transaction.rollback(connection);
+    }
+    catch (TorqueException ee) {
+      // do nothing
+    }
+    // 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.
 
 -- Thomas Fischer
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to