AssertionFailure occurs detach after persist

        var emf = Persistence.createEntityManagerFactory("test");
        var em = emf.createEntityManager();
        var tx = em.getTransaction();

        try {

            var member1 = new Member("1", "bob", 23);


            var member2 = new Member("2", "chirs", 42);

        } catch (Exception e) {
        } finally {


In the provided code, It encounter an issue when trying to commit the transaction, which results in an AssertionFailure Exception. The expected behavior was for member1 to be ignored, and member2 to be inserted into the database.

The stacktrace is following.

ERROR: HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: possible non-threadsafe access to session
javax.persistence.RollbackException: Error while committing the transaction
	at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(
	at org.hibernate.engine.transaction.internal.TransactionImpl.commit(
	at test.Main.main(
Caused by: org.hibernate.AssertionFailure: possible non-threadsafe access to session
	at org.hibernate.action.internal.EntityInsertAction.execute(
	at org.hibernate.engine.spi.ActionQueue.executeActions(
	at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(
	at java.base/java.util.LinkedHashMap.forEach(
	at org.hibernate.engine.spi.ActionQueue.executeActions(
	at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(
	at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(
	at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(
	at org.hibernate.internal.SessionImpl.doFlush(
	at org.hibernate.internal.SessionImpl.managedFlush(
	at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(
	at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(
	at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(
	at org.hibernate.engine.transaction.internal.TransactionImpl.commit(
ERROR: HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: possible non-threadsafe access to session

The code functions correctly when an additional persist request (in this case, member2) is not present and the persistence context has no managing entities. However, the exception is thrown when an additional persist request is made during the transaction, causing the commit to fail.

The exception is being thrown from the EntityInsertAction class. When the detach method is called, it removes entity from first-level cache(PersistenceContext). However, it doesn’t remove the associated action from the actionQueue. As a result, when an action containing a detached entity is executed, it triggers an exception.


			final EntityEntry entry = persistenceContext.getEntry( instance );
			if ( entry == null ) {
				throw new AssertionFailure( "possible non-threadsafe access to session" );

Is this behavior intentional, or is it advisable not to use it in this manner?

I don’t know why you’re detaching an entity right after you pass it to persist as that will not make the entity persistent. You have to flush first.
Either way, I’d say this is a bug. The detach operation should remove entity actions involving the entity.

1 Like

It was just a curiosity. I know the JPA specification states that you ‘must use the flush method before the detach method,’ but I was wondering what would happen in Hibernate.

Thank you for the reply.