db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lily...@apache.org
Subject svn commit: r1043290 - in /db/derby/code/trunk: java/engine/org/apache/derby/iapi/ java/engine/org/apache/derby/iapi/error/ java/engine/org/apache/derby/iapi/services/context/ java/engine/org/apache/derby/impl/jdbc/ tools/jar/
Date Wed, 08 Dec 2010 03:34:19 GMT
Author: lilywei
Date: Wed Dec  8 03:34:19 2010
New Revision: 1043290

URL: http://svn.apache.org/viewvc?rev=1043290&view=rev
Log:
DERBY-4856 Add thread dump information for error StandardException and SQLException. Due to
DERBY-289, ThreadDump.java and ExceptionUtil.java should go to iapi/error for engine. Currently,
all thread dump information goes to derby.log

Added:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ExceptionUtil.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ThreadDump.java
Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/build.xml
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/context/ContextManager.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBOutputStream.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBStreamControl.java
    db/derby/code/trunk/tools/jar/extraDBMSclasses.properties

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/build.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/build.xml?rev=1043290&r1=1043289&r2=1043290&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/build.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/build.xml Wed Dec  8 03:34:19 2010
@@ -60,6 +60,25 @@
         <pathelement path="${compile.classpath}"/>
       </classpath>
       <include name="${derby.dir}/iapi/error/**"/>
+      <exclude name="${derby.dir}/iapi/error/ThreadDump.java"/>
+    </javac>
+
+    <javac
+      source="1.5"
+      target="1.5"
+      bootclasspath="${empty}"
+      nowarn="on"
+      debug="${debug}"
+      depend="${depend}"
+      optimize="${optimize}"
+      proceed="${proceed}"
+      verbose="${verbose}"
+      srcdir="${derby.engine.src.dir}"
+      destdir="${out.dir}">
+      <classpath>
+         <pathelement path="${java15compile.classpath}"/>
+      </classpath>
+      <include name="${derby.dir}/iapi/error/ThreadDump.java"/>
     </javac>
     <ant dir="${derby.engine.dir}/iapi/types" />
     <ant dir="${derby.engine.dir}/iapi/util"/>

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ExceptionUtil.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ExceptionUtil.java?rev=1043290&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ExceptionUtil.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ExceptionUtil.java Wed Dec
 8 03:34:19 2010
