示例#1
0
 /**
  * clear current JPA context and transaction
  *
  * @param rollback shall current transaction be committed (false) or cancelled (true)
  */
 public static void closeTx(boolean rollback) {
   if (!JPA.isEnabled() || JPA.local.get() == null) {
     return;
   }
   EntityManager manager = JPA.get().entityManager;
   try {
     if (autoTxs) {
       if (manager.getTransaction().isActive()) {
         if (JPA.get().readonly || rollback || manager.getTransaction().getRollbackOnly()) {
           manager.getTransaction().rollback();
         } else {
           try {
             if (autoTxs) {
               manager.getTransaction().commit();
             }
           } catch (Throwable e) {
             for (int i = 0; i < 10; i++) {
               if (e instanceof PersistenceException && e.getCause() != null) {
                 e = e.getCause();
                 break;
               }
               e = e.getCause();
               if (e == null) {
                 break;
               }
             }
             throw new JPAException("Cannot commit", e);
           }
         }
       }
     }
   } finally {
     manager.close();
     JPA.clearContext();
   }
 }