Search code examples
javajdbcapache-torque

Set transaction isolation level in torque


How do I set the transaction isolation level for a specific transaction when using torque?

My problem may be that I am using the org.apache.torque.util.Transaction class like this:

Connection con=Transaction.begin();
// Use connection
con.commit();

Con does have a setTransactionIsolation method, but the documentation for that method says: "If this method is called during a transaction, the result is implementation-defined."

which seems odd, since the only way to get a Connection object is to begin a transaction. So the only time I can call that method, is during a transaction.


Solution

  • Just use con.setTransactionIsolation(...) directly after getting the connection via Connection con=Transaction.begin();. The transaction is not yet started in the JDBC sense at this point. Make sure to call con.commit() or con.rollback() later, releasing the connection back to a connection pool via con.close() while a transaction is in progress is undefined behavior.

    Reference: org.apache.torque.util.Torque Transaction source