仿照ibatis的风格实现Log,以及添加ConnectionChecker

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@292 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-06-29 04:22:34 +00:00
parent bad5cdc7a8
commit 3c07ac1224
7 changed files with 608 additions and 0 deletions
@@ -0,0 +1,8 @@
package com.alibaba.druid.pool;
import java.sql.Connection;
import java.sql.SQLException;
public interface ValidConnectionChecker {
SQLException isValidConnection(Connection c);
}
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, 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 com.alibaba.druid.pool.vendor;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.alibaba.druid.logging.Log;
import com.alibaba.druid.logging.LogFactory;
import com.alibaba.druid.pool.ValidConnectionChecker;
import com.alibaba.druid.util.JdbcUtils;
/**
* A MSSQLValidConnectionChecker.
*
* @author <a href="weston.price@jboss.com">Weston Price</a>
* @version $Revision: 57189 $
*/
public class MSSQLValidConnectionChecker implements ValidConnectionChecker, Serializable {
private static final String QUERY = "SELECT x";
private static final Log LOG = LogFactory.getLog(MSSQLValidConnectionChecker.class);
/** The serialVersionUID */
private static final long serialVersionUID = 3995516551833725723L;
public SQLException isValidConnection(final Connection c) {
SQLException sqe = null;
Statement stmt = null;
try {
stmt = c.createStatement();
stmt.execute(QUERY);
}
catch (SQLException e) {
LOG.warn("warning: connection validation failed for current managed connection.");
sqe = e;
} finally {
JdbcUtils.close(stmt);
}
return sqe;
}
}
@@ -0,0 +1,76 @@
package com.alibaba.druid.pool.vendor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.alibaba.druid.logging.Log;
import com.alibaba.druid.logging.LogFactory;
import com.alibaba.druid.pool.ValidConnectionChecker;
import com.alibaba.druid.util.JdbcUtils;
public class MySQLValidConnectionChecker implements ValidConnectionChecker, Serializable {
private static final long serialVersionUID = 1L;
private static final Log LOG = LogFactory.getLog(MySQLValidConnectionChecker.class);
private Method ping;
private boolean driverHasPingMethod = false;
// The timeout (apparently the timeout is ignored?)
private static Object[] params = new Object[] {};
@SuppressWarnings({ "unchecked", "rawtypes" })
public MySQLValidConnectionChecker(){
try {
Class mysqlConnection = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
ping = mysqlConnection.getMethod("ping", new Class[] {});
if (ping != null) {
driverHasPingMethod = true;
}
} catch (Exception e) {
LOG.warn("Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
}
}
public SQLException isValidConnection(Connection c) {
// if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
if (driverHasPingMethod) {
try {
ping.invoke(c, params);
} 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 {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
}
}
return null;
}
}
@@ -0,0 +1,67 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, 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 com.alibaba.druid.pool.vendor;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import com.alibaba.druid.logging.Log;
import com.alibaba.druid.logging.LogFactory;
import com.alibaba.druid.pool.ValidConnectionChecker;
public class OracleValidConnectionChecker implements ValidConnectionChecker, Serializable {
private static final long serialVersionUID = -2227528634302168877L;
private static final Log LOG = LogFactory.getLog(OracleValidConnectionChecker.class);
private Method ping;
// The timeout (apparently the timeout is ignored?)
private static Object[] params = new Object[] { new Integer(5000) };
@SuppressWarnings({ "unchecked", "rawtypes" })
public OracleValidConnectionChecker(){
try {
Class oracleConnection = Thread.currentThread().getContextClassLoader().loadClass("oracle.jdbc.driver.OracleConnection");
ping = oracleConnection.getMethod("pingDatabase", new Class[] { Integer.TYPE });
} catch (Exception e) {
throw new RuntimeException("Unable to resolve pingDatabase method:", e);
}
}
public SQLException isValidConnection(Connection c) {
try {
Integer status = (Integer) ping.invoke(c, params);
// Error
if (status.intValue() < 0) return new SQLException("pingDatabase failed status=" + status);
} catch (Exception e) {
LOG.warn("Unexpected error in pingDatabase", e);
}
// OK
return null;
}
}