@Override protected void releaseResource(EntityManagerHolder resourceHolder, EntityManagerFactory resourceKey) { closeEntityManager(resourceHolder.getEntityManager()); } }
/** * Create a transactional EntityManager proxy for the given EntityManagerFactory. * @param emf the EntityManagerFactory to delegate to. * @return a shareable transaction EntityManager proxy */ public static EntityManager createSharedEntityManager(EntityManagerFactory emf) { return createSharedEntityManager(emf, null, true); }
/** * Create a new JpaTransactionManager instance. * @param emf the EntityManagerFactory to manage transactions for */ public JpaTransactionManager(EntityManagerFactory emf) { this(); this.entityManagerFactory = emf; afterPropertiesSet(); }
@Override public void flush() { try { getEntityManagerHolder().getEntityManager().flush(); } catch (RuntimeException ex) { throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect()); } }
/** * Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any. * @return the transactional EntityManager, or {@code null} if none * @throws IllegalStateException if this accessor is not configured with an EntityManagerFactory * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory) * @see EntityManagerFactoryUtils#getTransactionalEntityManager(javax.persistence.EntityManagerFactory, java.util.Map) */ @Nullable protected EntityManager getTransactionalEntityManager() throws IllegalStateException{ EntityManagerFactory emf = obtainEntityManagerFactory(); return EntityManagerFactoryUtils.getTransactionalEntityManager(emf, getJpaPropertyMap()); }
@Override protected Object doSuspend(Object transaction) { JpaTransactionObject txObject = (JpaTransactionObject) transaction; txObject.setEntityManagerHolder(null, false); EntityManagerHolder entityManagerHolder = (EntityManagerHolder) TransactionSynchronizationManager.unbindResource(obtainEntityManagerFactory()); txObject.setConnectionHolder(null); ConnectionHolder connectionHolder = null; if (getDataSource() != null && TransactionSynchronizationManager.hasResource(getDataSource())) { connectionHolder = (ConnectionHolder) TransactionSynchronizationManager.unbindResource(getDataSource()); } return new SuspendedResourcesHolder(entityManagerHolder, connectionHolder); }
public void setTransactionData(@Nullable Object transactionData) { this.transactionData = transactionData; getEntityManagerHolder().setTransactionActive(true); if (transactionData instanceof SavepointManager) { getEntityManagerHolder().setSavepointManager((SavepointManager) transactionData); } }
private SavepointManager getSavepointManager() { if (!isSavepointAllowed()) { throw new NestedTransactionNotSupportedException( "Transaction manager does not allow nested transactions"); } SavepointManager savepointManager = getEntityManagerHolder().getSavepointManager(); if (savepointManager == null) { throw new NestedTransactionNotSupportedException( "JpaDialect does not support savepoints - check your JPA provider's capabilities"); } return savepointManager; } }
@Override public void rollbackToSavepoint(Object savepoint) throws TransactionException { getSavepointManager().rollbackToSavepoint(savepoint); getEntityManagerHolder().resetRollbackOnly(); }
@Test public void testExceptionTranslationWithDialectFoundOnIntroducedEntityManagerInfo() throws Exception { doTestExceptionTranslationWithDialectFound(((EntityManagerFactoryInfo) entityManagerFactory).getJpaDialect()); }
/** * Create a container-managed extended EntityManager proxy. * @param emf the EntityManagerFactory to create the EntityManager with. * If this implements the EntityManagerFactoryInfo interface, the corresponding * JpaDialect and PersistenceUnitInfo will be detected accordingly. * @return a container-managed EntityManager that will automatically participate * in any managed transaction * @see javax.persistence.EntityManagerFactory#createEntityManager() */ public static EntityManager createContainerManagedEntityManager(EntityManagerFactory emf) { return createContainerManagedEntityManager(emf, null, true); }
/** * Create an application-managed extended EntityManager proxy. * @param rawEntityManager the raw EntityManager to decorate * @param emfInfo the EntityManagerFactoryInfo to obtain the JpaDialect * and PersistenceUnitInfo from * @return an application-managed EntityManager that can join transactions * but does not participate in them automatically */ public static EntityManager createApplicationManagedEntityManager( EntityManager rawEntityManager, EntityManagerFactoryInfo emfInfo) { return createProxy(rawEntityManager, emfInfo, false, false); }
/** * Create a new JpaTransactionManager instance. * <p>An EntityManagerFactory has to be set to be able to use it. * @see #setEntityManagerFactory */ public JpaTransactionManager() { setNestedTransactionAllowed(true); }
@Override protected boolean isExistingTransaction(Object transaction) { return ((JpaTransactionObject) transaction).hasTransaction(); }
@Override public void clear() { super.clear(); this.transaction = null; this.previousFlushMode = null; }
@Override public Object getResourceFactory() { return obtainEntityManagerFactory(); }
public LocalContainerEntityManagerFactoryBean parseValidPersistenceUnit() throws Exception { LocalContainerEntityManagerFactoryBean emfb = createEntityManagerFactoryBean( "org/springframework/orm/jpa/domain/persistence.xml", null, "Person"); return emfb; }
@Override protected void releaseResource(EntityManagerHolder resourceHolder, EntityManagerFactory resourceKey) { closeEntityManager(resourceHolder.getEntityManager()); }
/** * Create a container-managed extended EntityManager proxy. * @param rawEntityManager the raw EntityManager to decorate * @param emfInfo the EntityManagerFactoryInfo to obtain the JpaDialect * and PersistenceUnitInfo from * @return a container-managed EntityManager that will automatically participate * in any managed transaction */ public static EntityManager createContainerManagedEntityManager( EntityManager rawEntityManager, EntityManagerFactoryInfo emfInfo) { return createProxy(rawEntityManager, emfInfo, true, true); }
private void closeEntityManager() { if (this.timeoutInProgress || this.errorInProgress) { logger.debug("Closing JPA EntityManager after async request timeout/error"); EntityManagerFactoryUtils.closeEntityManager(this.emHolder.getEntityManager()); } }