📄 jpatransactionmanager.java
字号:
throw new IllegalTransactionStateException(
"Pre-bound JDBC Connection found! JpaTransactionManager does not support " +
"running within DataSourceTransactionManager if told to manage the DataSource itself. " +
"It is recommended to use a single JpaTransactionManager for all transactions " +
"on a single DataSource, no matter whether JPA or JDBC access.");
}
EntityManager em = null;
try {
if (txObject.getEntityManagerHolder() == null ||
txObject.getEntityManagerHolder().isSynchronizedWithTransaction()) {
EntityManager newEm = createEntityManagerForTransaction();
if (logger.isDebugEnabled()) {
logger.debug("Opened new EntityManager [" + newEm + "] for JPA transaction");
}
txObject.setEntityManagerHolder(new EntityManagerHolder(newEm), true);
}
txObject.getEntityManagerHolder().setSynchronizedWithTransaction(true);
em = txObject.getEntityManagerHolder().getEntityManager();
// Delegate to JpaDialect for actual transaction begin.
Object transactionData = getJpaDialect().beginTransaction(em, definition);
txObject.setTransactionData(transactionData);
// Register transaction timeout.
int timeout = determineTimeout(definition);
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
txObject.getEntityManagerHolder().setTimeoutInSeconds(timeout);
}
// Register the JPA EntityManager's JDBC Connection for the DataSource, if set.
if (getDataSource() != null) {
ConnectionHandle conHandle = getJpaDialect().getJdbcConnection(em, definition.isReadOnly());
if (conHandle != null) {
ConnectionHolder conHolder = new ConnectionHolder(conHandle);
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
conHolder.setTimeoutInSeconds(timeout);
}
if (logger.isDebugEnabled()) {
logger.debug("Exposing JPA transaction as JDBC transaction [" + conHolder.getConnectionHandle() + "]");
}
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
txObject.setConnectionHolder(conHolder);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Not exposing JPA transaction [" + em + "] as JDBC transaction because JpaDialect [" +
getJpaDialect() + "] does not support JDBC Connection retrieval");
}
}
}
// Bind the entity manager holder to the thread.
if (txObject.isNewEntityManagerHolder()) {
TransactionSynchronizationManager.bindResource(
getEntityManagerFactory(), txObject.getEntityManagerHolder());
}
}
catch (TransactionException ex) {
if (em != null) {
em.close();
}
throw ex;
}
catch (Exception ex) {
if (em != null) {
em.close();
}
throw new CannotCreateTransactionException("Could not open JPA EntityManager for transaction", ex);
}
}
/**
* 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 = getEntityManagerFactory();
if (emf instanceof EntityManagerFactoryInfo) {
emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory();
}
Map properties = getJpaPropertyMap();
return (!CollectionUtils.isEmpty(properties) ?
emf.createEntityManager(properties) : emf.createEntityManager());
}
protected Object doSuspend(Object transaction) {
JpaTransactionObject txObject = (JpaTransactionObject) transaction;
txObject.setEntityManagerHolder(null, false);
EntityManagerHolder entityManagerHolder = (EntityManagerHolder)
TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
txObject.setConnectionHolder(null);
ConnectionHolder connectionHolder = null;
if (getDataSource() != null) {
connectionHolder = (ConnectionHolder) TransactionSynchronizationManager.unbindResource(getDataSource());
}
return new SuspendedResourcesHolder(entityManagerHolder, connectionHolder);
}
protected void doResume(Object transaction, Object suspendedResources) {
SuspendedResourcesHolder resourcesHolder = (SuspendedResourcesHolder) suspendedResources;
TransactionSynchronizationManager.bindResource(
getEntityManagerFactory(), resourcesHolder.getEntityManagerHolder());
if (getDataSource() != null) {
TransactionSynchronizationManager.bindResource(getDataSource(), resourcesHolder.getConnectionHolder());
}
}
/**
* This implementation returns "true": a JPA commit will properly handle
* transactions that have been marked rollback-only at a global level.
*/
protected boolean shouldCommitOnGlobalRollbackOnly() {
return true;
}
protected void doCommit(DefaultTransactionStatus status) {
JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
if (status.isDebug()) {
logger.debug("Committing JPA transaction on EntityManager [" +
txObject.getEntityManagerHolder().getEntityManager() + "]");
}
try {
EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
tx.commit();
}
catch (RollbackException ex) {
throw new UnexpectedRollbackException(
"JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation)", ex);
}
catch (RuntimeException rawException) {
// Assumably failed to flush changes to database.
throw DataAccessUtils.translateIfNecessary(rawException, getJpaDialect());
}
}
protected void doRollback(DefaultTransactionStatus status) {
JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
if (status.isDebug()) {
logger.debug("Rolling back JPA transaction on EntityManager [" +
txObject.getEntityManagerHolder().getEntityManager() + "]");
}
try {
EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
if (tx.isActive()) {
tx.rollback();
}
}
catch (PersistenceException ex) {
throw new TransactionSystemException("Could not roll back JPA transaction", ex);
}
finally {
if (!txObject.isNewEntityManagerHolder()) {
// Clear all pending inserts/updates/deletes in the EntityManager.
// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
txObject.getEntityManagerHolder().getEntityManager().clear();
}
}
}
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
if (status.isDebug()) {
logger.debug("Setting JPA transaction on EntityManager [" +
txObject.getEntityManagerHolder().getEntityManager() + "] rollback-only");
}
txObject.setRollbackOnly();
}
protected void doCleanupAfterCompletion(Object transaction) {
JpaTransactionObject txObject = (JpaTransactionObject) transaction;
// Remove the entity manager holder from the thread.
if (txObject.isNewEntityManagerHolder()) {
TransactionSynchronizationManager.unbindResource(getEntityManagerFactory());
}
txObject.getEntityManagerHolder().clear();
// Remove the JDBC connection holder from the thread, if exposed.
if (txObject.hasConnectionHolder()) {
TransactionSynchronizationManager.unbindResource(getDataSource());
try {
getJpaDialect().releaseJdbcConnection(txObject.getConnectionHolder().getConnectionHandle(),
txObject.getEntityManagerHolder().getEntityManager());
}
catch (Exception ex) {
// Just log it, to keep a transaction-related exception.
logger.error("Could not close JDBC connection after transaction", ex);
}
}
getJpaDialect().cleanupTransaction(txObject.getTransactionData());
// Remove the entity manager holder from the thread.
if (txObject.isNewEntityManagerHolder()) {
EntityManager em = txObject.getEntityManagerHolder().getEntityManager();
if (logger.isDebugEnabled()) {
logger.debug("Closing JPA EntityManager [" + em + "] after transaction");
}
em.close();
}
else {
logger.debug("Not closing pre-bound JPA EntityManager after transaction");
}
}
/**
* JPA transaction object, representing a EntityManagerHolder.
* Used as transaction object by JpaTransactionManager.
*
* <p>Derives from JdbcTransactionObjectSupport in order to inherit the
* capability to manage JDBC 3.0 Savepoints for underlying JDBC Connections.
*
* @see EntityManagerHolder
*/
private static class JpaTransactionObject extends JdbcTransactionObjectSupport {
private EntityManagerHolder entityManagerHolder;
private boolean newEntityManagerHolder;
private Object transactionData;
public void setEntityManagerHolder(
EntityManagerHolder entityManagerHolder, boolean newEntityManagerHolder) {
this.entityManagerHolder = entityManagerHolder;
this.newEntityManagerHolder = newEntityManagerHolder;
}
public EntityManagerHolder getEntityManagerHolder() {
return this.entityManagerHolder;
}
public boolean isNewEntityManagerHolder() {
return this.newEntityManagerHolder;
}
public boolean hasTransaction() {
return (this.entityManagerHolder != null &&
this.entityManagerHolder.getEntityManager() != null &&
this.entityManagerHolder.getEntityManager().getTransaction().isActive());
}
public void setTransactionData(Object transactionData) {
this.transactionData = transactionData;
}
public Object getTransactionData() {
return this.transactionData;
}
public void setRollbackOnly() {
EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
if (tx.isActive()) {
tx.setRollbackOnly();
}
if (hasConnectionHolder()) {
getConnectionHolder().setRollbackOnly();
}
}
public boolean isRollbackOnly() {
EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
return tx.getRollbackOnly();
}
}
/**
* Holder for suspended resources.
* Used internally by <code>doSuspend</code> and <code>doResume</code>.
*/
private static class SuspendedResourcesHolder {
private final EntityManagerHolder entityManagerHolder;
private final ConnectionHolder connectionHolder;
private SuspendedResourcesHolder(EntityManagerHolder emHolder, ConnectionHolder conHolder) {
this.entityManagerHolder = emHolder;
this.connectionHolder = conHolder;
}
private EntityManagerHolder getEntityManagerHolder() {
return this.entityManagerHolder;
}
private ConnectionHolder getConnectionHolder() {
return this.connectionHolder;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -