Java Code Examples for javax.persistence.EntityTransaction#setRollbackOnly()

The following examples show how to use javax.persistence.EntityTransaction#setRollbackOnly() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InfinispanHibernateCacheLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private static void persistEntities() {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         em.persist(new Event("Caught a pokemon!"));
         em.persist(new Event("Hatched an egg"));
         em.persist(new Event("Became a gym leader"));
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 2
Source File: InfinispanHibernateCacheLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private static void updateEntity(long id) {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         Event event = em.find(Event.class, id);
         event.setName("Caught a Snorlax!!");

         System.out.printf("Updated entity: %s%n", event);
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 3
Source File: InfinispanHibernateCacheLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private static void deleteEntity(long id) {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         Event event = em.find(Event.class, id);
         em.remove(event);
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 4
Source File: InfinispanHibernateCacheLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private static void saveExpiringEntity() {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         em.persist(new Person("Satoshi"));
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 5
Source File: InfinispanHibernateCacheSpringLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private void persistEntities() {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         em.persist(new Event("Caught a pokemon!"));
         em.persist(new Event("Hatched an egg"));
         em.persist(new Event("Became a gym leader"));
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 6
Source File: InfinispanHibernateCacheSpringLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private void updateEntity(long id) {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         Event event = em.find(Event.class, id);
         event.setName("Caught a Snorlax!!");

         log.info(String.format("Updated entity: %s", event));
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 7
Source File: InfinispanHibernateCacheSpringLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private void deleteEntity(long id) {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         Event event = em.find(Event.class, id);
         em.remove(event);
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 8
Source File: InfinispanHibernateCacheSpringLocal.java    From infinispan-simple-tutorials with Apache License 2.0 6 votes vote down vote up
private void saveExpiringEntity() {
   try (Session em = createEntityManagerWithStatsCleared()) {
      EntityTransaction tx = em.getTransaction();
      try {
         tx.begin();

         em.persist(new Person("Satoshi"));
      } catch (Throwable t) {
         tx.setRollbackOnly();
         throw t;
      } finally {
         if (tx.isActive()) tx.commit();
         else tx.rollback();
      }
   }
}
 
Example 9
Source File: JpaTransactionManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public void setRollbackOnly() {
	EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
Example 10
Source File: JpaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
public void setRollbackOnly() {
	EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
Example 11
Source File: JpaTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void setRollbackOnly() {
	EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
Example 12
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public void setRollbackOnly() {
	EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
Example 13
Source File: JPATool.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 14
Source File: TransactionUtils.java    From testfun with Apache License 2.0 5 votes vote down vote up
public static void rollbackTransaction() {
    EntityTransaction tx = getTransaction();
    if (tx.isActive() && !tx.getRollbackOnly()) {
        // only flag the transaction for rollback - actual rollback will happen when the method starting the transaction is done
        tx.setRollbackOnly();
    }
}