提交 810a0771 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #563 from andreitokar/issue#556.2

Issue#556 close connections in tests (at least in case of successful execution)
...@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; ...@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
import org.h2.fulltext.FullText; import org.h2.fulltext.FullText;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.IOUtils;
import org.h2.util.Task; import org.h2.util.Task;
/** /**
...@@ -90,9 +91,9 @@ public class TestFullText extends TestBase { ...@@ -90,9 +91,9 @@ public class TestFullText extends TestBase {
deleteDb("fullTextReopen"); deleteDb("fullTextReopen");
} }
private static void close(Collection<Connection> list) throws SQLException { private static void close(Collection<Connection> list) {
for (Connection conn : list) { for (Connection conn : list) {
conn.close(); IOUtils.closeSilently(conn);
} }
} }
...@@ -491,8 +492,7 @@ public class TestFullText extends TestBase { ...@@ -491,8 +492,7 @@ public class TestFullText extends TestBase {
return; return;
} }
deleteDb("fullText"); deleteDb("fullText");
ArrayList<Connection> connList = new ArrayList<Connection>(); Connection conn = getConnection("fullText");
Connection conn = getConnection("fullText", connList);
String prefix = lucene ? "FTL_" : "FT_"; String prefix = lucene ? "FTL_" : "FT_";
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
String className = lucene ? "FullTextLucene" : "FullText"; String className = lucene ? "FullTextLucene" : "FullText";
...@@ -590,15 +590,15 @@ public class TestFullText extends TestBase { ...@@ -590,15 +590,15 @@ public class TestFullText extends TestBase {
if (!config.memory) { if (!config.memory) {
conn.close(); conn.close();
conn = getConnection("fullText");
} }
conn = getConnection("fullText", connList);
stat = conn.createStatement(); stat = conn.createStatement();
stat.executeQuery("SELECT * FROM " + prefix + "SEARCH('World', 0, 0)"); stat.executeQuery("SELECT * FROM " + prefix + "SEARCH('World', 0, 0)");
stat.execute("CALL " + prefix + "DROP_ALL()"); stat.execute("CALL " + prefix + "DROP_ALL()");
close(connList); conn.close();
} }
private void testDropIndex(boolean lucene) throws SQLException { private void testDropIndex(boolean lucene) throws SQLException {
...@@ -625,7 +625,6 @@ public class TestFullText extends TestBase { ...@@ -625,7 +625,6 @@ public class TestFullText extends TestBase {
"_CREATE_INDEX('PUBLIC', 'TEST', 'NAME1, NAME2')"); "_CREATE_INDEX('PUBLIC', 'TEST', 'NAME1, NAME2')");
stat.execute("UPDATE TEST SET NAME2=NULL WHERE ID=1"); stat.execute("UPDATE TEST SET NAME2=NULL WHERE ID=1");
stat.execute("UPDATE TEST SET NAME2='Hello World' WHERE ID=1"); stat.execute("UPDATE TEST SET NAME2='Hello World' WHERE ID=1");
conn.close();
conn.close(); conn.close();
FileUtils.deleteRecursive(getBaseDir() + "/fullTextDropIndex", false); FileUtils.deleteRecursive(getBaseDir() + "/fullTextDropIndex", false);
......
...@@ -1285,6 +1285,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -1285,6 +1285,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
String.format("SELECT ORA_HASH('%s', 0) FROM DUAL", testStr)); String.format("SELECT ORA_HASH('%s', 0) FROM DUAL", testStr));
assertResult(String.valueOf("foo".hashCode()), stat, assertResult(String.valueOf("foo".hashCode()), stat,
String.format("SELECT ORA_HASH('%s', 0, 0) FROM DUAL", testStr)); String.format("SELECT ORA_HASH('%s', 0, 0) FROM DUAL", testStr));
conn.close();
} }
private void testToDateException() { private void testToDateException() {
...@@ -2058,6 +2059,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -2058,6 +2059,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
assertFalse(parsed.before(before)); assertFalse(parsed.before(before));
assertFalse(parsed.after(after)); assertFalse(parsed.after(after));
conn.close();
} }
...@@ -2083,6 +2085,7 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -2083,6 +2085,7 @@ public class TestFunctions extends TestBase implements AggregateFunction {
rs.close(); rs.close();
assertEquals(first, second); assertEquals(first, second);
conn.close();
} }
private void testThatCurrentTimestampUpdatesOutsideATransaction() private void testThatCurrentTimestampUpdatesOutsideATransaction()
...@@ -2107,26 +2110,26 @@ public class TestFunctions extends TestBase implements AggregateFunction { ...@@ -2107,26 +2110,26 @@ public class TestFunctions extends TestBase implements AggregateFunction {
rs.close(); rs.close();
assertTrue(second.after(first)); assertTrue(second.after(first));
conn.close();
} }
private void callCompiledFunction(String functionName) throws SQLException { private void callCompiledFunction(String functionName) throws SQLException {
deleteDb("functions"); deleteDb("functions");
Connection conn = getConnection("functions"); try (Connection conn = getConnection("functions")) {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
ResultSet rs; ResultSet rs;
stat.execute("create alias " + functionName + " AS " stat.execute("create alias " + functionName + " AS "
+ "$$ boolean " + functionName + "() " + "$$ boolean " + functionName + "() "
+ "{ return true; } $$;"); + "{ return true; } $$;");
PreparedStatement stmt = conn.prepareStatement( PreparedStatement stmt = conn.prepareStatement(
"select " + functionName + "() from dual"); "select " + functionName + "() from dual");
rs = stmt.executeQuery(); rs = stmt.executeQuery();
rs.next(); rs.next();
assertEquals(Boolean.class.getName(), rs.getObject(1).getClass().getName()); assertEquals(Boolean.class.getName(), rs.getObject(1).getClass().getName());
stat.execute("drop alias " + functionName + "");
conn.close(); stat.execute("drop alias " + functionName + "");
}
} }
private void assertCallResult(String expected, Statement stat, String sql) private void assertCallResult(String expected, Statement stat, String sql)
......
...@@ -36,75 +36,75 @@ public class TestQueryCache extends TestBase { ...@@ -36,75 +36,75 @@ public class TestQueryCache extends TestBase {
} }
private void test1() throws Exception { private void test1() throws Exception {
Connection conn = getConnection("queryCache;QUERY_CACHE_SIZE=10"); try (Connection conn = getConnection("queryCache;QUERY_CACHE_SIZE=10")) {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("create table test(id int, name varchar)"); stat.execute("create table test(id int, name varchar)");
PreparedStatement prep; PreparedStatement prep;
// query execution may be fast here but the parsing must be slow // query execution may be fast here but the parsing must be slow
StringBuilder queryBuilder = new StringBuilder("select count(*) from test t1 where \n"); StringBuilder queryBuilder = new StringBuilder("select count(*) from test t1 where \n");
for (int i = 0; i < 1000; i++) { for (int i = 0; i < 1000; i++) {
if (i != 0) { if (i != 0) {
queryBuilder.append(" and "); queryBuilder.append(" and ");
}
queryBuilder.append(" TIMESTAMP '2005-12-31 23:59:59' = TIMESTAMP '2005-12-31 23:59:59' ");
} }
queryBuilder.append(" TIMESTAMP '2005-12-31 23:59:59' = TIMESTAMP '2005-12-31 23:59:59' "); String query = queryBuilder.toString();
} conn.prepareStatement(query);
String query = queryBuilder.toString(); int firstGreater = 0;
conn.prepareStatement(query); int firstSmaller = 0;
int firstGreater = 0; long time;
int firstSmaller = 0; ResultSet rs;
long time; long first = 0;
ResultSet rs; // 1000 iterations to warm up and avoid JIT effects
long first = 0; for (int i = 0; i < 1005; i++) {
// 1000 iterations to warm up and avoid JIT effects // this should both ensure results are not re-used
for (int i = 0; i < 1005; i++) { // stat.execute("set mode regular");
// this should both ensure results are not re-used // stat.execute("create table x()");
// stat.execute("set mode regular"); // stat.execute("drop table x");
// stat.execute("create table x()"); time = System.nanoTime();
// stat.execute("drop table x");
time = System.nanoTime();
prep = conn.prepareStatement(query);
execute(prep);
prep.close();
rs = stat.executeQuery(query);
rs.next();
int c = rs.getInt(1);
rs.close();
assertEquals(0, c);
time = System.nanoTime() - time;
if (i == 1000) {
// take from cache and do not close, so that next iteration will have a cache miss
prep = conn.prepareStatement(query); prep = conn.prepareStatement(query);
} else if (i == 1001) { execute(prep);
first = time; prep.close();
// try to avoid pauses in subsequent iterations rs = stat.executeQuery(query);
System.gc(); rs.next();
} else if (i > 1001) { int c = rs.getInt(1);
if (first > time) { rs.close();
firstGreater++; assertEquals(0, c);
} else { time = System.nanoTime() - time;
firstSmaller++; if (i == 1000) {
// take from cache and do not close, so that next iteration will have a cache miss
prep = conn.prepareStatement(query);
} else if (i == 1001) {
first = time;
// try to avoid pauses in subsequent iterations
System.gc();
} else if (i > 1001) {
if (first > time) {
firstGreater++;
} else {
firstSmaller++;
}
} }
} }
// first prepare time must be always greater because of query cache,
// but JVM is too unpredictable to assert that, so just check that
// usually this is true
assertSmaller(firstSmaller, firstGreater);
stat.execute("drop table test");
} }
// first prepare time must be always greater because of query cache,
// but JVM is too unpredictable to assert that, so just check that
// usually this is true
assertSmaller(firstSmaller, firstGreater);
stat.execute("drop table test");
conn.close();
} }
private void testClearingCacheWithTableStructureChanges() throws Exception { private void testClearingCacheWithTableStructureChanges() throws Exception {
Connection conn = getConnection("queryCache;QUERY_CACHE_SIZE=10"); try (Connection conn = getConnection("queryCache;QUERY_CACHE_SIZE=10")) {
assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, conn). assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, conn).
prepareStatement("SELECT * FROM TEST"); prepareStatement("SELECT * FROM TEST");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.executeUpdate("CREATE TABLE TEST(col1 bigint, col2 varchar(255))"); stat.executeUpdate("CREATE TABLE TEST(col1 bigint, col2 varchar(255))");
PreparedStatement prep = conn.prepareStatement("SELECT * FROM TEST"); PreparedStatement prep = conn.prepareStatement("SELECT * FROM TEST");
prep.close(); prep.close();
stat.executeUpdate("DROP TABLE TEST"); stat.executeUpdate("DROP TABLE TEST");
assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, conn). assertThrows(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, conn).
prepareStatement("SELECT * FROM TEST"); prepareStatement("SELECT * FROM TEST");
conn.close(); }
} }
} }
...@@ -33,34 +33,36 @@ public class TestShow extends TestBase { ...@@ -33,34 +33,36 @@ public class TestShow extends TestBase {
} }
private void testPgCompatibility() throws SQLException { private void testPgCompatibility() throws SQLException {
Connection conn = getConnection("mem:pg"); try (Connection conn = getConnection("mem:pg")) {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
assertResult("UNICODE", stat, "SHOW CLIENT_ENCODING"); assertResult("UNICODE", stat, "SHOW CLIENT_ENCODING");
assertResult("read committed", stat, "SHOW DEFAULT_TRANSACTION_ISOLATION"); assertResult("read committed", stat, "SHOW DEFAULT_TRANSACTION_ISOLATION");
assertResult("read committed", stat, "SHOW TRANSACTION ISOLATION LEVEL"); assertResult("read committed", stat, "SHOW TRANSACTION ISOLATION LEVEL");
assertResult("ISO", stat, "SHOW DATESTYLE"); assertResult("ISO", stat, "SHOW DATESTYLE");
assertResult("8.1.4", stat, "SHOW SERVER_VERSION"); assertResult("8.1.4", stat, "SHOW SERVER_VERSION");
assertResult("UTF8", stat, "SHOW SERVER_ENCODING"); assertResult("UTF8", stat, "SHOW SERVER_ENCODING");
}
} }
private void testMysqlCompatibility() throws SQLException { private void testMysqlCompatibility() throws SQLException {
Connection conn = getConnection("mem:pg"); try (Connection conn = getConnection("mem:pg")) {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
ResultSet rs; ResultSet rs;
// show tables without a schema // show tables without a schema
stat.execute("create table person(id int, name varchar)"); stat.execute("create table person(id int, name varchar)");
rs = stat.executeQuery("SHOW TABLES"); rs = stat.executeQuery("SHOW TABLES");
assertTrue(rs.next()); assertTrue(rs.next());
assertEquals("PERSON", rs.getString(1)); assertEquals("PERSON", rs.getString(1));
assertEquals("PUBLIC", rs.getString(2)); assertEquals("PUBLIC", rs.getString(2));
assertFalse(rs.next()); assertFalse(rs.next());
// show tables with a schema // show tables with a schema
assertResultRowCount(1, stat.executeQuery("SHOW TABLES FROM PUBLIC")); assertResultRowCount(1, stat.executeQuery("SHOW TABLES FROM PUBLIC"));
// columns // columns
assertResultRowCount(2, stat.executeQuery("SHOW COLUMNS FROM person")); assertResultRowCount(2, stat.executeQuery("SHOW COLUMNS FROM person"));
}
} }
} }
...@@ -45,6 +45,7 @@ public class TestConnection extends TestBase { ...@@ -45,6 +45,7 @@ public class TestConnection extends TestBase {
assertThrows(SQLClientInfoException.class, conn).setClientInfo("numServers", "SomeValue"); assertThrows(SQLClientInfoException.class, conn).setClientInfo("numServers", "SomeValue");
assertThrows(SQLClientInfoException.class, conn).setClientInfo("server23", "SomeValue"); assertThrows(SQLClientInfoException.class, conn).setClientInfo("server23", "SomeValue");
conn.close();
} }
private void testSetUnsupportedClientInfoProperties() throws SQLException { private void testSetUnsupportedClientInfoProperties() throws SQLException {
...@@ -54,6 +55,7 @@ public class TestConnection extends TestBase { ...@@ -54,6 +55,7 @@ public class TestConnection extends TestBase {
properties.put("ClientUser", "someUser"); properties.put("ClientUser", "someUser");
assertThrows(SQLClientInfoException.class, conn).setClientInfo(properties); assertThrows(SQLClientInfoException.class, conn).setClientInfo(properties);
conn.close();
} }
private void testSetSupportedClientInfoProperties() throws SQLException { private void testSetSupportedClientInfoProperties() throws SQLException {
...@@ -67,6 +69,7 @@ public class TestConnection extends TestBase { ...@@ -67,6 +69,7 @@ public class TestConnection extends TestBase {
assertNull(conn.getClientInfo("ApplicationName")); assertNull(conn.getClientInfo("ApplicationName"));
// new property has been set // new property has been set
assertEquals(conn.getClientInfo("ClientUser"), "someUser"); assertEquals(conn.getClientInfo("ClientUser"), "someUser");
conn.close();
} }
private void testSetSupportedClientInfo() throws SQLException { private void testSetSupportedClientInfo() throws SQLException {
...@@ -74,17 +77,20 @@ public class TestConnection extends TestBase { ...@@ -74,17 +77,20 @@ public class TestConnection extends TestBase {
conn.setClientInfo("ApplicationName", "Connection Test"); conn.setClientInfo("ApplicationName", "Connection Test");
assertEquals(conn.getClientInfo("ApplicationName"), "Connection Test"); assertEquals(conn.getClientInfo("ApplicationName"), "Connection Test");
conn.close();
} }
private void testSetUnsupportedClientInfo() throws SQLException { private void testSetUnsupportedClientInfo() throws SQLException {
Connection conn = getConnection("clientInfoDB2;MODE=DB2"); Connection conn = getConnection("clientInfoDB2;MODE=DB2");
assertThrows(SQLClientInfoException.class, conn).setClientInfo( assertThrows(SQLClientInfoException.class, conn).setClientInfo(
"UnsupportedName", "SomeValue"); "UnsupportedName", "SomeValue");
conn.close();
} }
private void testGetUnsupportedClientInfo() throws SQLException { private void testGetUnsupportedClientInfo() throws SQLException {
Connection conn = getConnection("clientInfo"); Connection conn = getConnection("clientInfo");
assertNull(conn.getClientInfo("UnknownProperty")); assertNull(conn.getClientInfo("UnknownProperty"));
conn.close();
} }
private void testSetGetSchema() throws SQLException { private void testSetGetSchema() throws SQLException {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论