]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5954593e9c4757dba25feccd89fd392e240c2242
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.common.storage.jpa;
2
3 import javax.persistence.EntityManager;
4 import javax.persistence.EntityManagerFactory;
5 import javax.persistence.Query;
6
7 import org.collectionspace.services.common.context.ServiceContext;
8 import org.collectionspace.services.common.document.TransactionException;
9 import org.collectionspace.services.common.storage.TransactionContext;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 @SuppressWarnings("rawtypes")
14 public class JPATransactionContext extends TransactionContext {
15     /** The logger. */
16     private final Logger logger = LoggerFactory.getLogger(TransactionContext.class);
17
18         private int transactionRefCount = 0;
19         private Boolean commitSuccessful = null;
20         
21         EntityManagerFactory emf;
22         EntityManager em;
23         
24         public JPATransactionContext(ServiceContext ctx) {
25         emf = JpaStorageUtils.getEntityManagerFactory();            
26         em = emf.createEntityManager();
27         this.ctx = ctx;
28         }
29         
30         public JPATransactionContext() {
31         emf = JpaStorageUtils.getEntityManagerFactory();            
32         em = emf.createEntityManager();
33         }       
34
35         protected EntityManagerFactory getEntityManagerFactory() {
36                 return emf;
37         }
38         
39         protected EntityManager getEntityManager() {
40                 return em;
41         }
42         
43         @Override
44         public ServiceContext getServiceContext() {
45                 return ctx;
46         }
47         
48         @Override
49         public void markForRollback() {
50                 if (em.getTransaction().isActive() == true) {
51                         em.getTransaction().setRollbackOnly();
52                 } else {
53                         String msg = "Attemped to mark an inactive transaction for rollback.";
54                         logger.warn(msg);
55                 }
56         }
57         
58         @Override
59         public void close() throws TransactionException  {
60                 if (em.getTransaction().isActive() == true && em.getTransaction().getRollbackOnly() == true) {
61                         em.getTransaction().rollback();
62         } else if (em.getTransaction().isActive() == true) {
63                 throw new JPATransactionException("There is an active transaction.  You must commit the active transaction prior to calling this close method.");
64         }
65         
66                 em.close();
67         JpaStorageUtils.releaseEntityManagerFactory(emf);
68         }
69
70         @Override
71         synchronized public void beginTransaction() {
72                 if (transactionRefCount == 0) {
73                         em.getTransaction().begin();
74                 }
75         transactionRefCount++;
76         }
77         
78         @Override
79         public void persist(Object entity) {
80                 em.persist(entity);
81         }
82         
83         @Override
84         public Object merge(Object entity) {
85                 return em.merge(entity);
86         }
87         
88         @Override
89         public Object find(Class entityClass, Object primaryKey) {
90                 return em.find(entityClass, primaryKey);
91         }
92         
93         @Override
94         public Object find(Class entityClass, String id) {
95                 return em.find(entityClass, id);
96         }
97         
98         @Override
99         public Query createQuery(String qlString) {
100                 return em.createQuery(qlString);
101         }
102         
103         @Override
104     public void remove(Object entity) {
105                 em.remove(entity);
106         }
107
108         @Override
109         public void commitTransaction() throws TransactionException {
110                 if (transactionRefCount == 0) {
111                 throw new JPATransactionException("There is no active transaction to commit.");
112                 }
113                 if (--transactionRefCount == 0) {
114                         em.getTransaction().commit();
115                         commitSuccessful = Boolean.TRUE;
116                 }
117         }
118
119         @Override
120         public boolean isTransactionActive() {
121                 return em.getTransaction().isActive();
122         }
123 }