| Method from org.springframework.orm.jpa.EntityManagerFactoryUtils Detail: |
public static void closeEntityManager(EntityManager em) {
if (em != null) {
logger.debug("Closing JPA EntityManager");
try {
em.close();
}
catch (PersistenceException ex) {
logger.debug("Could not close JPA EntityManager", ex);
}
catch (Throwable ex) {
logger.debug("Unexpected exception on closing JPA EntityManager", ex);
}
}
}
Close the given JPA EntityManager,
catching and logging any cleanup exceptions thrown. |
public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex) {
// Following the JPA specification, a persistence provider can also
// throw these two exceptions, besides PersistenceException.
if (ex instanceof IllegalStateException) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
if (ex instanceof IllegalArgumentException) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
// Check for well-known PersistenceException subclasses.
if (ex instanceof EntityNotFoundException) {
return new JpaObjectRetrievalFailureException((EntityNotFoundException) ex);
}
if (ex instanceof NoResultException) {
return new EmptyResultDataAccessException(ex.getMessage(), 1);
}
if (ex instanceof NonUniqueResultException) {
return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1);
}
if (ex instanceof OptimisticLockException) {
return new JpaOptimisticLockingFailureException((OptimisticLockException) ex);
}
if (ex instanceof EntityExistsException) {
return new DataIntegrityViolationException(ex.getMessage(), ex);
}
if (ex instanceof TransactionRequiredException) {
return new InvalidDataAccessApiUsageException(ex.getMessage(), ex);
}
// If we have another kind of PersistenceException, throw it.
if (ex instanceof PersistenceException) {
return new JpaSystemException((PersistenceException) ex);
}
// If we get here, we have an exception that resulted from user code,
// rather than the persistence provider, so we return null to indicate
// that translation should not occur.
return null;
}
Convert the given runtime exception to an appropriate exception from the
org.springframework.dao hierarchy.
Return null if no translation is appropriate: any other exception may
have resulted from user code, and should not be translated.
The most important cases like object not found or optimistic locking
failure are covered here. For more fine-granular conversion, JpaAccessor and
JpaTransactionManager support sophisticated translation of exceptions via a
JpaDialect. |
public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf,
Map properties) throws PersistenceException {
Assert.notNull(emf, "No EntityManagerFactory specified");
EntityManagerHolder emHolder =
(EntityManagerHolder) TransactionSynchronizationManager.getResource(emf);
if (emHolder != null) {
if (!emHolder.isSynchronizedWithTransaction() &&
TransactionSynchronizationManager.isSynchronizationActive()) {
// Try to explicitly synchronize the EntityManager itself
// with an ongoing JTA transaction, if any.
try {
emHolder.getEntityManager().joinTransaction();
}
catch (TransactionRequiredException ex) {
logger.debug("Could not join JTA transaction because none was active", ex);
}
Object transactionData = prepareTransaction(emHolder.getEntityManager(), emf);
TransactionSynchronizationManager.registerSynchronization(
new EntityManagerSynchronization(emHolder, emf, transactionData, false));
emHolder.setSynchronizedWithTransaction(true);
}
return emHolder.getEntityManager();
}
if (!TransactionSynchronizationManager.isSynchronizationActive()) {
// Indicate that we can't obtain a transactional EntityManager.
return null;
}
// Create a new EntityManager for use within the current transaction.
logger.debug("Opening JPA EntityManager");
EntityManager em =
(!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager());
if (TransactionSynchronizationManager.isSynchronizationActive()) {
logger.debug("Registering transaction synchronization for JPA EntityManager");
// Use same EntityManager for further JPA actions within the transaction.
// Thread object will get removed by synchronization at transaction completion.
emHolder = new EntityManagerHolder(em);
Object transactionData = prepareTransaction(em, emf);
TransactionSynchronizationManager.registerSynchronization(
new EntityManagerSynchronization(emHolder, emf, transactionData, true));
emHolder.setSynchronizedWithTransaction(true);
TransactionSynchronizationManager.bindResource(emf, emHolder);
}
return em;
}
Obtain a JPA EntityManager from the given factory. Is aware of a
corresponding EntityManager bound to the current thread,
for example when using JpaTransactionManager.
Same as getEntityManager, but throwing the original PersistenceException. |
public static EntityManagerFactory findEntityManagerFactory(ListableBeanFactory beanFactory,
String unitName) throws NoSuchBeanDefinitionException {
Assert.notNull(beanFactory, "ListableBeanFactory must not be null");
Assert.hasLength(unitName, "Unit name must not be empty");
// See whether we can find an EntityManagerFactory with matching persistence unit name.
String[] candidateNames =
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class);
for (String candidateName : candidateNames) {
EntityManagerFactory emf = (EntityManagerFactory) beanFactory.getBean(candidateName);
if (emf instanceof EntityManagerFactoryInfo) {
if (unitName.equals(((EntityManagerFactoryInfo) emf).getPersistenceUnitName())) {
return emf;
}
}
}
// No matching persistence unit found - simply take the EntityManagerFactory
// with the persistence unit name as bean name (by convention).
return (EntityManagerFactory) beanFactory.getBean(unitName, EntityManagerFactory.class);
}
Find an EntityManagerFactory with the given name in the given
Spring application context (represented as ListableBeanFactory).
The specified unit name will be matched against the configured
peristence unit, provided that a discovered EntityManagerFactory
implements the EntityManagerFactoryInfo interface. If not,
the persistence unit name will be matched against the Spring bean name,
assuming that the EntityManagerFactory bean names follow that convention. |
public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf) throws DataAccessResourceFailureException {
return getTransactionalEntityManager(emf, null);
}
|
public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf,
Map properties) throws DataAccessResourceFailureException {
try {
return doGetTransactionalEntityManager(emf, properties);
}
catch (PersistenceException ex) {
throw new DataAccessResourceFailureException("Could not obtain JPA EntityManager", ex);
}
}
|