KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > entity > ManagedEntityManagerFactory


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.entity;
23
24 import javax.persistence.EntityManager;
25 import javax.persistence.EntityManagerFactory;
26 import javax.persistence.PersistenceContextType;
27 import javax.persistence.TransactionRequiredException;
28 import javax.transaction.RollbackException JavaDoc;
29 import javax.transaction.Synchronization JavaDoc;
30 import javax.transaction.SystemException JavaDoc;
31 import javax.transaction.Transaction JavaDoc;
32 import org.jboss.logging.Logger;
33 import org.jboss.tm.TransactionLocal;
34 import org.jboss.tm.TxManager;
35 import org.jboss.tm.TxUtils;
36 import org.jboss.ejb3.ThreadLocalStack;
37 import org.jboss.ejb3.tx.TxUtil;
38
39 import java.util.IdentityHashMap JavaDoc;
40 import java.util.Map JavaDoc;
41
42 /**
43  * @author <a HREF="mailto:gavine@hibernate.org">Gavin King</a>
44  * @version $Revision: 56987 $
45  */

46 public class ManagedEntityManagerFactory
47 {
48    private static final Logger log = Logger.getLogger(ManagedEntityManagerFactory.class);
49
50    protected EntityManagerFactory entityManagerFactory;
51    protected TransactionLocal session = new TransactionLocal(TxUtil.getTransactionManager());
52    protected String JavaDoc kernelName;
53
54    public static ThreadLocalStack<Map JavaDoc> nonTxStack = new ThreadLocalStack<Map JavaDoc>();
55
56    public EntityManager getNonTxEntityManager()
57    {
58       Map JavaDoc map = nonTxStack.get();
59       EntityManager em = (EntityManager)map.get(this);
60       if (em == null)
61       {
62          em = entityManagerFactory.createEntityManager();
63          map.put(this, em);
64       }
65       return em;
66    }
67
68    public ManagedEntityManagerFactory(EntityManagerFactory sf, String JavaDoc kernelName)
69    {
70       this.entityManagerFactory = sf;
71       this.kernelName = kernelName;
72    }
73
74    public EntityManagerFactory getEntityManagerFactory()
75    {
76       return entityManagerFactory;
77    }
78
79    public String JavaDoc getKernelName()
80    {
81       return kernelName;
82    }
83
84    public void destroy()
85    {
86       entityManagerFactory.close();
87    }
88
89    private static class SessionSynchronization implements Synchronization JavaDoc
90    {
91       private EntityManager manager;
92       private Transaction JavaDoc tx;
93       private boolean closeAtTxCompletion;
94
95       public SessionSynchronization(EntityManager session, Transaction JavaDoc tx, boolean close)
96       {
97          this.manager = session;
98          this.tx = tx;
99          closeAtTxCompletion = close;
100       }
101
102       public void beforeCompletion()
103       {
104          /* IF THIS GETS REACTIVATED THEN YOU MUST remove the if(closeAtTxCompletion) block in getSession()
105          try
106          {
107             int status = tx.getStatus();
108             if (status != Status.STATUS_ROLLEDBACK && status != Status.STATUS_ROLLING_BACK && status != Status.STATUS_MARKED_ROLLBACK)
109             {
110                if (FlushModeInterceptor.getTxFlushMode() != FlushModeType.NEVER)
111                {
112                   log.debug("************** flushing.....");
113                   manager.flush();
114                }
115             }
116          }
117          catch (SystemException e)
118          {
119             throw new RuntimeException(e);
120          }
121          */

122       }
123
124       public void afterCompletion(int status)
125       {
126          if (closeAtTxCompletion)
127          {
128             log.debug("************** closing entity managersession **************");
129             manager.close();
130          }
131       }
132    }
133
134    public static ThreadLocal JavaDoc longLivedSession = new ThreadLocal JavaDoc();
135
136    public TransactionLocal getTransactionSession()
137    {
138       return session;
139    }
140
141    public void registerExtendedWithTransaction(EntityManager pc)
142    {
143       pc.joinTransaction();
144       session.set(pc);
145    }
146
147    public void verifyInTx()
148    {
149       Transaction JavaDoc tx = session.getTransaction();
150       if (tx == null || !TxUtils.isActive(tx)) throw new TransactionRequiredException("EntityManager must be access within a transaction");
151       if (!TxUtils.isActive(tx))
152          throw new TransactionRequiredException("Transaction must be active to access EntityManager");
153    }
154    public boolean isInTx()
155    {
156       Transaction JavaDoc tx = session.getTransaction();
157       if (tx == null || !TxUtils.isActive(tx)) return false;
158       return true;
159    }
160
161    public EntityManager getTransactionScopedEntityManager()
162    {
163       Transaction JavaDoc tx = session.getTransaction();
164       if (tx == null || !TxUtils.isActive(tx)) return getNonTxEntityManager();
165
166       EntityManager rtnSession = (EntityManager) session.get();
167       if (rtnSession == null)
168       {
169          rtnSession = createEntityManager();
170          try
171          {
172             tx.registerSynchronization(new SessionSynchronization(rtnSession, tx, true));
173          }
174          catch (RollbackException JavaDoc e)
175          {
176             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
177
}
178          catch (SystemException JavaDoc e)
179          {
180             throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
181
}
182          session.set(rtnSession);
183          rtnSession.joinTransaction(); // force registration with TX
184
}
185       return rtnSession;
186    }
187
188    public EntityManager createEntityManager()
189    {
190       return entityManagerFactory.createEntityManager();
191    }
192
193
194 }
195
Popular Tags