mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -181,6 +181,7 @@ public class DruidDataSourceC3P0Adapter implements DataSource, DruidDataSourceC3
|
||||
|
||||
public void setMaxPoolSize(int maxPoolSize) {
|
||||
dataSource.setMaxActive(maxPoolSize);
|
||||
dataSource.setMaxIdle(maxPoolSize);
|
||||
}
|
||||
|
||||
public int getMinPoolSize() {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.alibaba.druid.pool.ha;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DataSourceHolder {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
private boolean enable;
|
||||
private final AtomicLong connectCount = new AtomicLong();
|
||||
|
||||
public DataSourceHolder(DruidDataSource dataSource){
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public void resetStat() {
|
||||
connectCount.set(0);
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public DruidDataSource getDataSource() {
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public void setDataSource(DruidDataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public long getConnectCount() {
|
||||
return connectCount.get();
|
||||
}
|
||||
|
||||
public void incrementConnectCount() {
|
||||
connectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
if (dataSource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return dataSource.getUrl();
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
connectCount.incrementAndGet();
|
||||
return dataSource.getConnection();
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package com.alibaba.druid.pool.ha;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -14,47 +13,52 @@ import com.alibaba.druid.pool.ManagedDataSource;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements HADataSourceMBean, ManagedDataSource, DataSource {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
|
||||
private final AtomicLong masterConnectCount = new AtomicLong();
|
||||
private final AtomicLong slaveConnectCount = new AtomicLong();
|
||||
|
||||
protected DruidDataSource master;
|
||||
protected DruidDataSource slave;
|
||||
protected DataSourceHolder master;
|
||||
protected DataSourceHolder slave;
|
||||
|
||||
public HADataSource(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void resetStat() {
|
||||
super.resetStat();
|
||||
|
||||
masterConnectCount.set(0);
|
||||
slaveConnectCount.set(0);
|
||||
|
||||
master.resetStat();
|
||||
slave.resetStat();
|
||||
}
|
||||
|
||||
public long getMasterConnectCount() {
|
||||
return masterConnectCount.get();
|
||||
return master.getConnectCount();
|
||||
}
|
||||
|
||||
public long getSlaveConnectCount() {
|
||||
return slaveConnectCount.get();
|
||||
return slave.getConnectCount();
|
||||
}
|
||||
|
||||
public DruidDataSource getMaster() {
|
||||
public DataSourceHolder getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(DruidDataSource master) {
|
||||
this.setMaster(new DataSourceHolder(master));
|
||||
}
|
||||
|
||||
public void setMaster(DataSourceHolder master) {
|
||||
this.getDataSources().put("master", master);
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public DruidDataSource getSlave() {
|
||||
public DataSourceHolder getSlave() {
|
||||
return slave;
|
||||
}
|
||||
|
||||
public void setSlave(DruidDataSource slave) {
|
||||
this.setSlave(new DataSourceHolder(slave));
|
||||
}
|
||||
|
||||
public void setSlave(DataSourceHolder slave) {
|
||||
this.getDataSources().put("slave", slave);
|
||||
this.slave = slave;
|
||||
}
|
||||
@@ -74,12 +78,12 @@ public class HADataSource extends MultiDataSource implements HADataSourceMBean,
|
||||
|
||||
master.setEnable(value);
|
||||
}
|
||||
|
||||
|
||||
public String getMasterUrl() {
|
||||
if (master == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return master.getUrl();
|
||||
}
|
||||
|
||||
@@ -87,10 +91,10 @@ public class HADataSource extends MultiDataSource implements HADataSourceMBean,
|
||||
if (slave == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return slave.getUrl();
|
||||
}
|
||||
|
||||
|
||||
public boolean isSlaveEnable() {
|
||||
if (slave == null) {
|
||||
return false;
|
||||
@@ -106,11 +110,11 @@ public class HADataSource extends MultiDataSource implements HADataSourceMBean,
|
||||
|
||||
slave.setEnable(value);
|
||||
}
|
||||
|
||||
|
||||
public void switchMasterSlave() {
|
||||
DruidDataSource tmp = this.getMaster();
|
||||
this.setMaster(this.getSlave());
|
||||
this.setSlave(tmp);
|
||||
DataSourceHolder tmp = this.getMaster();
|
||||
this.setMaster(this.getSlave());
|
||||
this.setSlave(tmp);
|
||||
}
|
||||
|
||||
public synchronized void setDataSources(List<DruidDataSource> dataSources) {
|
||||
@@ -121,12 +125,10 @@ public class HADataSource extends MultiDataSource implements HADataSourceMBean,
|
||||
Connection conn = null;
|
||||
if (master.isEnable()) {
|
||||
conn = master.getConnection();
|
||||
masterConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (conn == null && slave.isEnable()) {
|
||||
conn = slave.getConnection();
|
||||
slaveConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (conn == null) {
|
||||
@@ -135,7 +137,7 @@ public class HADataSource extends MultiDataSource implements HADataSourceMBean,
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
super.close();
|
||||
if (LOG.isDebugEnabled()) {
|
||||
|
||||
@@ -21,8 +21,8 @@ import javax.management.ObjectName;
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.pool.DataSourceAdapter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.valid.DefaultDataSourceFailureDetecter;
|
||||
import com.alibaba.druid.pool.ha.valid.DataSourceFailureDetecter;
|
||||
import com.alibaba.druid.pool.ha.valid.DefaultDataSourceFailureDetecter;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
@@ -44,7 +44,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
private boolean inited = false;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
private ConcurrentMap<String, DruidDataSource> dataSources = new ConcurrentHashMap<String, DruidDataSource>();
|
||||
private ConcurrentMap<String, DataSourceHolder> dataSources = new ConcurrentHashMap<String, DataSourceHolder>();
|
||||
|
||||
private ObjectName objectName;
|
||||
|
||||
@@ -127,10 +127,10 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
}
|
||||
|
||||
public void failureDetect() {
|
||||
for (DruidDataSource dataSource : getDataSources().values()) {
|
||||
boolean isValid = validDataSourceChecker.isValid(dataSource);
|
||||
for (DataSourceHolder dataSourceHolder : getDataSources().values()) {
|
||||
boolean isValid = validDataSourceChecker.isValid(dataSourceHolder.getDataSource());
|
||||
if (!isValid) {
|
||||
handleNotAwailableDatasource(dataSource);
|
||||
handleNotAwailableDatasource(dataSourceHolder);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -163,7 +163,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
return statementIdSeed.incrementAndGet();
|
||||
}
|
||||
|
||||
public Map<String, DruidDataSource> getDataSources() {
|
||||
public Map<String, DataSourceHolder> getDataSources() {
|
||||
return dataSources;
|
||||
}
|
||||
|
||||
@@ -179,8 +179,8 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
|
||||
public abstract Connection getConnectionInternal(MultiDataSourceConnection conn, String sql) throws SQLException;
|
||||
|
||||
public void handleNotAwailableDatasource(DruidDataSource dataSource) {
|
||||
dataSource.setEnable(false);
|
||||
public void handleNotAwailableDatasource(DataSourceHolder dataSourceHolder) {
|
||||
dataSourceHolder.setEnable(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.DataSourceHolder;
|
||||
import com.alibaba.druid.pool.ha.MultiDataSource;
|
||||
import com.alibaba.druid.pool.ha.MultiDataSourceConnection;
|
||||
|
||||
@@ -38,11 +38,11 @@ public class RoundRobinBlancer implements Balancer {
|
||||
|
||||
int index = (int) (connectionId % size);
|
||||
|
||||
DruidDataSource dataSource = null;
|
||||
DataSourceHolder dataSource = null;
|
||||
|
||||
try {
|
||||
// 处理并发时的错误
|
||||
List<DruidDataSource> dataSources = new ArrayList<DruidDataSource>(multiDataSource.getDataSources().values());
|
||||
List<DataSourceHolder> dataSources = new ArrayList<DataSourceHolder>(multiDataSource.getDataSources().values());
|
||||
dataSource = dataSources.get(index);
|
||||
} catch (Exception ex) {
|
||||
indexErrorCount.incrementAndGet();
|
||||
@@ -54,7 +54,7 @@ public class RoundRobinBlancer implements Balancer {
|
||||
|
||||
assert dataSource != null;
|
||||
|
||||
if (dataSource.isInited() && !dataSource.isEnable()) {
|
||||
if (!dataSource.isEnable()) {
|
||||
multiDataSource.handleNotAwailableDatasource(dataSource);
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user