@Override public Object getResourceFactory() { return obtainEntityManagerFactory(); }
@Override public Object getResourceFactory() { return obtainEntityManagerFactory(); }
/** * Create a JPA EntityManager to be used for a transaction. * <p>The default implementation checks whether the EntityManagerFactory * is a Spring proxy and unwraps it first. * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory() */ protected EntityManager createEntityManagerForTransaction() { EntityManagerFactory emf = obtainEntityManagerFactory(); if (emf instanceof EntityManagerFactoryInfo) { emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); } Map<String, Object> properties = getJpaPropertyMap(); return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); }
@Override protected void doResume(@Nullable Object transaction, Object suspendedResources) { SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources; TransactionSynchronizationManager.bindResource( obtainEntityManagerFactory(), resourcesHolder.getEntityManagerHolder()); if (getDataSource() != null && resourcesHolder.getConnectionHolder() != null) { TransactionSynchronizationManager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder()); } }
/** * Create a JPA EntityManager to be used for a transaction. * <p>The default implementation checks whether the EntityManagerFactory * is a Spring proxy and unwraps it first. * @see javax.persistence.EntityManagerFactory#createEntityManager() * @see EntityManagerFactoryInfo#getNativeEntityManagerFactory() */ protected EntityManager createEntityManagerForTransaction() { EntityManagerFactory emf = obtainEntityManagerFactory(); if (emf instanceof EntityManagerFactoryInfo) { emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); } Map<String, Object> properties = getJpaPropertyMap(); return (!CollectionUtils.isEmpty(properties) ? emf.createEntityManager(properties) : emf.createEntityManager()); }
@Override protected void doResume(@Nullable Object transaction, Object suspendedResources) { SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources; TransactionSynchronizationManager.bindResource( obtainEntityManagerFactory(), resourcesHolder.getEntityManagerHolder()); if (getDataSource() != null && resourcesHolder.getConnectionHolder() != null) { TransactionSynchronizationManager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder()); } }
@Override protected Object doGetTransaction() { JpaTransactionObject txObject = new JpaTransactionObject(); txObject.setSavepointAllowed(isNestedTransactionAllowed()); EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(obtainEntityManagerFactory()); if (emHolder != null) { if (logger.isDebugEnabled()) { logger.debug("Found thread-bound EntityManager [" + emHolder.getEntityManager() + "] for JPA transaction"); } txObject.setEntityManagerHolder(emHolder, false); } if (getDataSource() != null) { ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(getDataSource()); txObject.setConnectionHolder(conHolder); } return txObject; }
@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); }
TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory());
@Override protected Object doGetTransaction() { JpaTransactionObject txObject = new JpaTransactionObject(); txObject.setSavepointAllowed(isNestedTransactionAllowed()); EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager.getResource(obtainEntityManagerFactory()); if (emHolder != null) { if (logger.isDebugEnabled()) { logger.debug("Found thread-bound EntityManager [" + emHolder.getEntityManager() + "] for JPA transaction"); } txObject.setEntityManagerHolder(emHolder, false); } if (getDataSource() != null) { ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(getDataSource()); txObject.setConnectionHolder(conHolder); } return txObject; }
@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); }
obtainEntityManagerFactory(), txObject.getEntityManagerHolder());
TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory());
obtainEntityManagerFactory(), txObject.getEntityManagerHolder());