KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > CurrentEntityManagerImpl


1 //$Id: CurrentEntityManagerImpl.java,v 1.9 2005/07/21 00:08:34 epbernard Exp $
2
package org.hibernate.ejb;
3
4 import javax.persistence.PersistenceContextType;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.hibernate.Session;
9 import org.hibernate.SessionFactory;
10 import org.hibernate.engine.SessionFactoryImplementor;
11 import org.hibernate.engine.SessionImplementor;
12 import org.hibernate.util.JTAHelper;
13
14 /**
15  * @author Gavin King
16  */

17 public class CurrentEntityManagerImpl extends AbstractEntityManagerImpl {
18     private static Log log = LogFactory.getLog( CurrentEntityManagerImpl.class );
19
20     private SessionFactory sessionFactory;
21
22     public CurrentEntityManagerImpl(SessionFactory sessionFactory) {
23         super( PersistenceContextType.TRANSACTION );
24         this.sessionFactory = sessionFactory;
25     }
26
27     public Session getSession() {
28         /**
29          * Handle non transactional mode by requesting a temporary session to the session factory
30          * This session, will aggressively use the AFTER_STATEMENT connection release mode to be
31          * sure the conenctions are released. Be aware that the session will not be closed explicitly.
32          */

33
34         Session s;
35         SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
36         if ( !JTAHelper.isTransactionInProgress( sfi ) ) {
37             s = sfi.openTemporarySession();
38             ( (SessionImplementor) s ).setAutoClear( true );
39         }
40         else {
41             s = sessionFactory.getCurrentSession();
42         }
43         return s;
44     }
45
46     public void close() {
47         throw new UnsupportedOperationException JavaDoc( "cannot close the JTA-bound EntityManager" );
48     }
49
50     public boolean isOpen() {
51         return true;
52     }
53
54 }
55
Popular Tags