mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -99,7 +99,7 @@ public class HADataSource extends MultiDataSource implements HADataSourceMBean,
|
||||
masterConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
if (slave.isEnable()) {
|
||||
if (conn == null && slave.isEnable()) {
|
||||
conn = slave.getConnection();
|
||||
slaveConnectCount.incrementAndGet();
|
||||
}
|
||||
|
||||
@@ -579,6 +579,26 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
public Date getConnectedTime() {
|
||||
return connectedTime;
|
||||
}
|
||||
|
||||
public boolean isWrapperFor(Class<?> iface) throws SQLException {
|
||||
boolean result = super.isWrapperFor(iface);
|
||||
|
||||
if (result == false && conn != null) {
|
||||
result = conn.isWrapperFor(iface);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public <T> T unwrap(Class<T> iface) throws SQLException {
|
||||
T object = super.unwrap(iface);
|
||||
|
||||
if (object == null && conn != null) {
|
||||
object = conn.unwrap(iface);
|
||||
}
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
public void setSchema(String schema) throws SQLException {
|
||||
throw new SQLFeatureNotSupportedException();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.alibaba.druid.bvt.pool.ha;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Statement;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
@@ -12,41 +13,70 @@ import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
|
||||
public class HADataSourceTest2 extends TestCase {
|
||||
|
||||
private DruidDataSource dataSourceA;
|
||||
private DruidDataSource dataSourceB;
|
||||
private DruidDataSource dataSourceA;
|
||||
private DruidDataSource dataSourceB;
|
||||
|
||||
private HADataSource dataSourceHA;
|
||||
private HADataSource dataSourceHA;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
|
||||
dataSourceA = new DruidDataSource();
|
||||
dataSourceA.setUrl("jdbc:mock:ha1");
|
||||
dataSourceA.setFilters("trace");
|
||||
private String MASTER_URL = "jdbc:mock:master";
|
||||
private String SLAVE_URL = "jdbc:mock:slave";
|
||||
|
||||
dataSourceB = new DruidDataSource();
|
||||
dataSourceB.setUrl("jdbc:mock:ha2");
|
||||
dataSourceB.setFilters("stat");
|
||||
protected void setUp() throws Exception {
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance()
|
||||
.getDataSourceList().size());
|
||||
|
||||
dataSourceHA = new HADataSource();
|
||||
dataSourceHA.setMaster(dataSourceA);
|
||||
dataSourceHA.setSlave(dataSourceB);
|
||||
}
|
||||
dataSourceA = new DruidDataSource();
|
||||
dataSourceA.setUrl(MASTER_URL);
|
||||
dataSourceA.setFilters("trace");
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSourceHA.close();
|
||||
|
||||
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance().getDataSourceList().size());
|
||||
}
|
||||
dataSourceB = new DruidDataSource();
|
||||
dataSourceB.setUrl(SLAVE_URL);
|
||||
dataSourceB.setFilters("stat");
|
||||
|
||||
public void test_switch() throws Exception {
|
||||
Connection conn = dataSourceHA.getConnection();
|
||||
|
||||
MockConnection mockConn = conn.unwrap(MockConnection.class);
|
||||
conn.close();
|
||||
}
|
||||
dataSourceHA = new HADataSource();
|
||||
dataSourceHA.setMaster(dataSourceA);
|
||||
dataSourceHA.setSlave(dataSourceB);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
dataSourceHA.close();
|
||||
|
||||
for (DruidDataSource dataSource : DruidDataSourceStatManager
|
||||
.getDruidDataSourceInstances()) {
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
Assert.assertEquals(0, DruidDataSourceStatManager.getInstance()
|
||||
.getDataSourceList().size());
|
||||
}
|
||||
|
||||
public void test_switch() throws Exception {
|
||||
{
|
||||
Connection conn = dataSourceHA.getConnection();
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute("select 1");
|
||||
|
||||
MockConnection mockConn = conn.unwrap(MockConnection.class);
|
||||
Assert.assertNotNull(mockConn);
|
||||
|
||||
Assert.assertEquals(dataSourceA.getUrl(), mockConn.getUrl());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
dataSourceHA.setMasterEnable(false);
|
||||
{
|
||||
Connection conn = dataSourceHA.getConnection();
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute("select 1");
|
||||
|
||||
MockConnection mockConn = conn.unwrap(MockConnection.class);
|
||||
Assert.assertNotNull(mockConn);
|
||||
|
||||
Assert.assertEquals(dataSourceB.getUrl(), mockConn.getUrl());
|
||||
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user