KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > Ejb3Util


1 package org.hibernate.ce.auction.persistence;
2
3 import org.apache.commons.logging.*;
4
5 import javax.persistence.*;
6
7
8 /**
9  * Basic Hibernate helper class, handles EBJ3 EntityManager.
10  *
11  * @author christian@hibernate.org
12  */

13 public class Ejb3Util {
14
15     private static Log log = LogFactory.getLog(Ejb3Util.class);
16
17     public static EntityManagerFactory emf;
18     private static final ThreadLocal JavaDoc<EntityManager> threadEM = new ThreadLocal JavaDoc<EntityManager>();
19     private static final ThreadLocal JavaDoc<EntityTransaction> threadTransaction = new ThreadLocal JavaDoc<EntityTransaction>();
20
21     static {
22         try {
23             emf = Persistence.createEntityManagerFactory("caveatemptor");
24         } catch (Throwable JavaDoc ex) {
25             // We have to catch Throwable, otherwise we will miss
26
// NoClassDefFoundError and other subclasses of Error
27
log.error("Building EntityManagerFactory failed.", ex);
28             throw new ExceptionInInitializerError JavaDoc(ex);
29         }
30     }
31
32     public static EntityManager getCurrentEM() {
33         EntityManager em = (EntityManager) threadEM.get();
34         if (em == null) {
35             log.debug("Opening new EntityManager for this thread.");
36             em = emf.createEntityManager();
37             threadEM.set(em);
38         }
39         return em;
40     }
41
42     public static void closeEM() {
43         EntityManager em = (EntityManager) threadEM.get();
44         if (em != null && em.isOpen()) {
45             log.debug("Closing EntityManager of this thread.");
46             em.close();
47             threadEM.set(null);
48         }
49     }
50
51     public static void beginTransaction() {
52         EntityTransaction tx = (EntityTransaction) threadTransaction.get();
53         if (tx == null) {
54             log.debug("Starting new resource-local transaction in this thread.");
55             tx = getCurrentEM().getTransaction();
56             threadTransaction.set(tx);
57             tx.begin();
58         }
59     }
60
61     public static void commitTransaction() {
62         EntityTransaction tx = (EntityTransaction) threadTransaction.get();
63         try {
64             if ( tx != null ) {
65                 log.debug("Committing resource-local transaction of this thread.");
66                 tx.commit();
67             }
68             threadTransaction.set(null);
69         } catch (RuntimeException JavaDoc ex) {
70             rollbackTransaction();
71             throw ex;
72         }
73     }
74
75     public static void rollbackTransaction() {
76         EntityTransaction tx = (EntityTransaction) threadTransaction.get();
77         try {
78             threadTransaction.set(null);
79             if ( tx != null ) {
80                 log.debug("Tyring to rollback resource-local transaction of this thread.");
81                 tx.rollback();
82             }
83         } catch (RuntimeException JavaDoc ex) {
84             log.debug("Couldn't roll back transaction! Initial exception might get swallowed by this!");
85             throw ex;
86         } finally {
87             closeEM();
88         }
89     }
90
91 }
92
93
Popular Tags