git-svn-id: http://code.alibabatech.com/svn/druid/trunk@458 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-07-17 05:34:30 +00:00
parent d85c4b94f6
commit 91c276c92c
8 changed files with 125 additions and 75 deletions
@@ -0,0 +1,72 @@
package com.alibaba.druid.pool;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
public class DataSourceAdapter implements DataSource {
private int loginTimeout = 0;
private PrintWriter logWriter = new PrintWriter(System.out);
public DataSourceAdapter(){
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return logWriter;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
this.logWriter = out;
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
loginTimeout = seconds;
}
@Override
public int getLoginTimeout() throws SQLException {
return loginTimeout;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (iface == null) {
return false;
}
if (iface.isInstance(this)) {
return true;
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == null) {
return null;
}
if (iface.isInstance(this)) {
return (T) this;
}
return null;
}
@Override
public Connection getConnection() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
throw new UnsupportedOperationException();
}
}
@@ -2,7 +2,6 @@ package com.alibaba.druid.pool.ha;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
@@ -17,14 +16,14 @@ public class HADataSource extends MultiDataSource implements DataSource {
private final static Log LOG = LogFactory.getLog(HADataSource.class);
private AtomicInteger requestCount = new AtomicInteger();
private final AtomicInteger requestCount = new AtomicInteger();
private final AtomicInteger indexErrorCount = new AtomicInteger();
protected final List<DruidDataSource> notAvailableDatasources = new CopyOnWriteArrayList<DruidDataSource>();
public HADataSource() {
super(new ArrayList<DruidDataSource>());
public HADataSource(){
}
@Override
public Connection getConnection() throws SQLException {
int tryCount = 0;
@@ -32,15 +31,34 @@ public class HADataSource extends MultiDataSource implements DataSource {
for (;;) {
int size = dataSources.size();
if (size == 0) {
throw new SQLException("can not get connection, no availabe datasources");
}
int index = requestNumber % size;
DruidDataSource dataSource = dataSources.get(index);
DruidDataSource dataSource = null;
try {
// 处理并发时的错误
dataSource = dataSources.get(index);
} catch (Exception ex) {
indexErrorCount.incrementAndGet();
if (LOG.isDebugEnabled()) {
LOG.debug("getDataSource error, index : " + index, ex);
}
continue;
}
assert dataSource != null;
if (!dataSource.isEnable()) {
boolean removed = dataSources.remove(dataSource);
if (removed) {
notAvailableDatasources.add(dataSource);
}
continue;
}
Connection conn = null;
@@ -62,4 +80,8 @@ public class HADataSource extends MultiDataSource implements DataSource {
}
}
public long getIndexErrorCount() {
return indexErrorCount.get();
}
}
@@ -1,83 +1,22 @@
package com.alibaba.druid.pool.ha;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.sql.DataSource;
import java.util.ArrayList;
import com.alibaba.druid.pool.DataSourceAdapter;
import com.alibaba.druid.pool.DruidDataSource;
public abstract class MultiDataSource implements DataSource {
public abstract class MultiDataSource extends DataSourceAdapter {
protected final List<DruidDataSource> dataSources;
protected ArrayList<DruidDataSource> dataSources;
private int loginTimeout = 0;
private PrintWriter logWriter = new PrintWriter(System.out);
public MultiDataSource() {
dataSources = new CopyOnWriteArrayList<DruidDataSource>();
}
public MultiDataSource(List<DruidDataSource> dataSources) {
this.dataSources = dataSources;
}
public List<DruidDataSource> getDataSources() {
return dataSources;
public MultiDataSource(){
dataSources = new ArrayList<DruidDataSource>();
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return logWriter;
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
this.logWriter = out;
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
loginTimeout = seconds;
}
@Override
public int getLoginTimeout() throws SQLException {
return loginTimeout;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
if (iface == null) {
return false;
}
if (iface.isInstance(this)) {
return true;
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == null) {
return null;
}
if (iface.isInstance(this)) {
return (T) this;
}
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
public Connection getConnection() throws SQLException {
throw new UnsupportedOperationException();
}
}