Java – I’m trying to transfer the app from Spring to Spring Boot and and I’m getting some issue with that. In particular the following exception

exceptionhibernatejavaspringspring-boot

I'm not using Entity manager at all. Instead I'm using Hibernate(Session factory). Here is my application properties

mysql.driver=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring_hiber?verifyServerCertificate=false&useSSL=false&requireSSL=false&useLegacyDatetimeCode=false&amp&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password

# Hibernate properties
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create



Exception in thread "task-2" java.lang.IllegalStateException: EntityManagerFactory is closed
at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:507)
at org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:501)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:830)

Best Answer

Spring boot automatically configures spring data related beans even if you use it or not. If you don't want to use EntityManager then exclude these autoconfiguration classes related to the Relational database.

In spring boot application you need to do

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@SpringBootApplication
public class SpringApplication {
  //...
}
Related Topic