【发布时间】:2021-05-12 11:37:10
【问题描述】:
我正在使用自定义事务隔离级别在 JPA (EclipseLink) 中执行事务,我使用此代码在 JPA EntityManager 的底层连接上设置了该级别:
// 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);
如果我在提交事务后尝试将隔离级别重置为旧值,则底层连接为null(entityManager.unwrap(Connection.class) 返回 null)。我很担心,如果我不重新设置隔离级别,那么隔离级别不好的连接会泄漏回池中。
更改隔离级别后清理的正确方法是什么?我应该在致电commit() 之前这样做吗?
【问题讨论】:
标签: java jpa transactions eclipselink