Error while execute the <method name> procedure :: org.hibernate.AssertionFailure: possible non-threadsafe access to the session

I upgraded my spring boot to 3.2.2 which turn updated other dependency as:
spring orm - 6.1.3
spring jdbc 6.1.3
spring data jpa 3.2.2
hibernate core 6.4.1 final (I tried with 6.5.2 version also)
hibernate commons annotations 6.0.6

Code is not changed but procedure call is failing. after upgrade for SQL server procedure

I start getting “non-threadsafe access to the session” error when execute my procedure.
i create factory at class level @PersistenceUnit(unitName = “sqlServerConfig”)
private EntityManagerFactory emf;

In my method looks like this
EntityManager em = null;
em = emf.createEntityManager();
em = emf.createEntityManager();
StoredProcedureQuery query = em
.createNamedStoredProcedureQuery(“myName”);
query.setParameter(“name1”, “value”);
query.setParameter(“name2”, value2);
query.setParameter(“name3”, value3);
------
query.execute(); // Break here with Assertion error:
org.hibernate.AssertionFailure. 29 - 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 the session

more trace:
org.hibernate.AssertionFailure: possible non-threadsafe access to the session
at org.hibernate.event.internal.DefaultPostLoadEventListener.onPostLoad(DefaultPostLoadEventListener.java:48)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:127)
at org.hibernate.engine.internal.StatefulPersistenceContext.processLoadedEntityHolder(StatefulPersistenceContext.java:466)
at org.hibernate.engine.internal.StatefulPersistenceContext.postLoad(StatefulPersistenceContext.java:433)
at org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl.finishUp(JdbcValuesSourceProcessingStateStandardImpl.java:174)
at org.hibernate.result.internal.OutputsImpl.extractResults(OutputsImpl.java:250)
at org.hibernate.result.internal.OutputsImpl.extractCurrentResults(OutputsImpl.java:139)
at org.hibernate.result.internal.OutputsImpl$CurrentReturnState.buildOutput(OutputsImpl.java:303)
at org.hibernate.result.internal.OutputsImpl$CurrentReturnState.getOutput(OutputsImpl.java:287)
at org.hibernate.result.internal.OutputsImpl.getCurrent(OutputsImpl.java:108)
at org.hibernate.procedure.internal.ProcedureCallImpl.execute(ProcedureCallImpl.java:842)

Hello

You should after upgrading to Spring Boot 3.2.2 and related dependencies (Spring ORM 6.1.3, Spring JDBC 6.1.3, Spring Data JPA 3.2.2, Hibernate Core 6.4.1 Final/6.5.2, and Hibernate Commons Annotations 6.0.6), you’re encountering a “non-threadsafe access to the session” error when calling a stored procedure in SQL Server.

The issue seems to stem from Hibernate’s session management. Here’s a revised version of your code to address this:

@PersistenceUnit(unitName = "sqlServerConfig")
private EntityManagerFactory emf;

public void callProcedure() {
    EntityManager em = emf.createEntityManager();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("myName");
        query.setParameter("name1", "value");
        query.setParameter("name2", value2);
        query.setParameter("name3", value3);
        query.execute();
    } finally {
        em.close();
    }
}

I am already closing EntityManager in finally. The code was working and EM is always closed. The issue started after upgrade.

@ansh05 Please try reproducing the issue using our test case templates. If you are able to get the same error you can open a new ticket in our issue tracker and attach the reproducer.