[jboss-cvs] JBossAS SVN: r78074 - trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Sep 5 06:59:12 EDT 2008


Author: jesper.pedersen
Date: 2008-09-05 06:59:12 -0400 (Fri, 05 Sep 2008)
New Revision: 78074

Modified:
   trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLReplicationValidConnectionChecker.java
   trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLValidConnectionChecker.java
   trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/OracleValidConnectionChecker.java
Log:
[JBAS-5047] Fix ValidConnectionChecker serialization

Modified: trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLReplicationValidConnectionChecker.java
===================================================================
--- trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLReplicationValidConnectionChecker.java	2008-09-05 09:45:33 UTC (rev 78073)
+++ trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLReplicationValidConnectionChecker.java	2008-09-05 10:59:12 UTC (rev 78074)
@@ -21,7 +21,11 @@
  */
 package org.jboss.resource.adapter.jdbc.vendor;
 
+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;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -45,164 +49,213 @@
  */
 public class MySQLReplicationValidConnectionChecker implements ValidConnectionChecker, Serializable
 {
-	/**
-	 * Serial version ID
-	 */
-	private static final long serialVersionUID = 2658231045989623858L;
+   /**
+    * Serial version ID
+    */
+   private static final long serialVersionUID = 2658231045989623858L;
 
-	/**
-	 * Tells if the connection supports the isValid method.
-	 * (Java 6 only)
-	 */
-	private boolean driverHasIsValidMethod = false;
+   /**
+    * Tells if the connection supports the isValid method.
+    * (Java 6 only)
+    */
+   private boolean driverHasIsValidMethod;
 	
-	/**
-	 * Tells if the connection supports the ping method.
-	 */
-	private boolean driverHasPingMethod = false;
+   private transient Method isValid;
 
-	/**
-	 * Classname of the supported connection
-	 */
-	protected final static String CONNECTION_CLASS = "com.mysql.jdbc.ReplicationConnection";
+   /**
+    * Tells if the connection supports the ping method.
+    */
+   private boolean driverHasPingMethod;
+
+   private transient Method ping;
+
+   /**
+    * Classname of the supported connection
+    */
+   protected final static String CONNECTION_CLASS = "com.mysql.jdbc.ReplicationConnection";
 	
-	/**
-	 * Log access object
-	 */
-	private static final Logger log = Logger.getLogger(MySQLReplicationValidConnectionChecker.class);
+   /**
+    * Log access object
+    */
+   private static transient Logger log;
 	
-	// The timeout (apparently the timeout is ignored?)
-	private static Object[] timeoutParam = new Object[] {};
+   // The timeout (apparently the timeout is ignored?)
+   private static Object[] timeoutParam = new Object[] {};
 
-	/**
-	 * Initiates the ValidConnectionChecker implementation.
-	 */
-	public MySQLReplicationValidConnectionChecker() 
-	{
-		Class mysqlConnection = null;
+   /**
+    * 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);
+      }
+   }
 		
-		try
-		{
-			// Load connection class
-			mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass( CONNECTION_CLASS  );
+   /* (non-Javadoc)
+    * @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection)
+    */
+   public SQLException isValidConnection(Connection c) 
+   {
+      if (driverHasIsValidMethod)
+      {
+         try 
+         {
+            isValid.invoke(c, timeoutParam);
+         } 
+         catch (Exception e) 
+         {
+            if (e instanceof SQLException)
+            {
+               return (SQLException) e;
+            }
+            else
+            {
+               log.warn("Unexpected error in ping", e);
+               return new SQLException("ping failed: " + e.toString());
+            }
+         }
+      }
+      else if (driverHasPingMethod) 
+      {
+         //if there is a ping method then use it
+         try
+         {
+            ping.invoke(c, timeoutParam);
+         }
+         catch (Exception e)
+         {
+            if (e instanceof SQLException)
+            {
+               return (SQLException) e;
+            }
+            else
+            {
+               log.warn("Unexpected error in ping", e);
+               return new SQLException("ping failed: " + e.toString());
+            }
+         }
+      } 
+      else 
+      {
+         //otherwise just use a 'SELECT 1' statement
 			
-			// Check for Java 6 compatibility and use isValid on the connection
-			try 
-			{
-				mysqlConnection.getMethod("isValid", new Class[] {});
-				driverHasIsValidMethod = true;
-			} 
+         Statement stmt = null;
+         ResultSet rs = null;
+         
+         try
+         {
+            stmt = c.createStatement();
+            rs = stmt.executeQuery("SELECT 1");
+         }
+         catch (Exception e)
+         {
+            if (e instanceof SQLException)
+            {
+               return (SQLException) e;
+            }
+            else 
+            {
+               log.warn("Unexpected error in ping (SELECT 1)", e);
+               return new SQLException("ping (SELECT 1) failed: " + e.toString());
+            }
+            
+         }
+         finally
+         {
+            // Cleanup everything and make sure to handle
+            // sql exceptions occuring
+            try
+            {
+               if (rs != null) 
+                  rs.close();
+            }
+            catch (SQLException e)
+            {
+            }
+            try
+            {
+               if (stmt != null) 
+                  stmt.close();
+            }
+            catch (SQLException e)
+            {
+            }
+         }
+      }
+      return null;
+   }
+
+
+   private void initPing() throws ClassNotFoundException, NoSuchMethodException
+   {
+      driverHasIsValidMethod = false;
+      driverHasPingMethod = false;
+
+      log = Logger.getLogger(MySQLReplicationValidConnectionChecker.class);
+
+      // Load connection class
+      Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass( CONNECTION_CLASS  );
 			
-			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);
-			}
+      // 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
-					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);
-				}
-				
-			}
-			
-		} catch (ClassNotFoundException e) {
-			log.error("Cannot find the driver class defined in CONNECTION_CLASS", e);
-		}
-	}
-	
-	
-	/* (non-Javadoc)
-	 * @see org.jboss.resource.adapter.jdbc.ValidConnectionChecker#isValidConnection(java.sql.Connection)
-	 */
-	public SQLException isValidConnection(Connection c) 
-	{
-		
-		if (driverHasIsValidMethod)
-		{
-			try {
-				c.getClass().getMethod("isValid", new Class[] {}).invoke(c, timeoutParam);
-			} catch (Exception e) {
-				if (e instanceof SQLException) {
-					return (SQLException) e;
-				} else {
-					log.warn("Unexpected error in ping", e);
-					return new SQLException("ping failed: " + e.toString());
-				}
-			}
-		}
-		
-		//if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
-		else if (driverHasPingMethod) 
-		{
-			try {
-				c.getClass().getMethod("ping", new Class[] {}).invoke(c, timeoutParam);
-			} catch (Exception e) {
-				if (e instanceof SQLException) {
-					return (SQLException) e;
-				} else {
-					log.warn("Unexpected error in ping", e);
-					return new SQLException("ping failed: " + e.toString());
-				}
-			}
-			
-		} 
-		
-		else 
-		{
-			
-			Statement stmt = null;
-			ResultSet rs = null;
-			
-			try {
-				stmt = c.createStatement();
-				rs = stmt.executeQuery("SELECT 1");
-			
-			} catch (Exception e) {
-				
-				if (e instanceof SQLException)
-					return (SQLException) e;
-				
-				else 
-				{
-					log.warn("Unexpected error in ping (SELECT 1)", e);
-					return new SQLException("ping (SELECT 1) failed: " + e.toString());
-				}
-				
-			} finally {
-				
-				// Cleanup everything and make sure to handle
-				// sql exceptions occuring
-				try {
-					if (rs != null) rs.close();
-				} catch (SQLException e) {}
-				finally {
-					try {
-						if (stmt != null) stmt.close();
-					} catch (SQLException e) {}
-				}
-			}
-			
-		}
-		return null;
-	}
+      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: trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLValidConnectionChecker.java
===================================================================
--- trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLValidConnectionChecker.java	2008-09-05 09:45:33 UTC (rev 78073)
+++ trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/MySQLValidConnectionChecker.java	2008-09-05 10:59:12 UTC (rev 78074)
@@ -21,6 +21,9 @@
  */
 package org.jboss.resource.adapter.jdbc.vendor;
 
