Reputation: 871
I'm fairly new to Hibernate. I wrote a program with Hibernate and JPA. The problem is that when I call Persistence.createEntityManagerFactory("name") it takes at least 3000 milliseconds (or even 8000 milliseconds) to return a EntityManagerFactory.
The code from my PersistenceCtrl is the folowing:
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();
}
}
My persistence.xml is the folowing:
<?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>
Is this normal?
Will it be more fast to use SessionFactory from Hibernate instead?
Thank you.
Upvotes: 0
Views: 1481
Reputation: 691625
An EntityManagerFactory is an object that is thread-safe, and that should be created only once for the lifetime of the application. It shouldn't matter much if it takes 3 seconds to create.
There's no reason to close the EntityManagerFactory and reopen it like you seem to be doing.
Upvotes: 2