[jboss-cvs] JBossAS SVN: r112646 - in projects/jboss-jca/trunk: adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/novendor and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Feb 14 16:13:20 EST 2012


Author: jesper.pedersen
Date: 2012-02-14 16:13:19 -0500 (Tue, 14 Feb 2012)
New Revision: 112646

Added:
   projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/novendor/JDBC4ValidConnectionChecker.java
Modified:
   projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLReplicationValidConnectionChecker.java
   projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLValidConnectionChecker.java
   projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/oracle/OracleValidConnectionChecker.java
   projects/jboss-jca/trunk/doc/userguide/en-US/modules/deployment.xml
Log:
Additional plugin cleanups

Modified: projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLReplicationValidConnectionChecker.java
===================================================================
--- projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLReplicationValidConnectionChecker.java	2012-02-14 20:21:22 UTC (rev 112645)
+++ projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLReplicationValidConnectionChecker.java	2012-02-14 21:13:19 UTC (rev 112646)
@@ -24,9 +24,6 @@
 
 import org.jboss.jca.adapters.jdbc.spi.ValidConnectionChecker;
 
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.sql.Connection;
@@ -51,52 +48,18 @@
  */
 public class MySQLReplicationValidConnectionChecker implements ValidConnectionChecker, Serializable
 {
+   private static Logger log = Logger.getLogger(MySQLReplicationValidConnectionChecker.class);
+
    /**
     * Serial version ID
     */
    private static final long serialVersionUID = 2658231045989623858L;
 
    /**
-    * Tells if the connection supports the isValid method.
-    * (Java 6 only)
-    */
-   private boolean driverHasIsValidMethod;
-
-   private transient Method isValid;
-
-   /**
-    * Tells if the connection supports the ping method.
-    */
-   private boolean driverHasPingMethod;
-
-   private transient Method ping;
-
-   /**
-    * Classname of the supported connection
-    */
-   protected static final String CONNECTION_CLASS = "com.mysql.jdbc.ReplicationConnection";
-
-   /**
-    * Log access object
-    */
-   private static transient Logger log;
-
-   // The timeout (apparently the timeout is ignored?)
-   private static Object[] timeoutParam = new Object[] {};
-
-   /**
     * Initiates the ValidConnectionChecker implementation.
     */
    public MySQLReplicationValidConnectionChecker()
    {
-      try
-      {
-         initPing();
-      }
-      catch (Exception e)
-      {
-         log.warn("Cannot find the driver class defined in CONNECTION_CLASS.  Will use 'SELECT 1' instead.", e);
-      }
    }
 
    /**
@@ -105,12 +68,38 @@
    @Override
    public SQLException isValidConnection(Connection c)
    {
-      if (driverHasIsValidMethod)
+      Method isValid = null;
+      Method ping = null;
+
+      try
       {
+         isValid = c.getClass().getMethod("isValid", new Class<?>[] {});
+         isValid.setAccessible(true);
+      }
+      catch (Throwable t)
+      {
+         // Ignore
+      }
+
+      if (isValid == null)
+      {
          try
          {
-            isValid.invoke(c, timeoutParam);
+            ping = c.getClass().getMethod("ping", (Class[])null);
+            ping.setAccessible(true);
          }
+         catch (Throwable t)
+         {
+            // Ignore
+         }
+      }
+
+      if (isValid != null)
+      {
+         try
+         {
+            isValid.invoke(c, new Object[] {});
+         }
          catch (Exception e)
          {
             if (e instanceof SQLException)
@@ -119,17 +108,16 @@
             }
             else
             {
-               log.warn("Unexpected error in ping", e);
-               return new SQLException("ping failed: " + e.toString());
+               log.warn("Unexpected error", e);
+               return new SQLException("IsValid failed: " + e.toString());
             }
          }
       }
-      else if (driverHasPingMethod)
+      else if (ping != null)
       {
-         //if there is a ping method then use it
          try
          {
-            ping.invoke(c, timeoutParam);
+            ping.invoke(c, (Object[])null);
          }
          catch (Exception e)
          {
@@ -139,8 +127,8 @@
             }
             else
             {
-               log.warn("Unexpected error in ping", e);
-               return new SQLException("ping failed: " + e.toString());
+               log.warn("Unexpected error", e);
+               return new SQLException("Ping failed: " + e.toString());
             }
          }
       }
@@ -163,8 +151,8 @@
             }
             else
             {
-               log.warn("Unexpected error in ping (SELECT 1)", e);
-               return new SQLException("ping (SELECT 1) failed: " + e.toString());
+               log.warn("Unexpected error", e);
+               return new SQLException("SELECT 1 failed: " + e.toString());
             }
          }
          finally
@@ -194,73 +182,4 @@
 
       return null;
    }
-
-
-   @SuppressWarnings("unchecked")
-   private void initPing() throws ClassNotFoundException, NoSuchMethodException
-   {
-      driverHasIsValidMethod = false;
-      driverHasPingMethod = false;
-
-      log = Logger.getLogger(MySQLReplicationValidConnectionChecker.class);
-
-      // Load connection class
-      Class<?> mysqlConnection = Class.forName(CONNECTION_CLASS, true, getClass().getClassLoader());
-
-      // Check for Java 6 compatibility and use isValid on the connection
-      try
-      {
-         isValid = mysqlConnection.getMethod("isValid", new Class<?>[] {});
-         driverHasIsValidMethod = true;
-      }
-      catch (NoSuchMethodException e)
-      {
-         // Notify someone
-         log.info("Cannot resolve com.mysq.jdbc.ReplicationConnection.isValid method. Fallback to ping.", e);
-      }
-      catch (SecurityException e)
-      {
-         // Notify someone
-         log.info("Cannot resolve com.mysq.jdbc.ReplicationConnection.isValid method. Fallback to ping.", e);
-      }
-
-      if (!driverHasIsValidMethod)
-      {
-         try
-         {
-            // Check for ping method
-            ping = mysqlConnection.getMethod("ping", new Class<?>[] {});
-            driverHasPingMethod = true;
-         }
-         catch (NoSuchMethodException e)
-         {
-            // Notify someone
-            log.warn("Cannot resolve com.mysq.jdbc.ReplicationConnection.ping method. Will use 'SELECT 1' instead.", e);
-         }
-         catch (SecurityException e)
-         {
-            // Notify someone
-            log.info("Cannot resolve com.mysq.jdbc.ReplicationConnection.ping method. Will use 'SELECT 1' instead.", e);
-         }
-      }
-   }
-
-   private void writeObject(ObjectOutputStream stream) throws IOException
-   {
-      // nothing
-   }
-
-   private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
-   {
-      try
-      {
-         initPing();
-      }
-      catch (Exception e)
-      {
-         IOException ioe = new IOException("Unable to resolve ping method: " + e.getMessage());
-         ioe.initCause(e);
-         throw ioe;
-      }
-   }
 }

Modified: projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLValidConnectionChecker.java
===================================================================
--- projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLValidConnectionChecker.java	2012-02-14 20:21:22 UTC (rev 112645)
+++ projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/mysql/MySQLValidConnectionChecker.java	2012-02-14 21:13:19 UTC (rev 112646)
@@ -24,9 +24,6 @@
 
 import org.jboss.jca.adapters.jdbc.spi.ValidConnectionChecker;
 
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.sql.Connection;
@@ -49,27 +46,15 @@
  */
 public class MySQLValidConnectionChecker implements ValidConnectionChecker, Serializable
 {
-   private static transient Logger log;
+   private static Logger log = Logger.getLogger(MySQLValidConnectionChecker.class);
 
    private static final long serialVersionUID = 1323747853035005642L;
 
-   private boolean driverHasPingMethod;
-
-   private transient Method ping;
-
    /**
     * Constructor
     */
    public MySQLValidConnectionChecker()
    {
-      try
-      {
-         initPing();
-      }
-      catch (Exception e)
-      {
-         log.warn("Cannot resolve com.mysq.jdbc.Connection.ping method.  Will use 'SELECT 1' instead.", e);
-      }
    }
 
    /**
@@ -78,9 +63,20 @@
    @Override
    public SQLException isValidConnection(Connection c)
    {
+      Method ping = null;
 
+      try
+      {
+         ping = c.getClass().getMethod("ping", (Class[])null);
+         ping.setAccessible(true);
+      }
+      catch (Throwable t)
+      {
+         // Ignore
+      }
+
       //if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
-      if (driverHasPingMethod)
+      if (ping != null)
       {
          try
          {
@@ -94,8 +90,8 @@
             }
             else
             {
-               log.warn("Unexpected error in ping", e);
-               return new SQLException("ping failed: " + e.toString());
+               log.warn("Unexpected error", e);
+               return new SQLException("Ping failed: " + e.toString());
             }
          }
       }
@@ -116,8 +112,8 @@
             }
             else
             {
-               log.warn("Unexpected error in ping (SELECT 1)", e);
-               return new SQLException("ping (SELECT 1) failed: " + e.toString());
+               log.warn("Unexpected error", e);
+               return new SQLException("SELECT 1 failed: " + e.toString());
             }
          }
          finally
@@ -142,41 +138,7 @@
             }
          }
       }
+
       return null;
    }
-
-   @SuppressWarnings("unchecked")
-   private void initPing() throws ClassNotFoundException, NoSuchMethodException
-   {
-      log = Logger.getLogger(MySQLValidConnectionChecker.class);
-      driverHasPingMethod = false;
-
-      Class<?> mysqlConnection = Class.forName("com.mysql.jdbc.Connection", true, getClass().getClassLoader());
-
-      ping = mysqlConnection.getMethod("ping", (Class[])null);
-
-      if (ping != null)
-      {
-         driverHasPingMethod = true;
-      }
-   }
-
-   private void writeObject(ObjectOutputStream stream) throws IOException
-   {
-      // nothing
-   }
-
-   private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
-   {
-      try
-      {
-         initPing();
-      }
-      catch (Exception e)
-      {
-         IOException ioe = new IOException("Unable to resolve ping method: " + e.getMessage());
-         ioe.initCause(e);
-         throw ioe;
-      }
-   }
 }

Added: projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/novendor/JDBC4ValidConnectionChecker.java
===================================================================
--- projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/novendor/JDBC4ValidConnectionChecker.java	                        (rev 0)
+++ projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/novendor/JDBC4ValidConnectionChecker.java	2012-02-14 21:13:19 UTC (rev 112646)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2012, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.jca.adapters.jdbc.extensions.novendor;
+
+import org.jboss.jca.adapters.jdbc.spi.ValidConnectionChecker;
+
+import java.io.Serializable;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * Use JDBC4 for connection validation
+ *
+ * @author <a href="mailto:jesper.pedersen at jboss.org">Jesper Pedersen</a>
+ */
+public class JDBC4ValidConnectionChecker implements ValidConnectionChecker, Serializable
+{
+   private static final long serialVersionUID = 1L;
+
+   private int pingTimeout;
+
+   /**
+    * Constructor
+    */
+   public JDBC4ValidConnectionChecker()
+   {
+      pingTimeout = 5;
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   public SQLException isValidConnection(Connection c)
+   {
+      try
+      {
+         boolean result = c.isValid(pingTimeout);
+
+         if (!result)
+            return new SQLException("Invalid connection: " + c);
+      }
+      catch (SQLException sqle)
+      {
+         return sqle;
+      }
+
+      return null;
+   }
+
+   /**
+    * Get the pingTimeOut.
+    *
+    * @return the pingTimeOut.
+    */
+   public int getPingTimeOut()
+   {
+      return pingTimeout;
+   }
+
+   /**
+    * Set the pingTimeOut.
+    *
+    * @param v The pingTimeOut to set.
+    */
+   public void setPingTimeOut(int v)
+   {
+      this.pingTimeout = v;
+   }
+}

Modified: projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/oracle/OracleValidConnectionChecker.java
===================================================================
--- projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/oracle/OracleValidConnectionChecker.java	2012-02-14 20:21:22 UTC (rev 112645)
+++ projects/jboss-jca/trunk/adapters/src/main/java/org/jboss/jca/adapters/jdbc/extensions/oracle/OracleValidConnectionChecker.java	2012-02-14 21:13:19 UTC (rev 112646)
@@ -24,9 +24,6 @@
 
 import org.jboss.jca.adapters.jdbc.spi.ValidConnectionChecker;
 
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.sql.Connection;
@@ -44,26 +41,17 @@
 {
    private static final long serialVersionUID = 5379340663276548636L;
 
-   private static transient Logger log;
+   private static Logger log = Logger.getLogger(OracleValidConnectionChecker.class);
 
    // The timeout in seconds (apparently the timeout is ignored?)
-   private Integer pingTimeOut = new Integer(5);
+   private Integer pingTimeOut;
 
-   private transient Method ping;
-
    /**
     * Constructor
     */
    public OracleValidConnectionChecker()
    {
-      try
-      {
-         initPing();
-      }
-      catch (Exception e)
-      {
-         throw new RuntimeException("Unable to resolve pingDatabase method:", e);
-      }
+      pingTimeOut = Integer.valueOf(5);
    }
 
    /**
@@ -72,10 +60,13 @@
    @Override
    public SQLException isValidConnection(Connection c)
    {
-      Object[] params = new Object[]{pingTimeOut};
+      Object[] params = new Object[] {pingTimeOut};
 
       try
       {
+         Method ping = c.getClass().getMethod("pingDatabase", new Class<?>[] {Integer.TYPE});
+         ping.setAccessible(true);
+
          Integer status = (Integer) ping.invoke(c, params);
 
          // Error
@@ -92,41 +83,12 @@
       return null;
    }
 
-   @SuppressWarnings("unchecked")
-   private void initPing() throws ClassNotFoundException, NoSuchMethodException
-   {
-      log = Logger.getLogger(OracleValidConnectionChecker.class);
-
-      Class<?> oracleConnection =
-         Class.forName("oracle.jdbc.driver.OracleConnection", true, getClass().getClassLoader());
-      ping = oracleConnection.getMethod("pingDatabase", new Class<?>[] {Integer.TYPE});
-   }
-
-   private void writeObject(ObjectOutputStream stream) throws IOException
-   {
-      // nothing
-   }
-
-   private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
-   {
-      try
-      {
-         initPing();
-      }
-      catch (Exception e)
-      {
-         IOException ioe = new IOException("Unable to resolve pingDatabase method: " + e.getMessage());
-         ioe.initCause(e);
-         throw ioe;
-      }
-   }
-
    /**
     * Get the pingTimeOut.
     *
     * @return the pingTimeOut.
     */
-   public final Integer getPingTimeOut()
+   public Integer getPingTimeOut()
    {
       return pingTimeOut;
    }
@@ -136,7 +98,7 @@
     *
     * @param pingTimeOut The pingTimeOut to set.
     */
-   public final void setPingTimeOut(Integer pingTimeOut)
+   public void setPingTimeOut(Integer pingTimeOut)
    {
       this.pingTimeOut = pingTimeOut;
    }

Modified: projects/jboss-jca/trunk/doc/userguide/en-US/modules/deployment.xml
===================================================================
--- projects/jboss-jca/trunk/doc/userguide/en-US/modules/deployment.xml	2012-02-14 20:21:22 UTC (rev 112645)
+++ projects/jboss-jca/trunk/doc/userguide/en-US/modules/deployment.xml	2012-02-14 21:13:19 UTC (rev 112646)
@@ -1683,6 +1683,9 @@
             <listitem>
               <code>org.jboss.jca.adapters.jdbc.extensions.novendor.NullValidConnectionChecker</code>
             </listitem>
+            <listitem>
+              <code>org.jboss.jca.adapters.jdbc.extensions.novendor.JDBC4ValidConnectionChecker</code>
+            </listitem>
           </itemizedlist>
         </para>
 



More information about the jboss-cvs-commits mailing list