+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;
@@ -45,25 +48,22 @@
 public class MySQLValidConnectionChecker implements ValidConnectionChecker, Serializable {
 
 
-   private static final Logger log = Logger.getLogger(MySQLValidConnectionChecker.class);
+   private static transient Logger log;
     
    private static final long serialVersionUID = 1323747853035005642L;
 
-   private boolean driverHasPingMethod = false;
-   
+   private boolean driverHasPingMethod;
+
    // The timeout (apparently the timeout is ignored?)
    private static Object[] params = new Object[] {};
    
+   private transient Method ping;
+   
    public MySQLValidConnectionChecker()
    {
       try
       {
-         Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
-         Method ping = mysqlConnection.getMethod("ping", new Class[] {});
-         if (ping != null)
-         {
-            driverHasPingMethod = true;
-         }
+         initPing();
       }
       catch (Exception e)
       {
@@ -78,8 +78,6 @@
       {
          try
          {
-            Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
-            Method ping = mysqlConnection.getMethod("ping", new Class[] {});
             ping.invoke(c, params);
          }
          catch (Exception e)
@@ -97,7 +95,6 @@
       }
       else
       {
-
          Statement stmt = null;
          ResultSet rs = null;
          try
@@ -133,8 +130,39 @@
             
             }
          }
-
       }
       return null;
    }
