mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
HADataSource implementation.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
@@ -60,7 +61,7 @@ import com.alibaba.druid.util.TransactionInfo;
|
||||
* @author ljw<ljw2083@alibaba-inc.com>
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
*/
|
||||
public class DruidDataSource extends DruidAbstractDataSource implements DruidDataSourceMBean, ManagedDataSource, Referenceable {
|
||||
public class DruidDataSource extends DruidAbstractDataSource implements DruidDataSourceMBean, ManagedDataSource, Referenceable, Closeable {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSource.class);
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ package com.alibaba.druid.pool.ha;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
import java.util.RandomAccess;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -14,17 +14,20 @@ 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;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements DataSource {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
|
||||
protected DruidDataSource master;
|
||||
protected ArrayList<DruidDataSource> dataSources = new ArrayList<DruidDataSource>();
|
||||
protected DruidDataSource slave;
|
||||
protected final List<DruidDataSource> notAvailableDatasources = new CopyOnWriteArrayList<DruidDataSource>();
|
||||
|
||||
protected Balancer balancer = new RoundRobinBlancer();
|
||||
|
||||
private DataSourceList dataSourceList = new DataSourceList();
|
||||
|
||||
public HADataSource(){
|
||||
|
||||
}
|
||||
@@ -37,18 +40,20 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public DruidDataSource getSlave() {
|
||||
return slave;
|
||||
}
|
||||
|
||||
public void setSlave(DruidDataSource slave) {
|
||||
this.slave = slave;
|
||||
}
|
||||
|
||||
public List<DruidDataSource> getDataSources() {
|
||||
return Collections.unmodifiableList(dataSources);
|
||||
return dataSourceList;
|
||||
}
|
||||
|
||||
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;
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,27 +74,63 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
}
|
||||
|
||||
public void handleNotAwailableDatasource(DruidDataSource dataSource) {
|
||||
boolean removed = dataSources.remove(dataSource);
|
||||
if (removed) {
|
||||
notAvailableDatasources.add(dataSource);
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("HADataSource closed");
|
||||
}
|
||||
|
||||
JdbcUtils.close(master);
|
||||
}
|
||||
|
||||
private class DataSourceList extends AbstractList<DruidDataSource> implements RandomAccess, java.io.Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2764017481108945198L;
|
||||
|
||||
public int size() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
return new DruidDataSource[] { master, slave };
|
||||
}
|
||||
|
||||
public <T> T[] toArray(T[] a) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public DruidDataSource get(int index) {
|
||||
if (index == 0) {
|
||||
return master;
|
||||
}
|
||||
|
||||
if (index == 1) {
|
||||
return slave;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("index : " + index);
|
||||
}
|
||||
|
||||
public DruidDataSource set(int index, DruidDataSource element) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public int indexOf(Object o) {
|
||||
if (o == master) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (o == slave) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return o == master || o == slave;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public class HADataSourceTest extends TestCase {
|
||||
dataSourceB.setFilters("stat");
|
||||
|
||||
dataSourceHA = new HADataSource();
|
||||
dataSourceHA.addDataSource(dataSourceA);
|
||||
dataSourceHA.addDataSource(dataSourceB);
|
||||
dataSourceHA.setMaster(dataSourceA);
|
||||
dataSourceHA.setSlave(dataSourceB);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user