连接关闭后 H2 模式消失
H2 schema disappears after connection is closed
在H2数据库中设置schema进行单元测试后,依赖schema的单元测试找不到它。
import java.sql.DriverManager
Class.forName("org.h2.Driver")
val setupConn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;MODE=PostgreSQL", "sa", "")
val setupStmt = setupConn.createStatement
// setup schema at the beginning of our test
setupStmt.execute("CREATE SCHEMA IF NOT EXISTS my_test_schema AUTHORIZATION sa;")
setupStmt.execute("GRANT ALL ON SCHEMA my_test_schema TO sa;")
setupStmt.execute("CREATE TABLE IF NOT EXISTS my_test_schema.my_test_table (test_id VARCHAR(255), test_column VARCHAR(255));")
setupStmt.executeQuery("select * from my_test_schema.my_test_table")
// res4: java.sql.ResultSet = rs3: org.h2.result.LocalResultImpl@3eb10d62 columns: 2 rows: 0 pos: -1
// this seems to work correctly ^^^
setupStmt.close
setupConn.close
// now run our test using the schema we just set up
val conn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;SCHEMA=my_test_schema;MODE=PostgreSQL", "sa", "")
val stmt = conn.createStatement
stmt.executeQuery("select * from my_test_table where test_id = '1'")
// org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "MY_TEST_SCHEMA" not found; SQL statement:
// SET SCHEMA my_test_schema [90079-200]
// ^^^^ something has gone horribly wrong
这很尴尬,但我没有意识到当我关闭与我的内存数据库的连接时,数据库会干涸并被吹走。回想起来,这似乎是显而易见的。解决方案是在整个测试过程中保持与数据库的第一个连接打开。
import java.sql.DriverManager
Class.forName("org.h2.Driver")
val setupConn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;MODE=PostgreSQL", "sa", "")
val setupStmt = setupConn.createStatement
// setup schema at the beginning of our test
setupStmt.execute("CREATE SCHEMA IF NOT EXISTS my_test_schema AUTHORIZATION sa;")
setupStmt.execute("GRANT ALL ON SCHEMA my_test_schema TO sa;")
setupStmt.execute("CREATE TABLE IF NOT EXISTS my_test_schema.my_test_table (test_id VARCHAR(255), test_column VARCHAR(255));")
setupStmt.executeQuery("select * from my_test_schema.my_test_table")
// res4: java.sql.ResultSet = rs3: org.h2.result.LocalResultImpl@3eb10d62 columns: 2 rows: 0 pos: -1
// DON'T CLOSE THE CONNECTION YET!
//setupStmt.close
//setupConn.close
val conn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;SCHEMA=my_test_schema;MODE=PostgreSQL", "sa", "")
val stmt = conn.createStatement
stmt.executeQuery("select * from my_test_table where test_id = '1'")
// res5: java.sql.ResultSet = rs4: org.h2.result.LocalResultImpl@293e66e4 columns: 2 rows: 0 pos: -1
// ^^^^ huzzah!
您只需将 ;DB_CLOSE_DELAY=-1
添加到 JDBC URL;无需有活动连接。
https://h2database.com/html/commands.html#set_db_close_delay
如果你使用一些最新版本的 H2,你可能还想添加 ;DATABASE_TO_LOWER=TRUE
以更好地与 PostgreSQL 兼容; PostgreSQL 兼容模式本身并不意味着此设置。
在H2数据库中设置schema进行单元测试后,依赖schema的单元测试找不到它。
import java.sql.DriverManager
Class.forName("org.h2.Driver")
val setupConn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;MODE=PostgreSQL", "sa", "")
val setupStmt = setupConn.createStatement
// setup schema at the beginning of our test
setupStmt.execute("CREATE SCHEMA IF NOT EXISTS my_test_schema AUTHORIZATION sa;")
setupStmt.execute("GRANT ALL ON SCHEMA my_test_schema TO sa;")
setupStmt.execute("CREATE TABLE IF NOT EXISTS my_test_schema.my_test_table (test_id VARCHAR(255), test_column VARCHAR(255));")
setupStmt.executeQuery("select * from my_test_schema.my_test_table")
// res4: java.sql.ResultSet = rs3: org.h2.result.LocalResultImpl@3eb10d62 columns: 2 rows: 0 pos: -1
// this seems to work correctly ^^^
setupStmt.close
setupConn.close
// now run our test using the schema we just set up
val conn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;SCHEMA=my_test_schema;MODE=PostgreSQL", "sa", "")
val stmt = conn.createStatement
stmt.executeQuery("select * from my_test_table where test_id = '1'")
// org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "MY_TEST_SCHEMA" not found; SQL statement:
// SET SCHEMA my_test_schema [90079-200]
// ^^^^ something has gone horribly wrong
这很尴尬,但我没有意识到当我关闭与我的内存数据库的连接时,数据库会干涸并被吹走。回想起来,这似乎是显而易见的。解决方案是在整个测试过程中保持与数据库的第一个连接打开。
import java.sql.DriverManager
Class.forName("org.h2.Driver")
val setupConn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;MODE=PostgreSQL", "sa", "")
val setupStmt = setupConn.createStatement
// setup schema at the beginning of our test
setupStmt.execute("CREATE SCHEMA IF NOT EXISTS my_test_schema AUTHORIZATION sa;")
setupStmt.execute("GRANT ALL ON SCHEMA my_test_schema TO sa;")
setupStmt.execute("CREATE TABLE IF NOT EXISTS my_test_schema.my_test_table (test_id VARCHAR(255), test_column VARCHAR(255));")
setupStmt.executeQuery("select * from my_test_schema.my_test_table")
// res4: java.sql.ResultSet = rs3: org.h2.result.LocalResultImpl@3eb10d62 columns: 2 rows: 0 pos: -1
// DON'T CLOSE THE CONNECTION YET!
//setupStmt.close
//setupConn.close
val conn = DriverManager.getConnection("jdbc:h2:mem:test_data_metrics;SCHEMA=my_test_schema;MODE=PostgreSQL", "sa", "")
val stmt = conn.createStatement
stmt.executeQuery("select * from my_test_table where test_id = '1'")
// res5: java.sql.ResultSet = rs4: org.h2.result.LocalResultImpl@293e66e4 columns: 2 rows: 0 pos: -1
// ^^^^ huzzah!
您只需将 ;DB_CLOSE_DELAY=-1
添加到 JDBC URL;无需有活动连接。
https://h2database.com/html/commands.html#set_db_close_delay
如果你使用一些最新版本的 H2,你可能还想添加 ;DATABASE_TO_LOWER=TRUE
以更好地与 PostgreSQL 兼容; PostgreSQL 兼容模式本身并不意味着此设置。