-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Does Hibernate follow JPA for closing EntityManagerFactory?
PostPosted: Mon Jun 11, 2012 2:54 pm 
Newbie

Joined: Tue Aug 23, 2011 2:57 pm
Posts: 6
Hi all! I was trying to run this simples code using Hibernate 3.6.0:

Code:
public class TestEMF {
   
   public static void main(String[] args) {
      EntityManager em = new JPAUtil().getEntityManager();
      Account a = new Account();
      a.setOwner("Test1");
      
      em.getTransaction().begin();
      em.persist(a);
      em.getTransaction().commit();
      
      em.close();

      System.out.println(JPAUtil.entityManagerFactory.isOpen());
      JPAUtil.entityManagerFactory.close();
      
      Account a2 = new Account();
      a2.setOwner("Test2");
      EntityManager em2 = new JPAUtil().getEntityManager();
      em2.getTransaction().begin();
      em2.persist(a2);
      em2.getTransaction().commit();
      
      System.out.println(JPAUtil.entityManagerFactory.isOpen());
   }

}



I thought that according to JPA2 this code was supposed to throw an exception, like it is described here:

"Get the properties and associated values that are in effect
* for the entity manager factory. Changing the contents of the
* map does not change the configuration in effect.
* @return properties
* @throws IllegalStateException if the entity manager factory
* has been closed"

(JPA2 pdf specification page 290 - http://download.oracle.com/otn-pub/jcp/ ... 838dec6ba4 )
Unfortunately, when you run this code no exception is thrown. My persistence.xml:

Code:
<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="money">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>br.com.model.Account</class>
      
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.format_sql" value="true"/>
         
         
         
         <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/accounts"/>
         <property name="javax.persistence.jdbc.user" value="root"/>
         <property name="javax.persistence.jdbc.password" value=""/>
      </properties>
   </persistence-unit>

</persistence>



And my JPAUtil class:

Code:

public class JPAUtil {
   
   public static EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("money");
   
   public EntityManager getEntityManager(){
      return entityManagerFactory.createEntityManager();
   }

}



Any thoughts?

Thanks a lot!


Last edited by hannelita on Tue Jun 12, 2012 3:07 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Does Hibernate follow JPA for closing EntityManagerFactory?
PostPosted: Tue Jun 12, 2012 1:12 pm 
Newbie

Joined: Fri Jan 30, 2004 8:42 am
Posts: 6
Looking in the implementation, you are probably right:

https://github.com/hibernate/hibernate- ... yImpl.java

I like Hibernates behavior better, but you can't argue with the spec :)
I guess you should open a JIRA


Top
 Profile  
 
 Post subject: Re: Does Hibernate follow JPA for closing EntityManagerFactory?
PostPosted: Tue Jun 12, 2012 2:50 pm 
Newbie

Joined: Tue Aug 23, 2011 2:57 pm
Posts: 6
I think Hibernate behavior may cause some problems. For example, imagine for hbm2ddl for create-drop.... this really causes a big problem because the table will not exist anymore and another EntityManagerFactory will not be opened. However, another EntityManager is created even with the factory closed! I shall open a JIRA. Thanks a lot!


Top
 Profile  
 
 Post subject: Re: Does Hibernate follow JPA for closing EntityManagerFactory?
PostPosted: Tue Jun 12, 2012 3:08 pm 
Newbie

Joined: Tue Aug 23, 2011 2:57 pm
Posts: 6
I have just tested on 4.1.4 and it throws an exception.


Top
 Profile  
 
 Post subject: Re: Does Hibernate follow JPA for closing EntityManagerFactory?
PostPosted: Wed Jun 13, 2012 1:36 pm 
Newbie

Joined: Tue Aug 23, 2011 2:57 pm
Posts: 6
However, the eception that is thrown is not according to the spec... So I'll open a JIRA. Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.