@@ -0,0 +1,195 @@
+/*
+   Derby - Class org.apache.derby.iapi.error.ExceptionUtil
+ 
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+ 
+      http://www.apache.org/licenses/LICENSE-2.0
+ 
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+ 
+ */
+package org.apache.derby.iapi.error;
+/* Until DERBY-289 related issue settle for shared code
+ * Engine have similar code as client code even though some of 
+ * code is potentially sharable. If you fix a bug in ExceptionUtil for engine, 
+ * please also change the code in 
+ * java/shared/org/apache/derby/shared/common/error/ExceptionUtil.java for 
+ * client if necessary.
+ */
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.security.AccessControlException;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+import org.apache.derby.shared.common.error.ExceptionSeverity;
+
+/**
+ * This class provides utility routines for exceptions 
+ */
+public class ExceptionUtil
+{
+
+
+    /**
+     *  Convert a message identifier from 
+     *  org.apache.derby.shared.common.reference.SQLState to
+     *  a SQLState five character string.
+     *
+     *	@param messageID - the sql state id of the message from Derby
+     *	@return String 	 - the 5 character code of the SQLState ID to returned to the user

+     */
+    public static String getSQLStateFromIdentifier(String messageID) {
+
+        if (messageID.length() == 5)
+            return messageID;
+        return messageID.substring(0, 5);
+    }
+    
+    /**
+     * Get the severity given a message identifier from SQLState.
+     */
+    public static int getSeverityFromIdentifier(String messageID) {
+
+        int lseverity = ExceptionSeverity.NO_APPLICABLE_SEVERITY;
+
+        switch (messageID.length()) {
+        case 5:
+            switch (messageID.charAt(0)) {
+            case '0':
+                switch (messageID.charAt(1)) {
+                case '1':
+                    lseverity = ExceptionSeverity.WARNING_SEVERITY;
+                    break;
+                case 'A':
+                case '7':
+                    lseverity = ExceptionSeverity.STATEMENT_SEVERITY;
+                    break;
+                case '8':
+                    lseverity = ExceptionSeverity.SESSION_SEVERITY;
+                    break;
+                }
+                break;	
+            case '2':
+            case '3':
+                lseverity = ExceptionSeverity.STATEMENT_SEVERITY;
+                break;
+            case '4':
+                switch (messageID.charAt(1)) {
+                case '0':
+                    lseverity = ExceptionSeverity.TRANSACTION_SEVERITY;
+                    break;
+                case '2':
+                    lseverity = ExceptionSeverity.STATEMENT_SEVERITY;
+                    break;
+                }
+                break;	
+            }
+            break;
+
+        default:
+            switch (messageID.charAt(6)) {
+            case 'M':
+                lseverity = ExceptionSeverity.SYSTEM_SEVERITY;
+                break;
+            case 'D':
+                lseverity = ExceptionSeverity.DATABASE_SEVERITY;
+                break;
+            case 'C':
+                lseverity = ExceptionSeverity.SESSION_SEVERITY;
+                break;
+            case 'T':
+                lseverity = ExceptionSeverity.TRANSACTION_SEVERITY;
+                break;
+            case 'S':
+                lseverity = ExceptionSeverity.STATEMENT_SEVERITY;
+                break;
+            case 'U':
+                lseverity = ExceptionSeverity.NO_APPLICABLE_SEVERITY;
+                break;
+            }
+            break;
+        }
+
+        return lseverity;
+    }
+
+    /**
+     * Dumps stack traces for all the threads if the JVM supports it.
+     * The result is returned as a string, ready to print.
+     *
+     * If the JVM doesn't have the method Thread.getAllStackTraces
+     * i.e, we are on a JVM < 1.5, or  if we don't have the permissions:
+     * java.lang.RuntimePermission "getStackTrace" and "modifyThreadGroup",
+     * a message saying so is returned instead.
+     *
+     * @return stack traces for all live threads as a string or an error message.
+     */
+    public static String dumpThreads() {
+
+        StringWriter out = new StringWriter();
+        PrintWriter p = new PrintWriter(out, true);
+
+        //Try to get a thread dump and deal with various situations.
+        try {
+            //This checks that we are on a jvm >= 1.5 where we
+            //can actually do threaddumps.
+            Thread.class.getMethod("getAllStackTraces", new Class[] {});
+
+            //Then get the thread dump.
+            Class c = Class.forName("org.apache.derby.iapi.error.ThreadDump");
+            final Method m = c.getMethod("getStackDumpString",new Class[] {});
+
+            String dump;
+
+            dump = (String) AccessController.doPrivileged
+            (new PrivilegedExceptionAction(){
+                public Object run() throws
+                IllegalArgumentException,
+                IllegalAccessException,
+                InvocationTargetException{
+                    return m.invoke(null, null);
+                }
+            }
+            );
+
+            //Print the dump to the message string. That went OK.
+            p.print("---------------\nStack traces for all " +
+            "live threads:");
+            p.println("\n" + dump);
+            p.println("---------------");
+        } catch (NoSuchMethodException e) {
+            p.println("(Skipping thread dump because it is not " +
+            "supported on JVM 1.4)");
+
+        } catch (Exception e) {
+            if (e instanceof PrivilegedActionException &&
+                e.getCause() instanceof InvocationTargetException &&
+                e.getCause().getCause() instanceof AccessControlException){
+
+                p.println("(Skipping thread dump "
+                        + "because of insufficient permissions:\n"
+                        + e.getCause().getCause() + ")\n");
+            } else {
+                p.println("\nAssertFailure tried to do a thread dump, but "
+                        + "there was an error:");
+                e.getCause().printStackTrace(p);
+            }
+        }
+        return out.toString();
+    }
+
+}

