some comment stripping
[sdbl4j] / src / java / org / gc / sdbl4j / DBConnectionPool.java
index e0a4422fda5d7a9f6683084c4c942df4f8933987..336c2d06ff65f9fb56cdfa2e4e290debe4cabc23 100644 (file)
@@ -179,11 +179,7 @@ public class DBConnectionPool {
      *   This connection can be reserved or shared with other supplementary Threads depending on configuration
      *   (<code>connectionGroup</code> for asynchronous events, dedicated instance properties for other threads).
      *   
-     * (1) a supplementary Thread, e.g. not a request processing Thread, is thus:
-     *     - any Thread invoking asynchronous events (such as Spooler, ElectionsStartStop, etc)
-     *     - the Spooler sub threads used for MT retries
-     *     - any {@link ThreadedElementsConsumer} uses, e.g. DB loggers, DLR processors, Notification Sender
-     *     - the MessageSender threads
+     * (1) a supplementary Thread, e.g. not a request processing Thread
      */ 
     public static Connection getConnection( String dbkind ) {
         // for request processing threads, we have one connection per thread; for supplementary threads, we
@@ -211,14 +207,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 +276,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 +416,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 "