2

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.

MTilsted
  • 5,425
  • 9
  • 44
  • 76

1 Answers1

0

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

HHK
  • 4,852
  • 1
  • 23
  • 40
  • So when does the transaction start? When i do the first select/update? – MTilsted Jun 21 '14 at 10:53
  • Yes, typically. From JDBC specification: "When to start a new transaction is a decision made implicitly by either the JDBC driver or the underlying data source. Although some data sources implement an explicit “begin transaction” statement, there is no JDBC API to do so. Typically, a new transaction is started when the current SQL statement requires one and there is no transaction already in place. Whether or not a given SQL statement requires a transaction is also specified by SQL:2003." – HHK Jun 21 '14 at 11:12
  • 1
    BTW since this is a pooled connection don't forget to restore the original isolation level after `commit()` or `rollback()` and before `con.close()` – HHK Jun 21 '14 at 11:32
  • Thanks. I had not thought about the need to restore it to the default level. – MTilsted Jun 21 '14 at 11:38