I'm trying to use h2 as a unit test. but I found the mem mode has some
problem: the new created database by connection 1 is not visibile from
connection2.
below is an example code:
Class.forName("org.h2.Driver");
Connection connection =
DriverManager.getConnection("jdbc:h2:mem:;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;");
Connection connection2 =
DriverManager.getConnection("jdbc:h2:mem:;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;");
Statement s = connection.createStatement();
s.execute("create schema testdb");
s.execute("create table testdb.testt(id bigint not null primary key
auto_increment, a int)");
PreparedStatement ps = connection.prepareStatement("insert into testdb.testt
values (111, 33)", Statement.RETURN_GENERATED_KEYS);
int in = ps.executeUpdate();
System.out.println("use connection1 test:" + tableExists(connection, "testdb",
"testt"));
System.out.println("use connection2 test:" + tableExists(connection2, "testdb",
"testt"));
public static boolean tableExists(Connection conn, String dbName, String
tableName) throws SQLException {
String[] types = {"TABLE"};
ResultSet rs = null;
try {
rs = conn.getMetaData().getTables(null, dbName, tableName, types);
if(rs.next()) {
return true;
}
else {
return false;
}
}
finally {
if (rs != null) {
rs.close();
}
}
}
the output is:
use connection1 test:true
use connection2 test:false
Any idea about this problem?
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/h2-database/33a31298-33e1-44cf-aecd-49e2828c27be%40googlegroups.com.