mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持HA
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@456 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -39,6 +39,7 @@ import javax.sql.DataSource;
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.filter.FilterChainImpl;
|
||||
import com.alibaba.druid.pool.vendor.NullExceptionSorter;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.util.DruidLoaderUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
@@ -730,8 +731,20 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean testConnection(Connection conn) {
|
||||
if (conn instanceof PoolableConnection) {
|
||||
conn = ((PoolableConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getConnectionRaw();
|
||||
}
|
||||
|
||||
return testConnectionInternal(conn);
|
||||
}
|
||||
|
||||
protected boolean testConnection(Connection conn) {
|
||||
protected boolean testConnectionInternal(Connection conn) {
|
||||
try {
|
||||
if (validConnectionChecker != null) {
|
||||
return validConnectionChecker.isValidConnection(conn);
|
||||
|
||||
@@ -272,7 +272,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
PoolableConnection poolalbeConnection = getConnectionInternal();
|
||||
|
||||
if (isTestOnBorrow()) {
|
||||
boolean validate = testConnection(poolalbeConnection.getConnection());
|
||||
boolean validate = testConnectionInternal(poolalbeConnection.getConnection());
|
||||
if (!validate) {
|
||||
LOG.debug("skip not validate connection.");
|
||||
Connection realConnection = poolalbeConnection.getConnection();
|
||||
@@ -393,7 +393,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
//
|
||||
if (isTestOnReturn()) {
|
||||
boolean validate = testConnection(conn);
|
||||
boolean validate = testConnectionInternal(conn);
|
||||
if (!validate) {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -753,7 +753,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
for (ConnectionHolder idleConnection : idleConnections) {
|
||||
if (testConnection(idleConnection.getConnection())) {
|
||||
if (testConnectionInternal(idleConnection.getConnection())) {
|
||||
connections[count++] = idleConnection;
|
||||
idleConnection.setLastActiveMillis(System.currentTimeMillis());
|
||||
idleCheckCount++;
|
||||
|
||||
@@ -109,7 +109,7 @@ public class PoolableConnection implements PooledConnection, Connection {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
public Connection getConnection() {
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,24 +6,48 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements DataSource {
|
||||
|
||||
private AtomicInteger requestCount = new AtomicInteger();
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
|
||||
private AtomicInteger requestCount = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
int requestNumber = requestCount.getAndIncrement();
|
||||
int size = dataSources.size();
|
||||
int index = requestNumber % size;
|
||||
int tryCount = 0;
|
||||
for (;;) {
|
||||
int requestNumber = requestCount.getAndIncrement();
|
||||
int size = dataSources.size();
|
||||
int index = requestNumber % size;
|
||||
|
||||
DruidDataSource dataSource = dataSources.get(index);
|
||||
Connection conn = dataSource.getConnection();
|
||||
DruidDataSource dataSource = dataSources.get(index);
|
||||
Connection conn = null;
|
||||
|
||||
return conn;
|
||||
try {
|
||||
tryCount++;
|
||||
conn = dataSource.getConnection();
|
||||
} catch (SQLException ex) {
|
||||
LOG.error("getConnection error", ex);
|
||||
|
||||
if (tryCount >= size) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (conn != null) {
|
||||
if (!dataSource.isTestOnBorrow()) {
|
||||
dataSource.testConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user