0
我對Hibernate相當陌生。我用Hibernate和JPA編寫了一個程序。問題是當我調用Persistence.createEntityManagerFactory(「name」)時,它至少需要3000毫秒(甚至8000毫秒)來返回一個EntityManagerFactory。Hibernate - createEntityManagerFactory太慢
從我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會更快嗎?
謝謝。