即使事务已提交,为什么EntityManager也会关闭?

时间:2015-01-24 20:32:39

标签: java hibernate jpa

我正在使用JPA和Hibernate进行一些简单的测试,当我不期待时,我碰巧关闭了EntityManager。

这是我的测试主要内容:

public class TestMain {
    public static void main(String[] args) {
        DBResourcesManager.initPeristence();
        List<Event> allEvents = EventDao.findAllEventsA();
        Event e1 = new Event("Event", new Date());
        EventDao.store(e1);
        DBResourcesManager.shutdown();
    }
}

数据库助手:

public class DBResourcesManager {

    private static EntityManager entityManager;
    private static EntityManagerFactory entityManagerFactory;

    public static void initPeristence() {
        entityManagerFactory = Persistence.createEntityManagerFactory("pu");
        entityManager = entityManagerFactory.createEntityManager();
    }

    public static EntityManager getEntityManager() {
        return entityManager;
    }

    public static void shutdown() {
        entityManager.close();
        entityManagerFactory.close();
    }
}

实体和相应的DAO:

public class Event {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;

    @Temporal(TemporalType.DATE)
    private Date date;
/*...*/
}

public class EventDao {
    public static void store(Event e) {
        EntityManager em = DBResourcesManager.getEntityManager();
        EntityTransaction t = em.getTransaction();
        t.begin();
        em.persist(e);
        t.commit();
        em.close();
    }

    public static List<Event> findAllEventsA() {
        EntityManager em = DBResourcesManager.getEntityManager();
        EntityTransaction t = em.getTransaction();
        t.begin();
        List<Event> events = em.createQuery("from Event", Event.class)
                .getResultList();
        t.commit();
        return events;
    }
}

在商店中,开始交易时,我得到以下内容:

Exception in thread "main" java.lang.IllegalStateException: EntityManager is closed

不执行关机方法。 为什么实体经理关闭?

为了完整起见,这里是persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="pu">
        <class>com.lh.hibernateexample.Event</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url"
                value="jdbc:mysql://localhost:3306/myfirsthibernatedb" />
            <property name="hibernate.connection.username" value="XXXXXXXX" />
            <property name="hibernate.connection.password" value="XXXXXXXX" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.archive.autodetection" value="class" />
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:2)

您在store方法中关闭实体管理器,然后尝试在shutdown方法中再次关闭它。

在DAO中使用静态方法很奇怪。