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