Skip to content

Cleaning up after changing the isolation level in JPA / EclipsLink EntityManager

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

// store the old isolation level
int isolationLevelOld = entityManager.unwrap(Connection.class).getTransactionIsolation();

// set the desired isolation level for this transaction


// commit transaction

// reset isolation level to the old value (throws NullPointerException)

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()?


The java.sql.Connection gets returned to the pool in the call to entityManager.getTransaction().commit(); So resetting the isolation level afterwards is not possible and prevented by EclipseLink by returning a null connection.

Maintaining a reference to the Connection to circumvent this will likely leak a connection with altered settings, so I cannot accept your answer RomanC

I ended up creating two instances of EntityManagerFactory. One that creates default EntityManagers and one that creates EntityManagers with Connections with my desired transaction level using a SessionCustomizer:

public static class SessionCustomizer implements org.eclipse.persistence.config.SessionCustomizer {
    public void customize(Session session) throws Exception {
        DatabaseLogin databaseLogin = (DatabaseLogin) session.getDatasourceLogin();

private void init() {
    entityManagerFactoryRegular = Persistence.createEntityManagerFactory("MyPersitenceRegular");
    Map<String, String> props = new HashMap<>();
    props.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, SessionCustomizer.class.getName());
    entityManagerFactoryTransactionSerializable = Persistence.createEntityManagerFactory("MyPersitenceTransactionSerializable", props);

See here Set Isolation level in eclipselink

I then use the EntityManagerFactory that provides whichever connection type I need. Caveat: Transactions cannot span EntityManagers from multiple EntityManagerFactories