mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持HA
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@458 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -27,6 +27,10 @@ public class JakartaCommonsLoggingImpl implements com.alibaba.druid.logging.Log
|
||||
public void debug(String s) {
|
||||
log.debug(s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.debug(s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.warn(s);
|
||||
|
||||
@@ -26,6 +26,10 @@ public class Jdk14LoggingImpl implements Log {
|
||||
public void debug(String s) {
|
||||
log.log(Level.FINE, s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.log(Level.FINE, s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.log(Level.WARNING, s);
|
||||
|
||||
@@ -9,6 +9,8 @@ public interface Log {
|
||||
void error(String s);
|
||||
|
||||
public void debug(String s);
|
||||
|
||||
public void debug(String s, Throwable e);
|
||||
|
||||
public void warn(String s);
|
||||
|
||||
|
||||
@@ -25,6 +25,10 @@ public class Log4jImpl implements Log {
|
||||
public void debug(String s) {
|
||||
log.debug(s);
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
log.debug(s, e);
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
log.warn(s);
|
||||
|
||||
@@ -17,6 +17,9 @@ public class NoLoggingImpl implements Log {
|
||||
|
||||
public void debug(String s) {
|
||||
}
|
||||
|
||||
public void debug(String s, Throwable e) {
|
||||
}
|
||||
|
||||
public void warn(String s) {
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user