5

We have used Hibernate jars in spring application. Data persistence is done by Java Persistence API using EntityManagerFactory being injected via context xml.

When we switched to Hibernate 4, the application is not getting stopped when it is deployed in tomcat 7. 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)

Could anyone suggest what could be the problem in stopping tomcat??

CSchulz
  • 10,882
  • 11
  • 60
  • 114
vegeta
  • 297
  • 1
  • 14

2 Answers2

1

You're going to have to post more than just the stacktrace. I would imagine this is stemming from your injection of your EntityManager into one of your beans. Could you post a sample of one of your beans showing your injection?

Should be similar to:

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) {
        // ...
    }
}
Mike
  • 463
  • 2
  • 11
0

One reason for the HibernateException you are getting could be that your application has explicitly called close() on the EntityManagerFactory that was actually created by Spring.

When Spring destroys the bean for the EntityManagerFactory, it will call close() on it. Hibernate internally maintains a registry of EntityManagerFactory instances. When an EntityManagerFactory is closed, it is removed from the registry. So when Spring attempts to close the same EntityManagerFactory that your application has already closed, Hibernate reports that it can't find it in the registry.

Greg X
  • 1