javax.persistence.EntityTransaction#setRollbackOnly ( )源码实例Demo

下面列出了javax.persistence.EntityTransaction#setRollbackOnly ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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();
      }
   }
}
 
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();
      }
   }
}
 
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();
      }
   }
}
 
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();
      }
   }
}
 
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();
      }
   }
}
 
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();
      }
   }
}
 
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();
      }
   }
}
 
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();
      }
   }
}
 
public void setRollbackOnly() {
	EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
public void setRollbackOnly() {
	EntityTransaction tx = getEntityManagerHolder().getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
源代码11 项目: lams   文件: JpaTransactionManager.java
public void setRollbackOnly() {
	EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
public void setRollbackOnly() {
	EntityTransaction tx = this.entityManagerHolder.getEntityManager().getTransaction();
	if (tx.isActive()) {
		tx.setRollbackOnly();
	}
	if (hasConnectionHolder()) {
		getConnectionHolder().setRollbackOnly();
	}
}
 
源代码13 项目: rapidoid   文件: JPATool.java
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);
	}
}
 
源代码14 项目: testfun   文件: TransactionUtils.java
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();
    }
}