Added: db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ThreadDump.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ThreadDump.java?rev=1043290&view=auto
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ThreadDump.java (added)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/error/ThreadDump.java Wed Dec  8
03:34:19 2010
@@ -0,0 +1,58 @@
+/*
+
+   Derby - Class org.apache.derby.iapi.error.ThreadDump
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+
+package org.apache.derby.iapi.error;
+
+/* Until DERBY-289 related issue settle for shared code
+ * Engine have similar code as client code even though some of 
+ * code is potentially sharable. If you fix a bug in ThreadDump for engine, 
+ * please also change the code in 
+ * java/shared/org/apache/derby/shared/common/sanity/ThreadDump.java for 
+ * client if necessary.
+ */
+
+import java.util.Map;
+
+public class ThreadDump {
+
+    /**
+     * 
+     * @return A string representation of a full thread dump
+     */
+    public static String getStackDumpString() {
+        StringBuffer sb = new StringBuffer();
+        Map<Thread, StackTraceElement[]> st = Thread.getAllStackTraces();
+        for (Map.Entry<Thread, StackTraceElement[]> e : st.entrySet()) {
+            StackTraceElement[] lines = e.getValue();
+            Thread t = e.getKey();
+            sb.append("Thread name=" + t.getName() + " id=" + t.getId()
+                    + " priority=" + t.getPriority() + " state=" + t.getState()
+                    + " isdaemon=" + t.isDaemon() + "\n");
+            for (int i = 0; i < lines.length; i++) {
+                sb.append("\t" + lines[i] + "\n");
+
+            }
+            sb.append("\n");
+        }
+        return sb.toString();
+    }
+
+}

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/context/ContextManager.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/context/ContextManager.java?rev=1043290&r1=1043289&r2=1043290&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/context/ContextManager.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/services/context/ContextManager.java
Wed Dec  8 03:34:19 2010
@@ -29,6 +29,7 @@ import org.apache.derby.iapi.error.PassT
 import org.apache.derby.iapi.error.ShutdownException;
 
 import org.apache.derby.iapi.error.StandardException;
+import org.apache.derby.iapi.error.ExceptionUtil;
 import org.apache.derby.iapi.services.monitor.Monitor;
 
 import org.apache.derby.iapi.reference.Property;
@@ -37,6 +38,7 @@ import org.apache.derby.iapi.services.pr
 import org.apache.derby.iapi.error.ExceptionSeverity;
 import org.apache.derby.iapi.services.i18n.LocaleFinder;
 
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.ArrayList;
 import java.util.List;
