public void execute(CommandContext commandContext) { if (isTransactionActive()) { entityManager.getTransaction().rollback(); } } };
factorySupplier.get().createEntityManager(properties); function.beforeTransactionCompletion(); txn = entityManager.getTransaction(); txn.begin(); result = function.apply( entityManager ); txn.commit(); txn.rollback();
@Test public void testMarkRollbackOnlyAnActiveTransaction() { EntityTransaction transaction = entityManager.getTransaction(); final TransactionImplementor hibernateTransaction = (TransactionImplementor) transaction; transaction.begin(); hibernateTransaction.markRollbackOnly(); transaction.rollback(); assertFalse( transaction.isActive() ); } }
@Override public void close() { if (this.em.getTransaction().isActive()) { if (this.rollbackOnly) { this.em.getTransaction().rollback(); } else { this.em.getTransaction().commit(); } } this.em.close(); } }
factorySupplier.get().createEntityManager(properties); function.beforeTransactionCompletion(); txn = entityManager.getTransaction(); txn.begin(); function.accept( entityManager ); if ( !txn.getRollbackOnly() ) { txn.commit(); txn.rollback(); if ( txn != null && txn.isActive() ) { try { txn.rollback();
@Test @TestForIssue(jiraKey = "HHH-12138") public void testStoredProcedureReturnValue() { EntityManager entityManager = createEntityManager(); entityManager.getTransaction().begin(); try { Integer phoneCount = (Integer) entityManager .createNativeQuery( "SELECT fn_count_phones(:personId) FROM SYS.DUMMY" ) .setParameter( "personId", 1 ) .getSingleResult(); assertEquals( Integer.valueOf( 2 ), phoneCount ); } finally { entityManager.getTransaction().rollback(); entityManager.close(); } }
public class EntityUtil { public static void executeWithEntityManager(EntityManagerAction action) { EntityManager em = someHowCreateEntityManager(); EntityTransaction tx = null; try { action.execute(em); tx = em.getTransaction(); } catch (RuntimeException e) { if (tx != null && tx.isActive()) { tx.rollback(); } throw e; } finally { em.close(); } } }
private void commit() { try { if (entityManager.getTransaction().isActive()) { entityManager.getTransaction().commit(); } } catch (RollbackException e) { if (entityManager.getTransaction().isActive()) { // this should usually not be the case. // commit() will rollback itself before throwing the RollbackException! entityManager.getTransaction().rollback(); } throw e; } }
public <E> E transactional(Callable<E> action, boolean readOnly) { ensureNotInRollbackOnlyTransation(); EntityTransaction tx = em.getTransaction(); U.notNull(tx, "transaction"); boolean newTx = !tx.isActive(); if (newTx) { tx.begin(); } if (readOnly) { tx.setRollbackOnly(); } try { E result = action.call(); if (newTx) { if (tx.getRollbackOnly()) { tx.rollback(); } else { tx.commit(); } } return result; } catch (Throwable e) { if (newTx) { if (tx.isActive()) { tx.rollback(); } } throw U.rte("Transaction execution error, rolled back!", e); } }
@Test @TestForIssue(jiraKey = "HHH-12138") public void testNamedNativeQueryStoredProcedureRefCursor() { EntityManager entityManager = createEntityManager(); entityManager.getTransaction().begin(); try { List<Object[]> postAndComments = entityManager .createNamedQuery( "fn_person_and_phones_hana" ) .setParameter( 1, 1L ) .getResultList(); Object[] postAndComment = postAndComments.get( 0 ); Person person = (Person) postAndComment[0]; Phone phone = (Phone) postAndComment[1]; assertEquals( 2, postAndComments.size() ); } finally { entityManager.getTransaction().rollback(); entityManager.close(); } }
@Override public void afterCompletion(int status) { try { super.afterCompletion(status); if (status != STATUS_COMMITTED) { // Haven't had an afterCommit call: trigger a rollback. try { this.entityManager.getTransaction().rollback(); } catch (RuntimeException ex) { throw convertException(ex); } } } finally { if (this.closeOnCompletion) { EntityManagerFactoryUtils.closeEntityManager(this.entityManager); } } }
@Override public void ended(Coordination coordination) throws Exception { if (em.getTransaction().getRollbackOnly()) { try { em.getTransaction().rollback(); } catch (Exception e) { LOG.debug("Exception on transaction rollback", e); } } else { em.getTransaction().commit(); } } }
try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); em.persist(person); tx.commit(); tx.rollback(); throw e; } finally {
@Test @TestForIssue(jiraKey = "HHH-12138") public void testNamedNativeQueryStoredProcedureRefCursorWithJDBC() { EntityManager entityManager = createEntityManager(); entityManager.getTransaction().begin(); try { Session session = entityManager.unwrap( Session.class ); session.doWork( connection -> { try ( PreparedStatement function = connection.prepareStatement( "select * from fn_person_and_phones( ? )" ) ) { function.setInt( 1, 1 ); function.execute(); try ( ResultSet resultSet = function.getResultSet() ) { while ( resultSet.next() ) { Long postCommentId = resultSet.getLong( 1 ); String review = resultSet.getString( 2 ); } } } } ); } finally { entityManager.getTransaction().rollback(); entityManager.close(); } }
/** * 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); } }
public void closeTxn(boolean rollback) { if (isTxnOpen()) { if (rollback || readOnly || entityManager.getTransaction().getRollbackOnly()) { entityManager.getTransaction().rollback(); } else { entityManager.getTransaction().commit(); } } }