mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -3,28 +3,36 @@ 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;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.balance.Balancer;
|
||||
import com.alibaba.druid.pool.ha.balance.RoundRobinBlancer;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements DataSource {
|
||||
public class HADataSource extends MultiDataSource implements HADataSourceMBean, 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 Balancer balancer = new RoundRobinBlancer();
|
||||
|
||||
public HADataSource(){
|
||||
|
||||
}
|
||||
|
||||
public long getMasterConnectCount() {
|
||||
return masterConnectCount.get();
|
||||
}
|
||||
|
||||
public long getSlaveConnectCount() {
|
||||
return slaveConnectCount.get();
|
||||
}
|
||||
|
||||
public DruidDataSource getMaster() {
|
||||
return master;
|
||||
}
|
||||
@@ -43,20 +51,59 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
this.slave = slave;
|
||||
}
|
||||
|
||||
public boolean isMasterEnable() {
|
||||
if (master == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return master.isEnable();
|
||||
}
|
||||
|
||||
public void setMasterEnable(boolean value) {
|
||||
if (master == null) {
|
||||
throw new IllegalStateException("slave is null");
|
||||
}
|
||||
|
||||
master.setEnable(value);
|
||||
}
|
||||
|
||||
public boolean isSlaveEnable() {
|
||||
if (slave == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return slave.isEnable();
|
||||
}
|
||||
|
||||
public void setSlaveEnable(boolean value) {
|
||||
if (slave == null) {
|
||||
throw new IllegalStateException("slave is null");
|
||||
}
|
||||
|
||||
slave.setEnable(value);
|
||||
}
|
||||
|
||||
public synchronized void setDataSources(List<DruidDataSource> dataSources) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Balancer getBalancer() {
|
||||
return balancer;
|
||||
}
|
||||
|
||||
public void setBalancer(Balancer balancer) {
|
||||
this.balancer = balancer;
|
||||
}
|
||||
|
||||
public Connection getConnectionInternal(MultiDataSourceConnection connection, String sql) throws SQLException {
|
||||
return this.balancer.getConnection(connection, sql);
|
||||
Connection conn = null;
|
||||
if (master.isEnable()) {
|
||||
conn = master.getConnection();
|
||||
masterConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (slave.isEnable()) {
|
||||
conn = slave.getConnection();
|
||||
slaveConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (conn == null) {
|
||||
throw new SQLException("get HAConnection error");
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
public void handleNotAwailableDatasource(DruidDataSource dataSource) {
|
||||
|
||||
Reference in New Issue
Block a user