@@ -302,9 +304,7 @@ public class ContextManager
 
 forever: for (;;) {
 
-			int errorSeverity = error instanceof StandardException ?
-				((StandardException) error).getSeverity() :
-				ExceptionSeverity.NO_APPLICABLE_SEVERITY;
+            int errorSeverity = getErrorSeverity(error);
  			if (reportError) {
 				errorStringBuilder.stackTrace(error);
 				flushErrorString();
@@ -331,6 +331,12 @@ cleanup:	for (int index = holder.size() 
 					lastHandler = ctx.isLastHandler(errorSeverity);
 
 					ctx.cleanupOnError(error);
+                    if (reportError
+                            && errorSeverity >= ExceptionSeverity.SESSION_SEVERITY)
{
+                        threadDump = ExceptionUtil.dumpThreads();
+                    } else {
+                        threadDump = null;
+                    }
 				}
 				catch (StandardException se) {
 	
@@ -401,6 +407,8 @@ cleanup:	for (int index = holder.size() 
 				}
 			}
 
+            if (threadDump != null)
+                errorStream.println(threadDump);
 			if (reportError) {
 				errorStream.println("Cleanup action completed");
 				errorStream.flush();
@@ -504,6 +512,29 @@ cleanup:	for (int index = holder.size() 
 		return !(t instanceof ShutdownException);
 
 	}
+    
+    /**
+     * return the severity of the exception. Currently, this method 
+     * does not determine a severity that is not StandardException 
+     * or SQLException.
+     * @param error - Throwable error
+     * 
+     * @return int vendorcode/severity for the Throwable error
+     *            - error/exception to extract vendorcode/severity. 
+     *            For error that we can not get severity, 
+     *            NO_APPLICABLE_SEVERITY will return.
+     */
+    public int getErrorSeverity(Throwable error) {
+        
+        if (error instanceof StandardException) {
+            return ((StandardException) error).getErrorCode();
+        }
+        
+        if (error instanceof SQLException) {
+            return ((SQLException) error).getErrorCode();
+        }
+        return ExceptionSeverity.NO_APPLICABLE_SEVERITY;
+    }
 
 	/**
 	 * Constructs a new instance. No CtxStacks are inserted into the
@@ -526,6 +557,8 @@ cleanup:	for (int index = holder.size() 
 
 	private HeaderPrintWriter errorStream;
 	private ErrorStringBuilder errorStringBuilder;
+    // DERBY-4856 add thread dump information.
+    private String threadDump;
 
 	private boolean shutdown;
 	private LocaleFinder finder;

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java?rev=1043290&r1=1043289&r2=1043290&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBInputStream.java Wed Dec
 8 03:34:19 2010
@@ -30,7 +30,7 @@ import org.apache.derby.iapi.reference.S
 import org.apache.derby.iapi.services.i18n.MessageService;
 import org.apache.derby.iapi.services.sanity.SanityManager;
 import org.apache.derby.iapi.types.PositionedStream;
-import org.apache.derby.shared.common.error.ExceptionUtil;
+import org.apache.derby.iapi.error.ExceptionUtil;
 
 /**
  * This input stream is built on top of {@link LOBStreamControl}.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBOutputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBOutputStream.java?rev=1043290&r1=1043289&r2=1043290&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBOutputStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBOutputStream.java Wed Dec
 8 03:34:19 2010
@@ -27,7 +27,7 @@ import java.io.OutputStream;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.reference.SQLState;
 import org.apache.derby.iapi.services.i18n.MessageService;
-import org.apache.derby.shared.common.error.ExceptionUtil;
+import org.apache.derby.iapi.error.ExceptionUtil;
 
 /**
  * This is an output stream built on top of LOBStreamControl.

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBStreamControl.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBStreamControl.java?rev=1043290&r1=1043289&r2=1043290&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBStreamControl.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/LOBStreamControl.java Wed Dec
 8 03:34:19 2010
@@ -37,7 +37,7 @@ import org.apache.derby.iapi.services.i1
 import org.apache.derby.iapi.services.monitor.Monitor;
 import org.apache.derby.iapi.store.raw.data.DataFactory;
 import org.apache.derby.io.StorageFile;
-import org.apache.derby.shared.common.error.ExceptionUtil;
+import org.apache.derby.iapi.error.ExceptionUtil;
 import org.apache.derby.shared.common.reference.MessageId;
 
 /**

Modified: db/derby/code/trunk/tools/jar/extraDBMSclasses.properties
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/tools/jar/extraDBMSclasses.properties?rev=1043290&r1=1043289&r2=1043290&view=diff
==============================================================================
--- db/derby/code/trunk/tools/jar/extraDBMSclasses.properties (original)
+++ db/derby/code/trunk/tools/jar/extraDBMSclasses.properties Wed Dec  8 03:34:19 2010
@@ -101,3 +101,4 @@ derby.module.store.cpf=org.apache.derby.
 derby.module.xml.sqlxmle=org.apache.derby.impl.sql.execute.SqlXmlExecutor
 
 derby.module.shared.threaddump=org.apache.derby.shared.common.sanity.ThreadDump
+derby.module.engine.threaddump=org.apache.derby.iapi.error.ThreadDump



Mime
View raw message