Refine search
protected boolean isTransactionActive() { if (handleTransactions && entityManager.getTransaction() != null) { return entityManager.getTransaction().isActive(); } return false; }
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(); } } }
/** * 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); } }
!target.getTransaction().isActive())) { throw new TransactionRequiredException("No EntityManager with actual transaction available " + "for current thread - cannot reliably process '" + method.getName() + "' call");
private void ensureNotInRollbackOnlyTransation() { EntityTransaction tx = em.getTransaction(); U.must(!tx.isActive() || !tx.getRollbackOnly(), "Cannot perform writes inside read-only transaction!"); }
if (TransactionSynchronizationManager.isSynchronizationActive()) { if (!TransactionSynchronizationManager.hasResource(this.target) && !this.target.getTransaction().isActive()) { enlistInCurrentTransaction();
public void setRollbackOnly() { EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
factorySupplier.get().createEntityManager(properties); function.beforeTransactionCompletion(); txn = entityManager.getTransaction(); txn.begin(); result = function.apply( entityManager ); if ( txn != null && txn.isActive() ) { txn.rollback();
factorySupplier.get().createEntityManager(properties); function.beforeTransactionCompletion(); txn = entityManager.getTransaction(); txn.begin(); function.accept( entityManager ); if ( txn != null && txn.isActive() ) { try { txn.rollback();
@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(); } } }
/** * 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); } }
@Test public void testMarkRollbackOnlyAnActiveTransaction() { EntityTransaction transaction = entityManager.getTransaction(); final TransactionImplementor hibernateTransaction = (TransactionImplementor) transaction; transaction.begin(); hibernateTransaction.markRollbackOnly(); transaction.rollback(); assertFalse( transaction.isActive() ); } }
public void setRollbackOnly() { EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction(); if (tx.isActive()) { tx.setRollbackOnly(); } if (hasConnectionHolder()) { getConnectionHolder().setRollbackOnly(); } }
@Test public void testMarkRollbackOnlyAnUnactiveTransaction() { EntityTransaction transaction = entityManager.getTransaction(); final TransactionImplementor hibernateTransaction = (TransactionImplementor) transaction; hibernateTransaction.markRollbackOnly(); transaction.rollback(); assertFalse( transaction.isActive() ); }
private void assertQueryRowCount(String queryString, int rowCount) { EntityManager entityManager = getOrCreateEntityManager(); try { // persist the data entityManager.getTransaction().begin(); entityManager.persist( new Simple( new SimpleId( 1, 1 ), 1 ) ); entityManager.getTransaction().commit(); Query query = entityManager.createQuery( queryString ); assertEquals( rowCount, query.getResultList().size() ); } catch ( Exception e ) { if ( entityManager.getTransaction().isActive() ) { entityManager.getTransaction().rollback(); } throw e; } finally { entityManager.close(); } }
private void withBatch() { int entityCount = 100; //tag::batch-session-batch-insert-example[] EntityManager entityManager = null; EntityTransaction txn = null; try { entityManager = entityManagerFactory().createEntityManager(); txn = entityManager.getTransaction(); txn.begin(); int batchSize = 25; for ( int i = 0; i < entityCount; i++ ) { if ( i > 0 && i % batchSize == 0 ) { //flush a batch of inserts and release memory entityManager.flush(); entityManager.clear(); } Person Person = new Person( String.format( "Person %d", i ) ); entityManager.persist( Person ); } txn.commit(); } catch (RuntimeException e) { if ( txn != null && txn.isActive()) txn.rollback(); throw e; } finally { if (entityManager != null) { entityManager.close(); } } //end::batch-session-batch-insert-example[] }
@Test // tests that Author has 3 books. public void testAuthorState() { EntityManager entityManager = getEntityManager(); try { final Author author = entityManager.find( Author.class, authorId ); assertNotNull( author ); assertEquals( 3, author.getBooks().size() ); } catch ( Exception e ) { if ( entityManager.getTransaction().isActive() ) { entityManager.getTransaction().rollback(); } } finally { entityManager.close(); } }
@Test public void testFlushAutoCommit() { EntityManager entityManager = null; EntityTransaction txn = null; try { //tag::flushing-auto-flush-commit-example[] entityManager = entityManagerFactory().createEntityManager(); txn = entityManager.getTransaction(); txn.begin(); Person person = new Person( "John Doe" ); entityManager.persist( person ); log.info( "Entity is in persisted state" ); txn.commit(); //end::flushing-auto-flush-commit-example[] } catch (RuntimeException e) { if ( txn != null && txn.isActive()) txn.rollback(); throw e; } finally { if (entityManager != null) { entityManager.close(); } } }
@Test @Priority(10) public void initData() { EntityManager entityManager = getEntityManager(); try { ChildEntity childEntity = new ChildEntity( 1, "Child" ); entityManager.getTransaction().begin(); entityManager.persist( childEntity ); entityManager.getTransaction().commit(); ChildListHolder holder = new ChildListHolder(); holder.setId( 1 ); holder.setChildren( Arrays.asList( childEntity ) ); entityManager.getTransaction().begin(); entityManager.persist( holder ); entityManager.getTransaction().commit(); } catch ( Exception e ) { if ( entityManager.getTransaction().isActive() ) { entityManager.getTransaction().rollback(); } throw e; } finally { entityManager.close(); } }
@Test // tests that Author has 3 books. public void testAuthorState() { EntityManager entityManager = getEntityManager(); try { final Author author = entityManager.find( Author.class, authorId ); assertNotNull( author ); assertEquals( 3, author.getBooks().size() ); } catch ( Exception e ) { if ( entityManager.getTransaction().isActive() ) { entityManager.getTransaction().rollback(); } } finally { entityManager.close(); } }