ERROR","loggerName":"org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler","message":"Unexpected error occurred in scheduled task","thrown":{"commonElementCount":0,"localizedMessage":"Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed","message":"Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed

Comment From: wilkinsona

Thanks for the report but it's impossible to tell from a single log message why the entity manager factory has been closed. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

Comment From: agu222

Hi @wilkinsona Previously we were using spring boot version 2.2.6 and java 8. Recently we migrated to java 17 and spring boot 2.7.14. My dependencies are as below

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>11.2.1.jre17</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.7.14</version>
</dependency>

My application is giving error on start up as :

{"instant":{"epochSecond":1691599802,"nanoOfSecond":355563759},"thread":"scheduling-1","level":"ERROR","loggerName":"org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler","message":"Unexpected error occurred in scheduled task","thrown":{"commonElementCount":0,"localizedMessage":"Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed","message":"Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed","name":"org.springframework.transaction.CannotCreateTransactionException","cause":{"commonElementCount":69,"localizedMessage":"EntityManagerFactory is closed","message":"EntityManagerFactory is closed","name":"java.lang.IllegalStateException","extendedStackTrace":"java.lang.IllegalStateException: EntityManagerFactory is closed\n\tat org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:547) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]\n\tat org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:636) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]\n\tat org.hibernate.internal.SessionFactoryImpl.createEntityManager(SessionFactoryImpl.java:158) ~[hibernate-core-5.6.15.Final.jar!/:5.6.15.Final]\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.createNativeEntityManager(AbstractEntityManagerFactoryBean.java:585) ~[spring-orm-5.3.29.jar!/:5.3.29]\n\tat jdk.internal.reflect.GeneratedMethodAccessor47.invoke(Unknown Source) ~[?:?]\n\tat jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]\n\tat java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.invokeProxyMethod(AbstractEntityManagerFactoryBean.java:487) ~[spring-orm-5.3.29.jar!/:5.3.29]\n\tat org.springframework.orm.jpa.AbstractEntityManagerFactoryBean$ManagedEntityManagerFactoryInvocationHandler.invoke(AbstractEntityManagerFactoryBean.java:734) ~[spring-orm-5.3.29.jar!/:5.3.29]\n\tat jdk.proxy2.$Proxy185.createNativeEntityManager(Unknown Source) ~[?:?]\n\tat org.springframework.orm.jpa.JpaTransactionManager.createEntityManagerForTransaction(JpaTransactionManager.java:485) ~[spring-orm-5.3.29.jar!/:5.3.29]\n\tat org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:410) ~[spring-orm-5.3.29.jar!/:5.3.29]\n"},"extendedStackTrace":"org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed\n\tat org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:467)

If I add property spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration my scheduler starts. It performs both read and update operation but is unable to perform saveAll operation in azure sql server. I can't see any error as well. It looks for hibernate sequence for inserting the values but is not doing insert operation (no error is observed in logs).

Comment From: wilkinsona

Thanks for the additional information. Unfortunately, it's not enough for us to be able to help. With those dependencies, Spring Batch wouldn't be involved at all so I don't understand why excluding BatchAutoConfiguration would make any difference. This is also the first time that you've mentioned a scheduler and Azure. It would appear that there's quite a bit more to the problem than you have described. We can't afford to spend time guessing what the missing parts may be, hence the request for a sample that reproduces the problem. Please provide that sample.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.