mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修正未关闭statement先关闭connection时的bug
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@433 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class Case3 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.close();
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT 1");
|
||||
rs.next();
|
||||
|
||||
conn.close();
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Date;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidTest {
|
||||
|
||||
private static DruidDataSource DS;
|
||||
|
||||
public DruidTest(String connectURI) {
|
||||
initDS(connectURI);
|
||||
}
|
||||
|
||||
public DruidTest(String connectURI, String username, String pswd,
|
||||
String driverClass, int initialSize, int maxActive, int maxIdle,
|
||||
int minIdle, int maxWait) {
|
||||
initDS(connectURI, username, pswd, driverClass, initialSize, maxActive,
|
||||
minIdle, maxIdle, maxWait);
|
||||
}
|
||||
|
||||
public Connection getConn() {
|
||||
Connection con = null;
|
||||
if (DS != null) {
|
||||
try {
|
||||
con = DS.getConnection();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
try {
|
||||
con.setAutoCommit(false);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return con;
|
||||
}
|
||||
return con;
|
||||
}
|
||||
|
||||
public static void initDS(String connectURI, String username, String pswd,
|
||||
String driverClass, int initialSize, int maxActive, int maxIdle,
|
||||
int minIdle, int maxWait) {
|
||||
DruidDataSource ds = new DruidDataSource();
|
||||
ds.setDriverClassName(driverClass);
|
||||
ds.setUsername(username);
|
||||
ds.setPassword(pswd);
|
||||
ds.setUrl(connectURI);
|
||||
ds.setInitialSize(initialSize); // 初始的连接数;
|
||||
ds.setMaxActive(maxActive);
|
||||
ds.setMaxIdle(maxIdle);
|
||||
ds.setMinIdle(minIdle);
|
||||
ds.setMaxWait(maxWait);
|
||||
DS = ds;
|
||||
}
|
||||
|
||||
public static void initDS(String connectURI) {
|
||||
initDS(connectURI, "root", "12345", "com.mysql.jdbc.Driver", 40, 40,
|
||||
40, 10, 5);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, SQLException {
|
||||
|
||||
DruidTest db = new DruidTest("jdbc:mysql://10.20.141.150:8066/amoeba");
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
|
||||
FileWriter fileWriter = new FileWriter("D:\\data.txt");
|
||||
try{
|
||||
conn = db.getConn();
|
||||
}catch(Exception e){
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
|
||||
long sum = 0;
|
||||
for (int i = 1; i < 10; i++) {
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
Date start = new Date();
|
||||
rs = stmt
|
||||
.executeQuery("select * from offer where member_id = 'forwd251'");
|
||||
Date end = new Date();
|
||||
sum = sum + (end.getTime() - start.getTime());
|
||||
fileWriter.write(String.valueOf((end.getTime() - start
|
||||
.getTime())));
|
||||
fileWriter.write("/\n");
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println((float) sum / 10);
|
||||
|
||||
|
||||
conn.close();
|
||||
stmt.close();
|
||||
rs.close();
|
||||
fileWriter.flush();
|
||||
fileWriter.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@@ -18,7 +19,7 @@ public class Test_kyline extends TestCase {
|
||||
ds.setPassword(password);
|
||||
ds.setDriverClassName(driver);
|
||||
|
||||
//DriverManager.getConnection(url, user, password);
|
||||
DriverManager.getConnection(url, user, password);
|
||||
|
||||
Connection conn = ds.getConnection();
|
||||
conn.close();
|
||||
|
||||
Reference in New Issue
Block a user