提交 1140d4a9 authored 作者: noelgrandin's avatar noelgrandin

fix connection leaks in the testing code. Prevents my machine from running out…

fix connection leaks in the testing code. Prevents my machine from running out of sockets during soak tests
上级 6c42717a
......@@ -155,6 +155,9 @@ public class Shell extends Tool implements Runnable {
}
execute(s);
}
if (conn != null) {
conn.close();
}
}
}
......
......@@ -99,8 +99,8 @@ public class TestCluster extends TestBase {
String url2 = "jdbc:h2:tcp://localhost:" + port2 + "/test";
String urlCluster = "jdbc:h2:tcp://" + serverList + "/test";
Server n1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
Server n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2 , "-baseDir", getBaseDir() + "/node2").start();
Server server1 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port1, "-baseDir", getBaseDir() + "/node1").start();
Server server2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2 , "-baseDir", getBaseDir() + "/node2").start();
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList",
serverList);
......@@ -113,7 +113,7 @@ public class TestCluster extends TestBase {
rs.next();
assertEquals(3, rs.getInt(1));
n2.stop();
server2.stop();
DeleteDbFiles.main("-dir", getBaseDir() + "/node2", "-quiet");
stat.execute("insert into t1 values(4, 'd'), (5, 'e')");
......@@ -121,7 +121,7 @@ public class TestCluster extends TestBase {
rs.next();
assertEquals(5, rs.getInt(1));
n2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2 , "-baseDir", getBaseDir() + "/node2").start();
server2 = org.h2.tools.Server.createTcpServer("-tcpPort", "" + port2 , "-baseDir", getBaseDir() + "/node2").start();
CreateCluster.main("-urlSource", url1, "-urlTarget", url2, "-user", user, "-password", password, "-serverList",
serverList);
......@@ -132,9 +132,10 @@ public class TestCluster extends TestBase {
rs = stat.executeQuery("select count(*) from t1");
rs.next();
assertEquals(5, rs.getInt(1));
conn.close();
n1.stop();
n2.stop();
server1.stop();
server2.stop();
deleteFiles();
}
......@@ -298,11 +299,13 @@ public class TestCluster extends TestBase {
// test the cluster connection
check(connApp, len, "'" + serverList + "'");
connApp.close();
// test a non-admin user
String user2 = "test", password2 = getPassword("test");
connApp = DriverManager.getConnection(urlCluster, user2, password2);
check(connApp, len, "'" + serverList + "'");
connApp.close();
n1.stop();
......
......@@ -316,6 +316,7 @@ public class TestCompatibility extends TestBase {
}
private void testDB2() throws SQLException {
conn.close();
conn = getConnection("compatibility;MODE=DB2");
Statement stat = conn.createStatement();
testLog(Math.log(10), stat);
......@@ -334,7 +335,6 @@ public class TestCompatibility extends TestBase {
stat.execute("create table test(id varchar)");
stat.execute("insert into test values ('3'),('1'),('2')");
res = stat.executeQuery("select id from test order by id fetch next 2 rows only");
conn = getConnection("compatibility");
res.next();
assertEquals("1", res.getString(1));
res.next();
......@@ -343,6 +343,7 @@ public class TestCompatibility extends TestBase {
}
private void testDerby() throws SQLException {
conn.close();
conn = getConnection("compatibility;MODE=Derby");
Statement stat = conn.createStatement();
testLog(Math.log(10), stat);
......
......@@ -95,9 +95,11 @@ public class TestMultiThreadedKernel extends TestBase {
ArrayList<Task> list = New.arrayList();
int size = 2;
final int count = 1000;
final Connection[] connections = new Connection[count];
String url = getURL("multiThreadedKernel;MULTI_THREADED=TRUE;CACHE_SIZE=16", true);
for (int i = 0; i < size; i++) {
final Connection conn = DriverManager.getConnection(url, getUser(), getPassword());
connections[i] = conn;
if (i == 0) {
Statement stat = conn.createStatement();
stat.execute("drop table test if exists");
......@@ -122,15 +124,20 @@ public class TestMultiThreadedKernel extends TestBase {
for (Task t : list) {
t.get();
}
for (int i = 0; i < size; i++) {
connections[i].close();
}
}
private void testCache() throws Exception {
ArrayList<Task> list = New.arrayList();
int size = 3;
final int count = 100;
final Connection[] connections = new Connection[count];
String url = getURL("multiThreadedKernel;MULTI_THREADED=TRUE;CACHE_SIZE=1", true);
for (int i = 0; i < size; i++) {
final Connection conn = DriverManager.getConnection(url, getUser(), getPassword());
connections[i] = conn;
if (i == 0) {
Statement stat = conn.createStatement();
stat.execute("drop table test if exists");
......@@ -155,6 +162,9 @@ public class TestMultiThreadedKernel extends TestBase {
for (Task t : list) {
t.get();
}
for (int i = 0; i < size; i++) {
connections[i].close();
}
}
}
......@@ -75,6 +75,7 @@ public class TestPowerOff extends TestBase {
stat.execute("insert into test values(null, space(11000))");
int max = Integer.MAX_VALUE - ((JdbcConnection) conn).getPowerOffCount();
for (int i = 0; i < max + 10; i++) {
conn.close();
conn = getConnection(url);
stat = conn.createStatement();
stat.execute("insert into test values(null, space(11000))");
......@@ -85,11 +86,7 @@ public class TestPowerOff extends TestBase {
} catch (SQLException e) {
// ignore
}
try {
conn.close();
} catch (SQLException e) {
// ignore
}
JdbcUtils.closeSilently(conn);
}
}
......@@ -271,8 +268,9 @@ public class TestPowerOff extends TestBase {
Database.setInitialPowerOffCount(Integer.MAX_VALUE);
}
int state = 0;
Connection conn = null;
try {
Connection conn = getConnection(url);
conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("SET WRITE_DELAY 0");
stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
......@@ -303,6 +301,7 @@ public class TestPowerOff extends TestBase {
throw e;
}
}
JdbcUtils.closeSilently(conn);
return state;
}
......
......@@ -13,6 +13,7 @@ import java.util.List;
import org.h2.constant.ErrorCode;
import org.h2.jaqu.Db;
import org.h2.test.TestBase;
import org.h2.util.JdbcUtils;
/**
* Test annotation processing.
......@@ -148,14 +149,18 @@ public class AnnotationsTest extends TestBase {
private void testCreateTableIfRequiredAnnotation() {
// tests JQTable.createTableIfRequired=false
Db noCreateDb = null;
try {
Db noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
noCreateDb = Db.open("jdbc:h2:mem:", "sa", "sa");
noCreateDb.insertAll(ProductNoCreateTable.getList());
noCreateDb.close();
} catch (RuntimeException r) {
SQLException s = (SQLException) r.getCause();
assertEquals(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, s.getErrorCode());
}
if (noCreateDb != null) {
JdbcUtils.closeSilently(noCreateDb.getConnection());
}
}
}
......@@ -446,6 +446,7 @@ public class TestWeb extends TestBase {
// the server stops on logout
}
t.get();
conn.close();
} finally {
if (old != null) {
System.setProperty(SysProperties.H2_BROWSER, old);
......
......@@ -550,6 +550,7 @@ int tes;
} catch (SQLException e) {
assertEquals(ErrorCode.DUPLICATE_KEY_1, e.getErrorCode());
}
conn.close();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论