I'm performing a transaction in JPA (EclipseLink) using a custom transaction isolation level, which I set on the underlying connection of the JPA EntityManager
using this code:
// begin transaction
entityManager.getTransaction().begin();
// store the old isolation level
int isolationLevelOld = entityManager.unwrap(Connection.class).getTransactionIsolation();
// set the desired isolation level for this transaction
entityManager.unwrap(Connection.class).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
[...Queries...]
// commit transaction
entityManager.getTransaction().commit();
// reset isolation level to the old value (throws NullPointerException)
entityManager.unwrap(Connection.class).setTransactionIsolation(isolationLevelOld);
If I try to reset the isolation level to the old value after having committed the transaction, the underlying connection is null
(entityManager.unwrap(Connection.class)
returns null). I'm worried, if I just don't reset the isolation level, a connection with a bad isolation level gets leaked back to the pool.
What is the correct way of cleaning up after changing the isolation level? Should I maybe do it before calling commit()
?