private void registerTransactionScopedSync(HibernateProxy proxy, Key key) throws EntityManagerCreationException { try { TransactionScopedSync sync = new TransactionScopedSync(key, proxy); key.transaction.registerSynchronization(sync); } catch (RollbackException e) { throw new EntityManagerCreationException(e); } catch (SystemException e) { throw new EntityManagerCreationException(e); } }
public EntityManager getEntityManager(String unitName, HibernateProxy proxy, Transaction transaction) throws EntityManagerCreationException { // Note this method is thread-safe as a Transaction is only visible to a single thread at time. Key key = new Key(transaction, unitName); EntityManager em = cache.get(key); if (em == null) { // no entity manager for the persistence unit associated with the transaction EntityManagerFactory emf = emfCache.get(unitName); if (emf == null) { throw new EntityManagerCreationException("No EntityManagerFactory found for persistence unit: " + unitName); } em = emf.createEntityManager(); // don't synchronize on the transaction since it can assume to be bound to a thread at this point registerTransactionScopedSync(proxy, key); cache.put(key, em); } return em; }