mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
仿照ibatis的风格实现Log,以及添加ConnectionChecker
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@299 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package com.alibaba.druid.logging;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Jdk14LoggingImpl implements Log {
|
||||
|
||||
private Logger log;
|
||||
|
||||
public Jdk14LoggingImpl(Class<?> clazz){
|
||||
log = Logger.getLogger(clazz.toString());
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isLoggable(Level.FINE);
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
log.log(Level.SEVERE, s, e);
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
log.log(Level.SEVERE, s);
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.log(Level.FINE, s);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.log(Level.WARNING, s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
log.log(Level.WARNING, s, e);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.alibaba.druid.logging;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class Log4jImpl implements Log {
|
||||
|
||||
private Logger log;
|
||||
|
||||
public Log4jImpl(Class<?> clazz){
|
||||
log = Logger.getLogger(clazz);
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return log.isDebugEnabled();
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
log.error(s, e);
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
log.error(s);
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
log.debug(s);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.warn(s);
|
||||
}
|
||||
|
||||
public void warn(String s, Throwable e) {
|
||||
log.warn(s, e);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,9 +10,9 @@ public class LogFactory {
|
||||
static {
|
||||
tryImplementation("org.apache.commons.logging.LogFactory",
|
||||
"com.ibatis.common.logging.jakarta.JakartaCommonsLoggingImpl");
|
||||
tryImplementation("org.apache.log4j.Logger", "com.ibatis.common.logging.log4j.Log4jImpl");
|
||||
tryImplementation("java.util.logging.Logger", "com.ibatis.common.logging.jdk14.Jdk14LoggingImpl");
|
||||
tryImplementation("java.lang.Object", "com.ibatis.common.logging.nologging.NoLoggingImpl");
|
||||
tryImplementation("org.apache.log4j.Logger", "com.alibaba.druid.logging.Log4jImpl");
|
||||
tryImplementation("java.util.logging.Logger", "com.alibaba.druid.logging.Jdk14LoggingImpl");
|
||||
tryImplementation("java.lang.Object", "com.alibaba.druid.logging.NoLoggingImpl");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.alibaba.druid.logging;
|
||||
|
||||
public class NoLoggingImpl implements Log {
|
||||
|
||||
public NoLoggingImpl(Class<?> clazz){
|
||||
}
|
||||
|
||||
public boolean isDebugEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void error(String s, Throwable e) {
|
||||
}
|
||||
|
||||
public void error(String s) {
|
||||
}
|
||||
|
||||
public void debug(String s) {
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(String s, Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+13
-3
@@ -17,17 +17,17 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
|
||||
private Class<?> clazz;
|
||||
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[] {});
|
||||
clazz = Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Connection");
|
||||
ping = clazz.getMethod("ping", new Class[] {});
|
||||
if (ping != null) {
|
||||
driverHasPingMethod = true;
|
||||
}
|
||||
@@ -37,6 +37,16 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection c) {
|
||||
try {
|
||||
Connection conn = (Connection) c.unwrap(clazz);
|
||||
if (conn != null) {
|
||||
c = conn;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
// if there is a ping method then use it, otherwise just use a 'SELECT 1' statement
|
||||
if (driverHasPingMethod) {
|
||||
try {
|
||||
|
||||
+8
-3
@@ -35,16 +35,16 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(OracleValidConnectionChecker.class);
|
||||
|
||||
private Class<?> clazz;
|
||||
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 });
|
||||
clazz = Thread.currentThread().getContextClassLoader().loadClass("oracle.jdbc.driver.OracleConnection");
|
||||
ping = clazz.getMethod("pingDatabase", new Class[] { Integer.TYPE });
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to resolve pingDatabase method:", e);
|
||||
}
|
||||
@@ -52,6 +52,11 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
|
||||
|
||||
public boolean isValidConnection(Connection c) {
|
||||
try {
|
||||
Connection conn = (Connection) c.unwrap(clazz);
|
||||
if (conn != null) {
|
||||
c = conn;
|
||||
}
|
||||
|
||||
Integer status = (Integer) ping.invoke(c, params);
|
||||
|
||||
// Error
|
||||
|
||||
Reference in New Issue
Block a user