mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve compatible for DBCP
This commit is contained in:
@@ -106,6 +106,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
protected volatile String password;
|
||||
protected volatile String jdbcUrl;
|
||||
protected volatile String driverClass;
|
||||
protected volatile ClassLoader driverClassLoader;
|
||||
protected volatile Properties connectProperties = new Properties();
|
||||
|
||||
protected volatile PasswordCallback passwordCallback;
|
||||
@@ -944,6 +945,14 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
this.driverClass = driverClass;
|
||||
}
|
||||
|
||||
public ClassLoader getDriverClassLoader() {
|
||||
return driverClassLoader;
|
||||
}
|
||||
|
||||
public void setDriverClassLoader(ClassLoader driverClassLoader) {
|
||||
this.driverClassLoader = driverClassLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getLogWriter() {
|
||||
return logWriter;
|
||||
@@ -1499,4 +1508,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
// just for code format
|
||||
public final static class ActiveConnectionTraceInfoMap extends ConcurrentIdentityHashMap<DruidPooledConnection, ActiveConnectionTraceInfo> {
|
||||
}
|
||||
|
||||
public abstract void discardConnection(Connection realConnection);
|
||||
}
|
||||
|
||||
@@ -379,7 +379,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (MockDriver.class.getName().equals(driverClass)) {
|
||||
driver = MockDriver.instance;
|
||||
} else {
|
||||
driver = JdbcUtils.createDriver(driverClass);
|
||||
driver = JdbcUtils.createDriver(driverClassLoader, driverClass);
|
||||
}
|
||||
} else {
|
||||
if (this.driverClass == null) {
|
||||
@@ -699,7 +699,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
* @param realConnection
|
||||
* @throws SQLException
|
||||
*/
|
||||
private void discardConnection(Connection realConnection) {
|
||||
public void discardConnection(Connection realConnection) {
|
||||
JdbcUtils.close(realConnection);
|
||||
|
||||
lock.lock();
|
||||
|
||||
@@ -109,7 +109,13 @@ public class DruidDataSourceFactory implements ObjectFactory {
|
||||
}
|
||||
}
|
||||
|
||||
return createDataSource(properties);
|
||||
return createDataSourceInternal(properties);
|
||||
}
|
||||
|
||||
protected DataSource createDataSourceInternal(Properties properties) throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
config(dataSource, properties);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
|
||||
@@ -165,6 +165,22 @@ public class DruidPooledConnection implements javax.sql.PooledConnection, Connec
|
||||
public boolean isDisable() {
|
||||
return disable;
|
||||
}
|
||||
|
||||
public void discard() {
|
||||
if (this.disable) {
|
||||
return;
|
||||
}
|
||||
|
||||
DruidConnectionHolder holder = this.holder;
|
||||
if (holder == null) {
|
||||
if (dupCloseLogEnable) {
|
||||
LOG.error("dup close");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
holder.getDataSource().discardConnection(holder.getConnection());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
|
||||
@@ -497,8 +497,24 @@ public final class JdbcUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Driver createDriver(String driverClassName) throws SQLException {
|
||||
return createDriver(null, driverClassName);
|
||||
}
|
||||
|
||||
public static Driver createDriver(ClassLoader classLoader, String driverClassName) throws SQLException {
|
||||
if (classLoader != null) {
|
||||
try {
|
||||
return (Driver) classLoader.loadClass(driverClassName).newInstance();
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return (Driver) Class.forName(driverClassName).newInstance();
|
||||
} catch (IllegalAccessException e) {
|
||||
|
||||
Reference in New Issue
Block a user