5

我们在 Spring 应用程序中使用了 Hibernate jars。数据持久性由 Java Persistence API 使用 EntityManagerFactory 通过上下文 xml 注入来完成。

当我们切换到 Hibernate 4 时,应用程序在部署到 tomcat 7 时并没有停止。以下是错误。

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)

任何人都可以提出停止tomcat的问题吗?

4

2 回答 2

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) {
        // ...
    }
}
于 2013-04-19T18:45:45.710 回答
0

您收到 HibernateException 的一个原因可能是您的应用程序已在 Spring 实际创建的 EntityManagerFactory 上显式调用 close()。

当 Spring 销毁 EntityManagerFactory 的 bean 时,它会在其上调用 close()。Hibernate 在内部维护 EntityManagerFactory 实例的注册表。当 EntityManagerFactory 关闭时,它会从注册表中删除。因此,当 Spring 尝试关闭您的应用程序已经关闭的同一个 EntityManagerFactory 时,Hibernate 会报告它在注册表中找不到它。

于 2013-05-24T08:45:26.660 回答