我对Hibernate相当新手。我用Hibernate和JPA编写了一个程序。问题是,当我调用Persistence.createEntityManagerFactory(“name”)时,返回EntityManagerFactory需要至少3000毫秒(甚至8000毫秒)。
我的PersistenceCtrl中的代码如下:
import javax.persistence.*;
public class PersistenceCtrl {
private EntityManager entityManager;
private EntityManagerFactory entityManagerFactory;
public PersistenceCtrl() {
startPersistenceCtrl();
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
public void startPersistenceCtrl(){
this.setEntityManagerFactory(Persistence.createEntityManagerFactory("name"));
this.setEntityManager(this.getEntityManagerFactory().createEntityManager());
}
public void closePersistenceCtrl(){
//EntityTransaction et = this.getEntityManager().getTransaction();
//et.begin();
//this.getEntityManager().flush();
//et.commit();
this.getEntityManager().close();
this.getEntityManagerFactory().close();
}
public void saveObject(Object obj){
EntityTransaction et = this.getEntityManager().getTransaction();
et.begin();
this.getEntityManager().persist(obj);
et.commit();
}
public void removeObject(Object obj){
EntityTransaction et = this.getEntityManager().getTransaction();
et.begin();
this.getEntityManager().remove(obj);
et.commit();
}
}
我的persistence.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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="name">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- My classes -->
<properties>
<property name="hibernate.validator.apply_to_ddl" value="false"/>
<property name="hibernate.validator.autoregister_listeners" value="false"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.validation.mode" value="ddl"/>
<property name="javax.persistence.jdbc.url" value="url"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.initialSize" value="2"/>
<property name="javax.persistence.jdbc.minIdle" value="0"/>
<property name="javax.persistence.jdbc.maxActive" value="10"/>
<!-- <property name="hibernate.hbm2ddl.auto" value="update"/> -->
<!-- <property name="javax.persistence.jdbc.minEvictableIdleTimeMillis" value="120000"/> -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
</properties>
</persistence-unit>
这是正常的吗?
使用Hibernate的SessionFactory会更快吗?
谢谢。
答案 0 :(得分:2)
EntityManagerFactory是一个线程安全的对象,应该只在应用程序的生命周期内创建一次。如果创建需要3秒钟,那应该没那么重要。
没有理由关闭EntityManagerFactory并像你似乎一样重新打开它。