/** * Close the current transaction's EntityManager. * Called after a transaction begin attempt failed. * @param txObject the current transaction */ protected void closeEntityManagerAfterFailedBegin(JpaTransactionObject txObject) { if (txObject.isNewEntityManagerHolder()) { EntityManager em = txObject.getEntityManagerHolder().getEntityManager(); try { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } } catch (Throwable ex) { logger.debug("Could not rollback EntityManager after failed transaction begin", ex); } finally { EntityManagerFactoryUtils.closeEntityManager(em); } txObject.setEntityManagerHolder(null, false); } }
@Override 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(); } } }
@Override public boolean isRollbackOnly() { EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction(); return tx.getRollbackOnly(); }
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 Object createSavepoint() throws TransactionException { if (getEntityManagerHolder().isRollbackOnly()) { throw new CannotCreateTransactionException( "Cannot create savepoint for transaction which is already marked as rollback-only"); } return getSavepointManager().createSavepoint(); }
@Override 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(); }
@Override public void flush() { try { getEntityManagerHolder().getEntityManager().flush(); } catch (RuntimeException ex) { throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect()); } }
@Override public void rollbackToSavepoint(Object savepoint) throws TransactionException { getSavepointManager().rollbackToSavepoint(savepoint); getEntityManagerHolder().resetRollbackOnly(); }
public void setRollbackOnly() { EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
@Override public boolean isRollbackOnly() { EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction(); return tx.getRollbackOnly(); }
public void setTransactionData(@Nullable Object transactionData) { this.transactionData = transactionData; getEntityManagerHolder().setTransactionActive(true); if (transactionData instanceof SavepointManager) { getEntityManagerHolder().setSavepointManager((SavepointManager) transactionData); } }
TransactionSynchronizationManager.unbindResourceIfPossible(obtainEntityManagerFactory()); txObject.getEntityManagerHolder().clear(); try { getJpaDialect().releaseJdbcConnection(conHandle, txObject.getEntityManagerHolder().getEntityManager()); EntityManager em = txObject.getEntityManagerHolder().getEntityManager(); if (logger.isDebugEnabled()) { logger.debug("Closing JPA EntityManager [" + em + "] after transaction");
@Override 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) { if (ex.getCause() instanceof RuntimeException) { DataAccessException dae = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause()); if (dae != null) { throw dae; } } throw new TransactionSystemException("Could not commit JPA transaction", ex); } catch (RuntimeException ex) { // Assumably failed to flush changes to database. throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect()); } }
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(); }
public void setRollbackOnly() { EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
@Override 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(); }
@Override public Object createSavepoint() throws TransactionException { if (getEntityManagerHolder().isRollbackOnly()) { throw new CannotCreateTransactionException( "Cannot create savepoint for transaction which is already marked as rollback-only"); } return getSavepointManager().createSavepoint(); }
@Override public void flush() { try { getEntityManagerHolder().getEntityManager().flush(); } catch (RuntimeException ex) { throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect()); } }
public void setTransactionData(@Nullable Object transactionData) { this.transactionData = transactionData; getEntityManagerHolder().setTransactionActive(true); if (transactionData instanceof SavepointManager) { getEntityManagerHolder().setSavepointManager((SavepointManager) transactionData); } }