You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by mp...@apache.org on 2014/04/04 17:10:48 UTC

[2/2] git commit: AMBARI-5319. Investigate how we can log all exceptions rather than getting log that asks to call getNextException. (mpapyrkovskyy)

AMBARI-5319. Investigate how we can log all exceptions rather than getting log that asks to call getNextException. (mpapyrkovskyy)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/ff43fe25
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/ff43fe25
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/ff43fe25

Branch: refs/heads/trunk
Commit: ff43fe25a0a27f502a02076200303b0a14bb0f17
Parents: f96d22e
Author: Myroslav Papirkovskyy <mp...@hortonworks.com>
Authored: Thu Apr 3 17:35:25 2014 +0300
Committer: Myroslav Papirkovskyy <mp...@hortonworks.com>
Committed: Fri Apr 4 18:10:44 2014 +0300

----------------------------------------------------------------------
 .../orm/AmbariJpaLocalTxnInterceptor.java       | 54 ++++++++++++++++----
 1 file changed, 45 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/ff43fe25/ambari-server/src/main/java/org/apache/ambari/server/orm/AmbariJpaLocalTxnInterceptor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/AmbariJpaLocalTxnInterceptor.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/AmbariJpaLocalTxnInterceptor.java
index 98ca484..6d7901c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/AmbariJpaLocalTxnInterceptor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/AmbariJpaLocalTxnInterceptor.java
@@ -24,22 +24,23 @@ import com.google.inject.persist.UnitOfWork;
 import com.google.inject.persist.jpa.AmbariJpaPersistService;
 import org.aopalliance.intercept.MethodInterceptor;
 import org.aopalliance.intercept.MethodInvocation;
+import org.eclipse.persistence.exceptions.EclipseLinkException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
+import javax.persistence.PersistenceException;
 import java.lang.reflect.Method;
+import java.sql.SQLException;
 
 public class AmbariJpaLocalTxnInterceptor implements MethodInterceptor {
 
+  private static final Logger LOG = LoggerFactory.getLogger(AmbariJpaLocalTxnInterceptor.class);
   @Inject
   private final AmbariJpaPersistService emProvider = null;
-
   @Inject
   private final UnitOfWork unitOfWork = null;
-
-  @Transactional
-  private static class Internal {}
-
   // Tracks if the unit of work was begun implicitly by this transaction.
   private final ThreadLocal<Boolean> didWeStartWork = new ThreadLocal<Boolean>();
 
@@ -67,11 +68,13 @@ public class AmbariJpaLocalTxnInterceptor implements MethodInterceptor {
       result = methodInvocation.proceed();
 
     } catch (Exception e) {
-      //commit transaction only if rollback didnt occur
+      //commit transaction only if rollback didn't occur
       if (rollbackIfNecessary(transactional, e, txn)) {
         txn.commit();
       }
 
+      detailedLogForPersistenceError(e);
+
       //propagate whatever exception is thrown anyway
       throw e;
     } finally {
@@ -86,9 +89,12 @@ public class AmbariJpaLocalTxnInterceptor implements MethodInterceptor {
     //  interferes with the advised method's throwing semantics)
     try {
       txn.commit();
+    } catch (Exception e) {
+      detailedLogForPersistenceError(e);
+      throw e;
     } finally {
       //close the em if necessary
-      if (null != didWeStartWork.get() ) {
+      if (null != didWeStartWork.get()) {
         didWeStartWork.remove();
         unitOfWork.end();
       }
@@ -98,6 +104,32 @@ public class AmbariJpaLocalTxnInterceptor implements MethodInterceptor {
     return result;
   }
 
+  private void detailedLogForPersistenceError(Exception e) {
+    if (e instanceof PersistenceException) {
+      PersistenceException rbe = (PersistenceException) e;
+      Throwable cause = rbe.getCause();
+
+      if (cause != null && cause instanceof EclipseLinkException) {
+        EclipseLinkException de = (EclipseLinkException) cause;
+        LOG.error("[DETAILED ERROR] Rollback reason: ", cause);
+        Throwable internal = de.getInternalException();
+
+        int exIndent = 1;
+        if (internal != null && internal instanceof SQLException) {
+          SQLException exception = (SQLException) internal;
+
+          while (exception != null) {
+            LOG.error("[DETAILED ERROR] Internal exception ("
+                + exIndent
+                + ") : ", exception); // Log the exception
+            exception = exception.getNextException();
+            exIndent++;
+          }
+        }
+      }
+    }
+  }
+
   // TODO Cache this method's results.
   private Transactional readTransactionMetadata(MethodInvocation methodInvocation) {
     Transactional transactional;
@@ -121,8 +153,8 @@ public class AmbariJpaLocalTxnInterceptor implements MethodInterceptor {
    * Returns True if rollback DID NOT HAPPEN (i.e. if commit should continue).
    *
    * @param transactional The metadata annotaiton of the method
-   * @param e The exception to test for rollback
-   * @param txn A JPA Transaction to issue rollbacks on
+   * @param e             The exception to test for rollback
+   * @param txn           A JPA Transaction to issue rollbacks on
    */
   private boolean rollbackIfNecessary(Transactional transactional, Exception e,
                                       EntityTransaction txn) {
@@ -157,4 +189,8 @@ public class AmbariJpaLocalTxnInterceptor implements MethodInterceptor {
 
     return commit;
   }
+
+  @Transactional
+  private static class Internal {
+  }
 }