📄 transactionutil.java
字号:
throw new GenericTransactionException("System error, could not register synchronization in transaction even though transactions are available", e); } } // ======================================= // ======================================= private static ThreadLocal suspendedTxStack = new ThreadLocal(); /** BE VERY CARFUL WHERE YOU CALL THIS!! */ public static int cleanSuspendedTransactions() throws GenericTransactionException { Transaction trans = null; int num = 0; while ((trans = popSuspendedTransaction()) != null) { resume(trans); rollback(); num++; } return num; } public static boolean suspendedTransactionsHeld() { List tl = (List) suspendedTxStack.get(); if (tl != null && tl.size() > 0) { return true; } else { return false; } } protected static void pushSuspendedTransaction(Transaction t) { List tl = (List) suspendedTxStack.get(); if (tl == null) { tl = new LinkedList(); suspendedTxStack.set(tl); } tl.add(0, t); } protected static Transaction popSuspendedTransaction() { List tl = (List) suspendedTxStack.get(); if (tl != null && tl.size() > 0) { return (Transaction) tl.remove(0); } else { return null; } } protected static void removeSuspendedTransaction(Transaction t) { List tl = (List) suspendedTxStack.get(); if (tl != null && tl.size() > 0) { tl.remove(t); } } // ======================================= // ======================================= private static ThreadLocal transactionBeginStack = new ThreadLocal(); private static ThreadLocal transactionBeginStackSave = new ThreadLocal(); private static void pushTransactionBeginStackSave(Exception e) { List el = (List) transactionBeginStackSave.get(); if (el == null) { el = new LinkedList(); transactionBeginStackSave.set(el); } el.add(0, e); } private static Exception popTransactionBeginStackSave() { List el = (List) transactionBeginStackSave.get(); if (el != null && el.size() > 0) { return (Exception) el.remove(0); } else { return null; } } private static void setTransactionBeginStack() { Exception e = new Exception("Tx Stack Placeholder"); setTransactionBeginStack(e); } private static void setTransactionBeginStack(Exception newExc) { if (transactionBeginStack.get() != null) { Exception e = (Exception) transactionBeginStack.get(); Debug.logWarning(e, "WARNING: In setTransactionBeginStack a stack placeholder was already in place, here is where the transaction began: ", module); Exception e2 = new Exception("Current Stack Trace"); Debug.logWarning(e2, "WARNING: In setTransactionBeginStack a stack placeholder was already in place, here is the current location: ", module); } transactionBeginStack.set(newExc); } private static Exception clearTransactionBeginStack() { Exception e = (Exception) transactionBeginStack.get(); if (e == null) { Exception e2 = new Exception("Current Stack Trace"); Debug.logWarning("WARNING: In clearTransactionBeginStack no stack placeholder was in place, here is the current location: ", module); return null; } else { transactionBeginStack.set(null); return e; } } public static Exception getTransactionBeginStack() { if (transactionBeginStack.get() == null) { Exception e2 = new Exception("Current Stack Trace"); Debug.logWarning("WARNING: In getTransactionBeginStack no stack placeholder was in place, here is the current location: ", module); } return (Exception) transactionBeginStack.get(); } // ======================================= // ======================================= private static class RollbackOnlyCause { protected String causeMessage; protected Throwable causeThrowable; public RollbackOnlyCause(String causeMessage, Throwable causeThrowable) { this.causeMessage = causeMessage; this.causeThrowable = causeThrowable; } public String getCauseMessage() { return this.causeMessage + (this.causeThrowable == null ? "" : this.causeThrowable.toString()); } public Throwable getCauseThrowable() { return this.causeThrowable; } public void logError(String message) { Debug.logError(this.getCauseThrowable(), (message == null ? "" : message) + this.getCauseMessage(), module); } public boolean isEmpty() { return (UtilValidate.isEmpty(this.getCauseMessage()) && this.getCauseThrowable() == null); } } private static ThreadLocal setRollbackOnlyCause = new ThreadLocal(); private static ThreadLocal setRollbackOnlyCauseSave = new ThreadLocal(); private static void pushSetRollbackOnlyCauseSave(RollbackOnlyCause e) { List el = (List) setRollbackOnlyCauseSave.get(); if (el == null) { el = new LinkedList(); setRollbackOnlyCauseSave.set(el); } el.add(0, e); } private static RollbackOnlyCause popSetRollbackOnlyCauseSave() { List el = (List) setRollbackOnlyCauseSave.get(); if (el != null && el.size() > 0) { return (RollbackOnlyCause) el.remove(0); } else { return null; } } private static void setSetRollbackOnlyCause(String causeMessage, Throwable causeThrowable) { RollbackOnlyCause roc = new RollbackOnlyCause(causeMessage, causeThrowable); setSetRollbackOnlyCause(roc); } private static void setSetRollbackOnlyCause(RollbackOnlyCause newRoc) { if (setRollbackOnlyCause.get() != null) { RollbackOnlyCause roc = (RollbackOnlyCause) setRollbackOnlyCause.get(); roc.logError("WARNING: In setSetRollbackOnlyCause a stack placeholder was already in place, here is the original rollbackOnly cause: "); Exception e2 = new Exception("Current Stack Trace"); Debug.logWarning(e2, "WARNING: In setSetRollbackOnlyCause a stack placeholder was already in place, here is the current location: ", module); } setRollbackOnlyCause.set(newRoc); } private static RollbackOnlyCause clearSetRollbackOnlyCause() { RollbackOnlyCause roc = (RollbackOnlyCause) setRollbackOnlyCause.get(); if (roc == null) { /* this is an obnoxious message, leaving out for now; could be added manually if a problem with this is suspected if (Debug.verboseOn()) { // for this in particular, unlike the begin location, normally there will not be a setRollbackOnlyCause, so don't complain about it except in verbose Debug.logVerbose(new Exception("Current Stack Trace"), "In clearSetRollbackOnlyCause no stack placeholder was in place, here is the current location: ", module); } */ return null; } else { setRollbackOnlyCause.set(null); return roc; } } public static RollbackOnlyCause getSetRollbackOnlyCause() { if (setRollbackOnlyCause.get() == null) { Exception e2 = new Exception("Current Stack Trace"); Debug.logWarning("WARNING: In getSetRollbackOnlyCause no stack placeholder was in place, here is the current location: ", module); } return (RollbackOnlyCause) setRollbackOnlyCause.get(); } // ======================================= // ======================================= private static ThreadLocal transactionStartStamp = new ThreadLocal(); private static ThreadLocal transactionLastNowStamp = new ThreadLocal(); public static Timestamp getTransactionStartStamp() { Timestamp curStamp = (Timestamp) transactionStartStamp.get(); if (curStamp == null) { curStamp = UtilDateTime.nowTimestamp(); transactionStartStamp.set(curStamp); // we know this is the first time set for this transaction, so make sure the StampClearSync is registered try { registerSynchronization(new StampClearSync()); } catch (GenericTransactionException e) { Debug.logError(e, "Error registering StampClearSync synchronization, stamps will still be reset if begin/commit/rollback are call through TransactionUtil, but not if otherwise", module); } } return curStamp; } public static Timestamp getTransactionUniqueNowStamp() { Timestamp lastNowStamp = (Timestamp) transactionLastNowStamp.get(); Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); // check for an overlap with the lastNowStamp, or if the lastNowStamp is in the future because of incrementing to make each stamp unique if (lastNowStamp != null && (lastNowStamp.equals(nowTimestamp) || lastNowStamp.after(nowTimestamp))) { nowTimestamp = new Timestamp(lastNowStamp.getTime() + 1); } transactionLastNowStamp.set(nowTimestamp); return nowTimestamp; } protected static void clearTransactionStamps() { transactionStartStamp.set(null); transactionLastNowStamp.set(null); } public static class StampClearSync implements Synchronization { public void afterCompletion(int status) { TransactionUtil.clearTransactionStamps(); } public void beforeCompletion() { } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -