mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修正回收连接时判断maxActive的策略
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@393 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -365,18 +365,18 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
|
||||
// 第四步,检查是符合MaxIdle的设定
|
||||
if (count >= maxIdle) {
|
||||
lock.lock();
|
||||
try {
|
||||
JdbcUtils.close(conn);
|
||||
destroyCount++;
|
||||
closeCount++;
|
||||
decrementActiveCount();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// if (count >= maxIdle) {
|
||||
// lock.lock();
|
||||
// try {
|
||||
// JdbcUtils.close(conn);
|
||||
// destroyCount++;
|
||||
// closeCount++;
|
||||
// decrementActiveCount();
|
||||
// } finally {
|
||||
// lock.unlock();
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
|
||||
//
|
||||
if (isTestOnReturn()) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.alibaba.druid.pool.benckmark;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
@@ -19,31 +21,31 @@ public class Case3 extends TestCase {
|
||||
private String user;
|
||||
private String password;
|
||||
private String driverClass;
|
||||
private int maxIdle = 40;
|
||||
private int maxActive = 50;
|
||||
private int maxWait = -1;
|
||||
private String validationQuery = null; //"SELECT 1";
|
||||
private int threadCount = 40;
|
||||
private int TEST_COUNT = 3;
|
||||
final int LOOP_COUNT = 1000 * 1;
|
||||
private boolean testOnBorrow = true;
|
||||
private String connectionProperties = "bigStringTryClob=true;clientEncoding=GBK;defaultRowPrefetch=50;serverEncoding=ISO-8859-1";
|
||||
private int maxIdle = 40;
|
||||
private int maxActive = 50;
|
||||
private int maxWait = -1;
|
||||
private String validationQuery = null; // "SELECT 1";
|
||||
private int threadCount = 5;
|
||||
private int TEST_COUNT = 3;
|
||||
final int LOOP_COUNT = 1000 * 1000;
|
||||
private boolean testOnBorrow = false;
|
||||
private String connectionProperties = "bigStringTryClob=true;clientEncoding=GBK;defaultRowPrefetch=50;serverEncoding=ISO-8859-1";
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
||||
user = "dragoon25";
|
||||
password = "dragoon25";
|
||||
driverClass = "com.alibaba.druid.mock.MockDriver";
|
||||
|
||||
// jdbcUrl = "jdbc:mysql://10.20.153.104:3306/druid2";
|
||||
// user = "root";
|
||||
// password = "root";
|
||||
// jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
||||
// user = "dragoon25";
|
||||
// password = "dragoon25";
|
||||
// driverClass = "com.alibaba.druid.mock.MockDriver";
|
||||
|
||||
jdbcUrl = "jdbc:mysql://10.20.153.104:3306/druid2";
|
||||
user = "root";
|
||||
password = "root";
|
||||
}
|
||||
|
||||
|
||||
public void test_perf() throws Exception {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
// druid();
|
||||
dbcp();
|
||||
druid();
|
||||
// dbcp();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,10 +91,9 @@ public class Case3 extends TestCase {
|
||||
for (int i = 0; i < TEST_COUNT; ++i) {
|
||||
p0(dataSource, "dbcp", threadCount);
|
||||
}
|
||||
//dataSource.close();
|
||||
// dataSource.close();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
private void p0(final DataSource dataSource, String name, int threadCount) throws Exception {
|
||||
|
||||
@@ -107,6 +108,14 @@ public class Case3 extends TestCase {
|
||||
|
||||
for (int i = 0; i < LOOP_COUNT; ++i) {
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1");
|
||||
while (rs.next()) {
|
||||
rs.getInt(1);
|
||||
}
|
||||
rs.close();
|
||||
stmt.close();
|
||||
|
||||
conn.close();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
||||
@@ -7,7 +7,9 @@ import java.awt.Toolkit;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
@@ -80,6 +82,9 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
private JLabel lbTestWhileIdle = new JLabel("TestWhileIdle : ");
|
||||
private JTextField txtTestWhileIdle = new JTextField("false");
|
||||
|
||||
private JLabel lbTestOnBorrow = new JLabel("TestOnBorrow : ");
|
||||
private JTextField txtTestOnBorrow = new JTextField("false");
|
||||
|
||||
private JTextField txtGetStep = new JTextField("1");
|
||||
private JTextField txtReleaseStep = new JTextField("1");
|
||||
|
||||
@@ -263,11 +268,21 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
layout.putConstraint(SpringLayout.VERTICAL_CENTER, txtTestWhileIdle, 0, SpringLayout.VERTICAL_CENTER, lbTestWhileIdle);
|
||||
layout.putConstraint(SpringLayout.WEST, txtTestWhileIdle, 0, SpringLayout.WEST, txtUrl);
|
||||
layout.putConstraint(SpringLayout.EAST, txtTestWhileIdle, 0, SpringLayout.EAST, txtUrl);
|
||||
|
||||
mainPanel.add(lbTestOnBorrow);
|
||||
layout.putConstraint(SpringLayout.NORTH, lbTestOnBorrow, 10, SpringLayout.SOUTH, lbTestWhileIdle);
|
||||
layout.putConstraint(SpringLayout.WEST, lbTestOnBorrow, 0, SpringLayout.WEST, lbUrl);
|
||||
layout.putConstraint(SpringLayout.EAST, lbTestOnBorrow, 0, SpringLayout.EAST, lbUrl);
|
||||
|
||||
mainPanel.add(txtTestOnBorrow);
|
||||
layout.putConstraint(SpringLayout.VERTICAL_CENTER, txtTestOnBorrow, 0, SpringLayout.VERTICAL_CENTER, lbTestOnBorrow);
|
||||
layout.putConstraint(SpringLayout.WEST, txtTestOnBorrow, 0, SpringLayout.WEST, txtUrl);
|
||||
layout.putConstraint(SpringLayout.EAST, txtTestOnBorrow, 0, SpringLayout.EAST, txtUrl);
|
||||
|
||||
// ////
|
||||
|
||||
mainPanel.add(btnInitDataSource);
|
||||
layout.putConstraint(SpringLayout.NORTH, btnInitDataSource, 10, SpringLayout.SOUTH, lbTestWhileIdle);
|
||||
layout.putConstraint(SpringLayout.NORTH, btnInitDataSource, 10, SpringLayout.SOUTH, lbTestOnBorrow);
|
||||
layout.putConstraint(SpringLayout.WEST, btnInitDataSource, 0, SpringLayout.WEST, lbUrl);
|
||||
btnInitDataSource.addActionListener(new ActionListener() {
|
||||
|
||||
@@ -367,6 +382,16 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
System.out.println("get connection is null");
|
||||
return;
|
||||
}
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(5);
|
||||
ResultSet rs = stmt.executeQuery("SELECT * FROM address LIMIT 10");
|
||||
while (rs.next()) {
|
||||
rs.getObject(1);
|
||||
}
|
||||
rs.close();
|
||||
|
||||
stmt.close();
|
||||
|
||||
activeConnections.add(conn);
|
||||
} catch (Exception ex) {
|
||||
@@ -397,7 +422,7 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
btnCloseDataSource.setEnabled(false);
|
||||
btnConnect.setEnabled(false);
|
||||
btnClose.setEnabled(false);
|
||||
|
||||
|
||||
statusThread.interrupt();
|
||||
}
|
||||
|
||||
@@ -416,6 +441,7 @@ public class DruidDataSourceUIManager extends JFrame {
|
||||
dataSource.setMaxWait(Integer.parseInt(txtMaxWait.getText().trim()));
|
||||
dataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(txtMinEvictableIdleTimeMillis.getText().trim()));
|
||||
dataSource.setTestWhileIdle(Boolean.parseBoolean(txtTestWhileIdle.getText().trim()));
|
||||
dataSource.setTestOnBorrow(Boolean.parseBoolean(txtTestOnBorrow.getText().trim()));
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
connectCount.incrementAndGet();
|
||||
|
||||
Reference in New Issue
Block a user