HAPoolTest

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@816 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-09-16 10:22:06 +00:00
parent ae6e7e45d8
commit 81a9557fa2
5 changed files with 83 additions and 2 deletions
@@ -149,6 +149,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.unlock();
}
}
public boolean isInited() {
return this.inited;
}
private void init() throws SQLException {
if (inited) {
@@ -9,12 +9,16 @@ import java.util.concurrent.CopyOnWriteArrayList;
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 {
private final static Log LOG = LogFactory.getLog(HADataSource.class);
protected ArrayList<DruidDataSource> dataSources = new ArrayList<DruidDataSource>();
protected final List<DruidDataSource> notAvailableDatasources = new CopyOnWriteArrayList<DruidDataSource>();
@@ -28,9 +32,15 @@ public class HADataSource extends MultiDataSource implements DataSource {
return Collections.unmodifiableList(dataSources);
}
public void setDataSources(List<DruidDataSource> dataSources) {
public synchronized void setDataSources(List<DruidDataSource> dataSources) {
this.dataSources = new ArrayList<DruidDataSource>(dataSources);
}
public synchronized void addDataSource(DruidDataSource dataSource) {
ArrayList<DruidDataSource> newList = new ArrayList<DruidDataSource>(dataSources);
newList.add(dataSource);
this.dataSources = newList;
}
@Override
public Connection getConnection() throws SQLException {
@@ -56,4 +66,21 @@ public class HADataSource extends MultiDataSource implements DataSource {
}
}
public void close() {
for (DruidDataSource item : dataSources) {
try {
item.close();
} catch (Exception ex) {
LOG.error("close dataSource error", ex);
}
}
for (DruidDataSource item : notAvailableDatasources) {
try {
item.close();
} catch (Exception ex) {
LOG.error("close dataSource error", ex);
}
}
}
}
@@ -51,7 +51,7 @@ public class RoundRobinBlancer implements Balancer {
assert dataSource != null;
if (!dataSource.isEnable()) {
if (dataSource.isInited() && !dataSource.isEnable()) {
multiDataSource.handleNotAwailableDatasource(dataSource);
continue;
}
@@ -92,6 +92,11 @@ public class TestDataSourceBasic extends TestCase {
public void test_wrap() throws Exception {
Assert.assertTrue(!dataSource.isWrapperFor(Date.class));
Assert.assertTrue(!dataSource.isWrapperFor(null));
Assert.assertTrue(dataSource.isWrapperFor(DataSource.class));
Assert.assertTrue(dataSource.unwrap(Date.class) == null);
Assert.assertTrue(dataSource.unwrap(null) == null);
Assert.assertTrue(dataSource.unwrap(DataSource.class) != null);
}
}
@@ -0,0 +1,45 @@
package com.alibaba.druid.bvt.pool.ha;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.ha.HADataSource;
public class HADataSourceTest extends TestCase {
private DruidDataSource dataSourceA;
private DruidDataSource dataSourceB;
private HADataSource dataSourceHA;
protected void setUp() throws Exception {
dataSourceA = new DruidDataSource();
dataSourceA.setUrl("jdbc:mock:x1");
dataSourceB = new DruidDataSource();
dataSourceB.setUrl("jdbc:mock:x1");
dataSourceHA = new HADataSource();
dataSourceHA.addDataSource(dataSourceA);
dataSourceHA.addDataSource(dataSourceB);
}
protected void tearDown() throws Exception {
dataSourceHA.close();
}
public void test_connect_close() throws Exception {
Connection conn = dataSourceHA.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
rs.close();
stmt.close();
conn.close();
}
}