添加ExceptionSorter

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@309 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-06-29 09:14:51 +00:00
parent fc538c954a
commit cdabd396a4
4 changed files with 57 additions and 21 deletions
@@ -106,7 +106,7 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
protected PrintWriter logWriter = new PrintWriter(System.out);
protected List<Filter> filters = new ArrayList<Filter>();
protected List<ExceptionSorter> exceptionSoters = new ArrayList<ExceptionSorter>();
protected ExceptionSorter exceptionSoter = null;
protected Driver driver;
@@ -142,6 +142,8 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
protected ValidConnectionChecker validConnectionChecker = null;
protected final AtomicLong errorCount = new AtomicLong();
public ValidConnectionChecker getValidConnectionChecker() {
return validConnectionChecker;
}
@@ -636,28 +638,28 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
this.driver = driver;
}
public List<ExceptionSorter> getExceptionSoters() {
return exceptionSoters;
public ExceptionSorter getExceptionSoter() {
return exceptionSoter;
}
public void setExceptionSoters(List<ExceptionSorter> exceptionSoters) {
this.exceptionSoters = exceptionSoters;
public void setExceptionSoter(ExceptionSorter exceptionSoter) {
this.exceptionSoter = exceptionSoter;
}
public void setExceptionSoters(String exceptionSorters) throws Exception {
this.exceptionSoters.clear();
if (exceptionSorters == null || exceptionSorters.length() == 0) {
public void setExceptionSoter(String exceptionSorter) throws Exception {
if (exceptionSorter == null) {
this.exceptionSoter = null;
return;
}
String[] sorterArray = exceptionSorters.split("\\,");
for (String item : sorterArray) {
Class<?> clazz = DruidLoaderUtils.loadClass(item);
ExceptionSorter sorter = (ExceptionSorter) clazz.newInstance();
exceptionSoters.add(sorter);
exceptionSorter = exceptionSorter.trim();
if (exceptionSorter.length() == 0) {
this.exceptionSoter = null;
return;
}
Class<?> clazz = DruidLoaderUtils.loadClass(exceptionSorter);
this.exceptionSoter = (ExceptionSorter) clazz.newInstance();
}
@Override
@@ -805,6 +807,10 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
}
}
protected void handleException(Throwable e) throws SQLException {
errorCount.incrementAndGet();
}
protected abstract void recycle(PoolableConnection pooledConnection) throws SQLException;
abstract void incrementCreateCount();
@@ -265,6 +265,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
return poolalbeConnection;
}
/**
* 回收连接
*/
@@ -40,6 +40,7 @@ import javax.sql.StatementEventListener;
import com.alibaba.druid.pool.PoolablePreparedStatement.PreparedStatementKey;
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
import com.alibaba.druid.pool.vendor.ExceptionSorter;
/**
* @author wenshao<szujobs@hotmail.com>
@@ -54,12 +55,40 @@ public class PoolableConnection implements PooledConnection, Connection {
this.holder = holder;
}
protected SQLException checkException(Throwable t) throws SQLException {
//if (mc != null) mc.connectionError(t);
protected SQLException handleException(Throwable t) throws SQLException {
final ConnectionHolder holder = this.holder;
//
if (holder != null) {
DruidAbstractDataSource dataSource = holder.getDataSource();
if (dataSource != null) {
holder.getDataSource().handleException(t);
}
}
if (t instanceof SQLException) {
throw (SQLException) t;
} else {
SQLException sqlEx = (SQLException) t;
// broadcastConnectionError
ConnectionEvent event = new ConnectionEvent(this, sqlEx);
for (ConnectionEventListener eventListener : holder.getConnectionEventListeners()) {
eventListener.connectionErrorOccurred(event);
}
// exceptionSorter.isExceptionFatal
if (holder != null) {
DruidAbstractDataSource dataSource = holder.getDataSource();
if (dataSource != null) {
ExceptionSorter exceptionSorter = dataSource.getExceptionSoter();
if (exceptionSorter != null && exceptionSorter.isExceptionFatal(sqlEx)) {
this.holder = null; // isClosed() is true
}
}
}
throw sqlEx;
} else {
throw new SQLException("Error", t);
}
}
@@ -38,7 +38,7 @@ public class PoolableStatement extends PoolableWrapper implements Statement {
}
protected SQLException checkException(Throwable error) throws SQLException {
return conn.checkException(error);
return conn.handleException(error);
}
public PoolableConnection getPoolableConnection() {