mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
DruidDataSource支持Filter
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@49 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -98,7 +98,7 @@ public abstract class DruidDataAbstractSource implements DataSource, DataSourceP
|
||||
|
||||
protected PrintWriter logWriter = new PrintWriter(System.out);
|
||||
|
||||
private List<Filter> filters = new ArrayList<Filter>();
|
||||
protected List<Filter> filters = new ArrayList<Filter>();
|
||||
|
||||
protected Driver driver;
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.ArrayList;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@@ -33,6 +34,7 @@ import javax.naming.Reference;
|
||||
import javax.naming.Referenceable;
|
||||
import javax.naming.StringRefAddr;
|
||||
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
@@ -72,6 +74,8 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
|
||||
private final IdentityHashMap<PoolableConnection, ActiveConnectionTraceInfo> activeConnections = new IdentityHashMap<PoolableConnection, ActiveConnectionTraceInfo>();
|
||||
|
||||
private final CountDownLatch initedLatch = new CountDownLatch(2);
|
||||
|
||||
public DruidDataSource(){
|
||||
}
|
||||
|
||||
@@ -81,14 +85,6 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
}
|
||||
|
||||
try {
|
||||
final int maxWaitThreadCount = getMaxWaitThreadCount();
|
||||
if (maxWaitThreadCount > 0) {
|
||||
if (lock.getQueueLength() > maxWaitThreadCount) {
|
||||
throw new SQLException("maxWaitThreadCount " + maxWaitThreadCount + ", current wait Thread count "
|
||||
+ lock.getQueueLength());
|
||||
}
|
||||
}
|
||||
|
||||
lock.lockInterruptibly();
|
||||
|
||||
if (inited) {
|
||||
@@ -116,6 +112,10 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
}
|
||||
|
||||
for (Filter filter : filters) {
|
||||
filter.init(this);
|
||||
}
|
||||
|
||||
initConnectionFactory();
|
||||
|
||||
@@ -145,7 +145,12 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
createConnectionThread.start();
|
||||
destoryConnectionThread.start();
|
||||
|
||||
initedLatch.await();
|
||||
inited = true;
|
||||
|
||||
if (count == 0) {
|
||||
lowWater.signal();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
} finally {
|
||||
@@ -193,6 +198,14 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
public Connection getConnection() throws SQLException {
|
||||
init();
|
||||
|
||||
final int maxWaitThreadCount = getMaxWaitThreadCount();
|
||||
if (maxWaitThreadCount > 0) {
|
||||
if (lock.getQueueLength() > maxWaitThreadCount) {
|
||||
throw new SQLException("maxWaitThreadCount " + maxWaitThreadCount + ", current wait Thread count "
|
||||
+ lock.getQueueLength());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
lock.lockInterruptibly();
|
||||
|
||||
@@ -380,6 +393,10 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
|
||||
try {
|
||||
while (count == 0) {
|
||||
if (minIdle == 0) {
|
||||
lowWater.signal();
|
||||
}
|
||||
|
||||
notEmpty.await();
|
||||
}
|
||||
} catch (InterruptedException ie) {
|
||||
@@ -475,11 +492,15 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
}
|
||||
|
||||
public void run() {
|
||||
initedLatch.countDown();
|
||||
|
||||
for (;;) {
|
||||
// addLast
|
||||
lock.lock();
|
||||
try {
|
||||
if (count >= minIdle) {
|
||||
if (count == 0 && minIdle == 0 && lock.getWaitQueueLength(notEmpty) > 0) {
|
||||
// not wait
|
||||
} else if (count >= minIdle) {
|
||||
lowWater.await();
|
||||
}
|
||||
|
||||
@@ -509,6 +530,8 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
|
||||
}
|
||||
|
||||
public void run() {
|
||||
initedLatch.countDown();
|
||||
|
||||
for (;;) {
|
||||
// 从前面开始删除
|
||||
try {
|
||||
|
||||
@@ -348,7 +348,7 @@ public final class JdbcUtils {
|
||||
return "com.microsoft.jdbc.sqlserver.SQLServerDriver";
|
||||
} else if (rawUrl.startsWith("jdbc:jtds:")) {
|
||||
return "net.sourceforge.jtds.jdbc.Driver";
|
||||
} else if (rawUrl.startsWith("jdbc:fake:")) {
|
||||
} else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) {
|
||||
return "com.alibaba.druid.mock.MockDriver";
|
||||
} else if (rawUrl.startsWith("jdbc:postgresql:")) {
|
||||
return "org.postgresql.Driver";
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.JdbcConnectionStat;
|
||||
import com.alibaba.druid.stat.JdbcStatManager;
|
||||
|
||||
public class DruidDataSourceFilterTest extends TestCase {
|
||||
|
||||
@@ -26,4 +30,22 @@ public class DruidDataSourceFilterTest extends TestCase {
|
||||
|
||||
Assert.assertEquals(2, dataSource.getFilters().size());
|
||||
}
|
||||
|
||||
public void test_filter_3() throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:");
|
||||
|
||||
Assert.assertEquals(0, dataSource.getFilters().size());
|
||||
|
||||
dataSource.setFilters("stat");
|
||||
|
||||
JdbcStatManager.getInstance().reset();
|
||||
|
||||
Assert.assertEquals(0, JdbcStatManager.getInstance().getConnectionstat().getConnectCount());
|
||||
Assert.assertEquals(1, dataSource.getFilters().size());
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(1, JdbcStatManager.getInstance().getConnectionstat().getConnectCount());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user