svn commit: r1620280 - in /ofbiz/branches/framework-api-cleanup/framework: common/src/org/ofbiz/common/CommonServices.java entity/src/org/ofbiz/entity/transaction/TransactionUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1620280 - in /ofbiz/branches/framework-api-cleanup/framework: common/src/org/ofbiz/common/CommonServices.java entity/src/org/ofbiz/entity/transaction/TransactionUtil.java

jacopoc
Author: jacopoc
Date: Mon Aug 25 09:07:10 2014
New Revision: 1620280

URL: http://svn.apache.org/r1620280
Log:
Two small optimizations to TransactionUtil:
* since the information stored in allThreadsTransactionBeginStack and allThreadsTransactionBeginStackSave was only used when the log INFO level was ON, now they are not populated if it is OFF
* instead of reading the value of debugResources from EntityConfig everytime, now it does only at initialization

Modified:
    ofbiz/branches/framework-api-cleanup/framework/common/src/org/ofbiz/common/CommonServices.java
    ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/transaction/TransactionUtil.java

Modified: ofbiz/branches/framework-api-cleanup/framework/common/src/org/ofbiz/common/CommonServices.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/framework-api-cleanup/framework/common/src/org/ofbiz/common/CommonServices.java?rev=1620280&r1=1620279&r2=1620280&view=diff
==============================================================================
--- ofbiz/branches/framework-api-cleanup/framework/common/src/org/ofbiz/common/CommonServices.java (original)
+++ ofbiz/branches/framework-api-cleanup/framework/common/src/org/ofbiz/common/CommonServices.java Mon Aug 25 09:07:10 2014
@@ -344,18 +344,14 @@ public class CommonServices {
     }
 
     public static Map<String, Object> displayXaDebugInfo(DispatchContext dctx, Map<String, ?> context) {
-        try {
-            if (TransactionUtil.debugResources()) {
-                if (UtilValidate.isNotEmpty(TransactionUtil.debugResMap)) {
-                    TransactionUtil.logRunningTx();
-                } else {
-                    Debug.logInfo("No running transaction to display.", module);
-                }
+        if (TransactionUtil.debugResources()) {
+            if (UtilValidate.isNotEmpty(TransactionUtil.debugResMap)) {
+                TransactionUtil.logRunningTx();
             } else {
-                Debug.logInfo("Debug resources is disabled.", module);
+                Debug.logInfo("No running transaction to display.", module);
             }
-        } catch (GenericEntityConfException e) {
-            return ServiceUtil.returnError(e.getMessage());
+        } else {
+            Debug.logInfo("Debug resources is disabled.", module);
         }
 
         return ServiceUtil.returnSuccess();

Modified: ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/transaction/TransactionUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/transaction/TransactionUtil.java?rev=1620280&r1=1620279&r2=1620280&view=diff
==============================================================================
--- ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/transaction/TransactionUtil.java (original)
+++ ofbiz/branches/framework-api-cleanup/framework/entity/src/org/ofbiz/entity/transaction/TransactionUtil.java Mon Aug 25 09:07:10 2014
@@ -63,19 +63,22 @@ import org.ofbiz.entity.jdbc.CursorConne
 public class TransactionUtil implements Status {
     // Debug module name
     public static final String module = TransactionUtil.class.getName();
-    public static Map<Xid, DebugXaResource> debugResMap = Collections.<Xid, DebugXaResource>synchronizedMap(new HashMap<Xid, DebugXaResource>());
 
     private static ThreadLocal<List<Transaction>> suspendedTxStack = new ThreadLocal<List<Transaction>>();
     private static ThreadLocal<List<Exception>> suspendedTxLocationStack = new ThreadLocal<List<Exception>>();
     private static ThreadLocal<Exception> transactionBeginStack = new ThreadLocal<Exception>();
     private static ThreadLocal<List<Exception>> transactionBeginStackSave = new ThreadLocal<List<Exception>>();
-    private static Map<Long, Exception> allThreadsTransactionBeginStack = Collections.<Long, Exception>synchronizedMap(new HashMap<Long, Exception>());
-    private static Map<Long, List<Exception>> allThreadsTransactionBeginStackSave = Collections.<Long, List<Exception>>synchronizedMap(new HashMap<Long, List<Exception>>());
     private static ThreadLocal<RollbackOnlyCause> setRollbackOnlyCause = new ThreadLocal<RollbackOnlyCause>();
     private static ThreadLocal<List<RollbackOnlyCause>> setRollbackOnlyCauseSave = new ThreadLocal<List<RollbackOnlyCause>>();
     private static ThreadLocal<Timestamp> transactionStartStamp = new ThreadLocal<Timestamp>();
     private static ThreadLocal<Timestamp> transactionLastNowStamp = new ThreadLocal<Timestamp>();
 
+    private static final boolean debugResources = readDebugResources();
+    public static Map<Xid, DebugXaResource> debugResMap = Collections.<Xid, DebugXaResource>synchronizedMap(new HashMap<Xid, DebugXaResource>());
+    // in order to improve performance allThreadsTransactionBeginStack and allThreadsTransactionBeginStackSave are only maintained when logging level INFO is on
+    private static Map<Long, Exception> allThreadsTransactionBeginStack = Collections.<Long, Exception>synchronizedMap(new HashMap<Long, Exception>());
+    private static Map<Long, List<Exception>> allThreadsTransactionBeginStackSave = Collections.<Long, List<Exception>>synchronizedMap(new HashMap<Long, List<Exception>>());
+
     @Deprecated
     public static <V> V doNewTransaction(String ifErrorMessage, Callable<V> callable) throws GenericEntityException {
         return noTransaction(inTransaction(callable, ifErrorMessage, 0, true)).call();
@@ -188,8 +191,6 @@ public class TransactionUtil implements
             } catch (SystemException e) {
                 //This is Java 1.4 only, but useful for certain debuggins: Throwable t = e.getCause() == null ? e : e.getCause();
                 throw new GenericTransactionException("System error, could not begin transaction", e);
-            } catch (GenericEntityConfException e) {
-                throw new GenericTransactionException("Configuration error, could not begin transaction", e);
             }
         } else {
             if (Debug.infoOn()) Debug.logInfo("[TransactionUtil.begin] no user transaction, so no transaction begun", module);
@@ -541,21 +542,26 @@ public class TransactionUtil implements
         }
     }
 
-    public static boolean debugResources() throws GenericEntityConfException {
-        return EntityConfig.getInstance().getDebugXaResources().getValue();
+    private static boolean readDebugResources() {
+        try {
+            return EntityConfig.getInstance().getDebugXaResources().getValue();
+        } catch (GenericEntityConfException gece) {
+            Debug.logWarning(gece, module);
+        }
+        return false;
+    }
+
+    public static boolean debugResources() {
+        return debugResources;
     }
 
     public static void logRunningTx() {
-        try {
-            if (debugResources()) {
-                if (UtilValidate.isNotEmpty(debugResMap)) {
-                    for (DebugXaResource dxa: debugResMap.values()) {
-                        dxa.log();
-                    }
+        if (debugResources()) {
+            if (UtilValidate.isNotEmpty(debugResMap)) {
+                for (DebugXaResource dxa: debugResMap.values()) {
+                    dxa.log();
                 }
             }
-        } catch (GenericEntityConfException e) {
-            Debug.logWarning("Exception thrown while logging: " + e, module);
         }
     }
 
@@ -669,23 +675,26 @@ public class TransactionUtil implements
         }
         el.add(0, e);
 
-        Long curThreadId = Thread.currentThread().getId();
-        List<Exception> ctEl = allThreadsTransactionBeginStackSave.get(curThreadId);
-        if (ctEl == null) {
-            ctEl = new LinkedList<Exception>();
-            allThreadsTransactionBeginStackSave.put(curThreadId, ctEl);
+        if (Debug.infoOn()) {
+            Long curThreadId = Thread.currentThread().getId();
+            List<Exception> ctEl = allThreadsTransactionBeginStackSave.get(curThreadId);
+            if (ctEl == null) {
+                ctEl = new LinkedList<Exception>();
+                allThreadsTransactionBeginStackSave.put(curThreadId, ctEl);
+            }
+            ctEl.add(0, e);
         }
-        ctEl.add(0, e);
     }
 
     private static Exception popTransactionBeginStackSave() {
-        // do the unofficial all threads Map one first, and don't do a real return
-        Long curThreadId = Thread.currentThread().getId();
-        List<Exception> ctEl = allThreadsTransactionBeginStackSave.get(curThreadId);
-        if (UtilValidate.isNotEmpty(ctEl)) {
-            ctEl.remove(0);
+        if (Debug.infoOn()) {
+            // do the unofficial all threads Map one first, and don't do a real return
+            Long curThreadId = Thread.currentThread().getId();
+            List<Exception> ctEl = allThreadsTransactionBeginStackSave.get(curThreadId);
+            if (UtilValidate.isNotEmpty(ctEl)) {
+                ctEl.remove(0);
+            }
         }
-
         // then do the more reliable ThreadLocal one
         List<Exception> el = transactionBeginStackSave.get();
         if (UtilValidate.isNotEmpty(el)) {
@@ -755,14 +764,17 @@ public class TransactionUtil implements
             Debug.logWarning(e2, "In setTransactionBeginStack a stack placeholder was already in place, here is the current location: ", module);
         }
         transactionBeginStack.set(newExc);
-        Long curThreadId = Thread.currentThread().getId();
-        allThreadsTransactionBeginStack.put(curThreadId, newExc);
+        if (Debug.infoOn()) {
+            Long curThreadId = Thread.currentThread().getId();
+            allThreadsTransactionBeginStack.put(curThreadId, newExc);
+        }
     }
 
     private static Exception clearTransactionBeginStack() {
-        Long curThreadId = Thread.currentThread().getId();
-        allThreadsTransactionBeginStack.remove(curThreadId);
-
+        if (Debug.infoOn()) {
+            Long curThreadId = Thread.currentThread().getId();
+            allThreadsTransactionBeginStack.remove(curThreadId);
+        }
         Exception e = transactionBeginStack.get();
         if (e == null) {
             Exception e2 = new Exception("Current Stack Trace");