+
+   private void initPing() throws ClassNotFoundException, NoSuchMethodException
+   {
+      log = Logger.getLogger(MySQLValidConnectionChecker.class);
+      driverHasPingMethod = false;
+
+      Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
+      ping = mysqlConnection.getMethod("ping", new Class[] {});
+      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;
+      }
+   }
 }

Modified: trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/OracleValidConnectionChecker.java
===================================================================
--- trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/OracleValidConnectionChecker.java	2008-09-05 09:45:33 UTC (rev 78073)
+++ trunk/connector/src/main/org/jboss/resource/adapter/jdbc/vendor/OracleValidConnectionChecker.java	2008-09-05 10:59:12 UTC (rev 78074)
@@ -21,6 +21,9 @@
  */
 package org.jboss.resource.adapter.jdbc.vendor;
 
+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;
@@ -41,17 +44,18 @@
 {
    private static final long serialVersionUID = 5379340663276548636L;
 
-   private static final Logger log = Logger.getLogger(OracleValidConnectionChecker.class);
+   private static transient Logger log;
 
    // The timeout (apparently the timeout is ignored?)
    private static Object[] params = new Object[] { new Integer(5000) };
 
+   private transient Method ping;
+
    public OracleValidConnectionChecker()
    {
       try
       {
-         Class oracleConnection = Thread.currentThread().getContextClassLoader().loadClass("oracle.jdbc.driver.OracleConnection");
-         Method ping = oracleConnection.getMethod("pingDatabase", new Class[] { Integer.TYPE });
+         initPing();
       }
       catch (Exception e)
       {
@@ -63,12 +67,10 @@
    {
       try
       {
-         Class oracleConnection = Thread.currentThread().getContextClassLoader().loadClass("oracle.jdbc.driver.OracleConnection");
-         Method ping = oracleConnection.getMethod("pingDatabase", new Class[] { Integer.TYPE });
          Integer status = (Integer) ping.invoke(c, params);
 
          // Error
-         if (status.intValue() < 0)
+         if (status == null || status.intValue() < 0)
             return new SQLException("pingDatabase failed status=" + status);
       }
       catch (Exception e)
@@ -80,4 +82,31 @@
       // OK
       return null;
    }
+
+   private void initPing() throws ClassNotFoundException, NoSuchMethodException
+   {
+      log = Logger.getLogger(OracleValidConnectionChecker.class);
+
+      Class oracleConnection = Thread.currentThread().getContextClassLoader().loadClass("oracle.jdbc.driver.OracleConnection");
+      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;
+      }
+   }
 }




More information about the jboss-cvs-commits mailing list