Errors when stopping tomcat 7 server when using Hibernate 4

We used Hibernate containers in the spring application. Data portability is performed by the Java Persistence API using EntityManagerFactory, which is entered through the xml context.

When we switched to Hibernate 4, the application does not stop when it is deployed to tomcat 7. The following is the error.

SEVERE: A child container failed during stop java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to stop component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/applicationName] at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source) at java.util.concurrent.FutureTask.get(Unknown Source) at org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:1179) at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1564) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: org.apache.catalina.LifecycleException: Failed to stop component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/applicationName] at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:236) ... 7 more Caused by: org.hibernate.HibernateException: registry does not contain entity manager factory: persistence-unit-name at org.hibernate.ejb.internal.EntityManagerFactoryRegistry.removeEntityManagerFactory(EntityManagerFactoryRegistry.java:117) at org.hibernate.ejb.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:195) 

Can anyone suggest what might be the problem when stopping tomcat?

+4
source share
2 answers

You will have to publish more than just a stack. I would suggest that this is because your injection of your EntityManager into one of your beans. Could you place a sample of one of your beans showing your injection?

It should look like:

Config

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:annotation-config /> <context:component-scan base-package="com.company.app.dao" /> <context:component-scan base-package="com.company.app.service" /> <task:executor id="taskExecutor" pool-size="5" /> <task:scheduler id="taskScheduler" pool-size="5" /> <task:annotation-driven executor="taskScheduler" scheduler="taskScheduler" /> <jee:jndi-lookup id="dataSource" jndi-name="jdbc/Test" /> <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> <bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="jpaAdapter" /> <property name="jpaDialect" ref="jpaDialect" /> <property name="packagesToScan" value="com.company.app.model" /> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManager" /> <tx:annotation-driven /> </beans> 

DAO

 import com.company.app.model.Job; import com.company.app.model.JobState; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import java.util.HashSet; import java.util.Set; import java.util.UUID; @Repository public class JobDao { @PersistenceContext private EntityManager em; @Transactional(propagation = Propagation.REQUIRED) public void create(Job job) { // ... } @Transactional(propagation = Propagation.REQUIRED, readOnly = true) public Set<Job> readAll() { // ... } @Transactional(propagation = Propagation.REQUIRED, readOnly = true) public Job readById(UUID id) { // ... } @Transactional(propagation = Propagation.REQUIRED, readOnly = true) public Set<Job> readByState(JobState state) { // ... } @Transactional(propagation = Propagation.REQUIRED) public void update(Job job) { // ... } @Transactional(propagation = Propagation.REQUIRED) public void delete(Job job) { // ... } } 
+1
source

One of the reasons for getting a HibernateException might be that your application explicitly called close () in the EntityManagerFactory that was actually created by Spring.

When Spring destroys the bean for EntityManagerFactory, it calls the close () function on it. Hibernate internally maintains an registry of EntityManagerFactory instances. When EntityManagerFactory is closed, it is removed from the registry. Therefore, when Spring tries to close the same EntityManagerFactory that your application is already closed, Hibernate reports that it cannot find it in the registry.

0
source

All Articles