some more merges: one more tomcat known thread, check if connection is still alive...
authorGuillaume Cottenceau <gcottenc@gmail.com>
Mon, 18 Feb 2013 20:08:30 +0000 (21:08 +0100)
committerGuillaume Cottenceau <gcottenc@gmail.com>
Mon, 18 Feb 2013 20:08:30 +0000 (21:08 +0100)
src/java/org/gc/sdbl4j/DBConnectionPool.java

index e0a4422fda5d7a9f6683084c4c942df4f8933987..af0223c95756a973db89edde32657fbe84c9aa46 100644 (file)
@@ -211,14 +211,15 @@ public class DBConnectionPool {
             // sanity
             if ( ! Thread.currentThread().getName().startsWith( "http" )
                  && ! Thread.currentThread().getName().startsWith( "pool" )
-                 && ! Thread.currentThread().getName().equals( "main" ) ) {
+                 && ! Thread.currentThread().getName().equals( "main" )
+                 && ! Thread.currentThread().getName().equals( "startStop" ) ) {
                 if ( isSupportedExternalThread( Thread.currentThread().getName() ) ) {
                     log.debug( "Thread identified as being externally created (external library): the DB " +
                                "connections allocated to non-request processing threads MUST be " +
                                "manually released after use" );
                 } else {
                     log.error( "Thread identified as request processing thread (parent of thread group is not "
-                               + "SupplementaryThreadHelper.parentThreadGroup), but name doesn't start with http!"
+                               + "SupplementaryThreadHelper.parentThreadGroup), but name doesn't start with http or pool or main or startStop!"
                                + " (thread name: " + Thread.currentThread().getName() + ")" );
                 }
             }
@@ -279,6 +280,11 @@ public class DBConnectionPool {
         if ( conn == null ) {
             return;
         }
+
+        if ( available == null ) {
+            log.error( "No available connections for " + key + " - already closed?" );
+            return;
+        }
         
         // There are subtle interleaved synchronizations happening here.
         // Use a traffic load generator with low preOpenedConnections for all modifications in here!
@@ -414,7 +420,23 @@ public class DBConnectionPool {
                     log.debug( "created connection " + conn + ", since busy size is " + busy.size() );
                 }
             } else {
-                // TODO: check if the connection is still alive?
+                try {
+                    long time_before = System.currentTimeMillis();
+                    conn.createStatement().executeQuery( "SELECT 1" );
+                    if ( log.isDebugEnabled() ) {
+                        log.debug( ( System.currentTimeMillis() - time_before ) + " ms for: SELECT 1 [connection validation]" );
+                    }
+                } catch ( SQLException se ) {
+                    log.error( "Validation error for idle connection for " + dbkind + ": " + se
+                               + ", will replace connection with a newly created one" );
+                    try {
+                        conn.close();
+                    } catch ( SQLException se2 ) {}
+                    conn = dbParams.createConnection();
+                    if ( conn == null ) {
+                        throw new RuntimeException( "Connection creation not available" );
+                    }
+                }
                 if ( log.isTraceEnabled() ) {
                     if ( busyKey instanceof ThreadGroup ) {
                         log.trace( dbkind + ": use available connection for "