我对休眠相当陌生。我用 Hibernate 和 JPA 编写了一个程序。问题是当我调用 Persistence.createEntityManagerFactory("name") 时,至少需要 3000 毫秒(甚至 8000 毫秒)才能返回一个 EntityManagerFactory。
我的 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 会更快吗?
谢谢你。