Reputation: 1040
I have some legacy JDBC code that is used within an EJB, in this code a call to setAutocommit() is made (which is not allowed for managed transactions, for understandable reasons).
I would like to skip this method call if the code is used within a managed transaction, but let the call remain if used in an un-managed context.
Is there a standardised way to detect if a JDBC Connection object is "managed" or not?
Upvotes: 2
Views: 230
Reputation: 3176
For me, checking auto commit and transaction isolation was not enough, because they were set to the same value (false
and Connection.TRANSACTION_READ_COMMITTED
, respectively), both when the Connection was provided by Hibernate directly (standalone) and when running inside a JEE container (managed transactions).
As I did not find any other attributes, I ended up using the following code, which checks if the Connection is of type org.jboss.jca.adapters.jdbc.jdk7.WrappedConnectionJDK7
(or similar).
if (!conn.getAutoCommit()) {
if (!conn.getClass().getName().startsWith("org.jboss.jca.")) {
conn.commit();
}
}
I am not happy with this solution, so if anyone has a better idea, please let me know!
Upvotes: 0
Reputation: 1109132
A bit decent transaction manager does setAutoCommit(false)
and setTransactionIsolation(Connection.TRANSACTION_XXX)
.
Depending on the JDBC driver and the transaction manager used, you may have some luck with getAutoCommit()
and/or getTransactionIsolation()
. Determine by testing which values are been used in both cases so that you can learn how to distinguish the one from other.
Upvotes: 2