}
}
private EntityManager getEntityManager() {
Transaction tx = TransactionCoordination.getInstance().getTransaction();
if (tx != null) {
if (tx.hasResource(entityManagerFactory)) {
log.debug("Retrieving session from current transaction");
Object r = tx.getResource(entityManagerFactory);
if (r instanceof JpaXaResource) {
return ((JpaXaResource) r).getEntityManager();
} else {
return (EntityManager) r;
}
}
}
log.debug("Retrieving new entityManager from factory");
EntityManager entityManager;
if (jpaProperties != null && !jpaProperties.isEmpty()) {
entityManager = entityManagerFactory.createEntityManager(jpaProperties);
} else {
entityManager = entityManagerFactory.createEntityManager();
}
if (tx != null) {
log.debug("Binding session to current transaction");
try {
Object r;
if (tx instanceof XaTransaction) {
if (isUnmanagedPoolAndOpenJpa) {
r = new JpaXaResourceForTomcatAndOpenJpa(entityManager);
} else {
r = new JpaXaResource(entityManager);
}
} else {
r = entityManager;
}
tx.bindResource(entityManagerFactory, r);
} catch (TransactionException e) {
throw new RuntimeException("Could not bind connection to current transaction", e);
}
}
return entityManager;