1 17 18 package org.apache.geronimo.persistence; 19 20 import java.util.concurrent.atomic.AtomicInteger ; 21 22 import javax.persistence.EntityManager; 23 import javax.persistence.EntityTransaction; 24 import javax.persistence.FlushModeType; 25 import javax.persistence.LockModeType; 26 import javax.persistence.Query; 27 28 import org.apache.geronimo.transaction.manager.TransactionImpl; 29 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 30 31 37 public class InternalCMPEntityManagerExtended implements EntityManager, EntityManagerWrapper { 38 39 private final EntityManager entityManager; 40 private final String persistenceUnit; 41 private final TransactionManagerImpl transactionManager; 42 private final AtomicInteger count = new AtomicInteger (); 44 45 public InternalCMPEntityManagerExtended(EntityManager entityManager, String persistenceUnit, TransactionManagerImpl transactionManager) { 46 this.entityManager = entityManager; 47 this.persistenceUnit = persistenceUnit; 48 this.transactionManager = transactionManager; 49 if (transactionManager.getTransaction() != null) { 50 joinTransaction(); 51 } 52 } 53 54 void registerBean() { 55 count.getAndIncrement(); 56 } 57 58 void beanRemoved() { 59 if (count.decrementAndGet() ==0 ) { 60 entityManager.close(); 61 EntityManagerExtendedRegistry.clearEntityManager(persistenceUnit); 62 } 63 64 } 65 66 public EntityManager getEntityManager() { 67 return entityManager; 68 } 69 70 public void persist(Object o) { 71 entityManager.persist(o); 72 } 73 74 public <T>T merge(T t) { 75 return entityManager.merge(t); 76 } 77 78 public void remove(Object o) { 79 entityManager.remove(o); 80 } 81 82 public <T>T find(Class <T> aClass, Object o) { 83 return entityManager.find(aClass, o); 84 } 85 86 public <T>T getReference(Class <T> aClass, Object o) { 87 return entityManager.getReference(aClass, o); 88 } 89 90 public void flush() { 91 entityManager.flush(); 92 } 93 94 public void setFlushMode(FlushModeType flushModeType) { 95 entityManager.setFlushMode(flushModeType); 96 } 97 98 public FlushModeType getFlushMode() { 99 return entityManager.getFlushMode(); 100 } 101 102 public void lock(Object o, LockModeType lockModeType) { 103 entityManager.lock(o, lockModeType); 104 } 105 106 public void refresh(Object o) { 107 entityManager.refresh(o); 108 } 109 110 public void clear() { 111 entityManager.clear(); 112 } 113 114 public boolean contains(Object o) { 115 return entityManager.contains(o); 116 } 117 118 public Query createQuery(String s) { 119 return entityManager.createQuery(s); 120 } 121 122 public Query createNamedQuery(String s) { 123 return entityManager.createNamedQuery(s); 124 } 125 126 public Query createNativeQuery(String s) { 127 return entityManager.createNativeQuery(s); 128 } 129 130 public Query createNativeQuery(String s, Class aClass) { 131 return entityManager.createNativeQuery(s, aClass); 132 } 133 134 public Query createNativeQuery(String s, String s1) { 135 return entityManager.createNativeQuery(s, s1); 136 } 137 138 public void close() { 139 } 141 142 public boolean isOpen() { 143 return true; 144 } 145 146 public EntityTransaction getTransaction() { 147 throw new IllegalStateException ("You cannot call getTransaction on a container managed EntityManager"); 148 } 149 150 public void joinTransaction() { 151 TransactionImpl transaction = (TransactionImpl) transactionManager.getTransaction(); 152 transaction.setEntityManager(persistenceUnit, this); 154 entityManager.joinTransaction(); 155 } 156 157 public Object getDelegate() { 158 return entityManager.getDelegate(); 159 } 160 161 } 162 | Popular Tags |