I'm using IntelliJ with this code:
@Test
public void testH2() throws SQLException {
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Server server = null;
try {
Class.forName("org.h2.Driver");
conn =
DriverManager.getConnection("jdbc:h2:mem:test_mem;DB_CLOSE_DELAY=-1", "sa",
"sa");
server = Server.createTcpServer().start();
preparedStatement = conn.prepareStatement("drop table if exists Test;");
preparedStatement.executeUpdate();
preparedStatement = conn.prepareStatement("create table Test(Id int
primary key, Name varchar(255));");
preparedStatement.executeUpdate();
preparedStatement = conn.prepareStatement("insert into Test values(1,
'Hello');");
preparedStatement.executeUpdate();
//debuggingDatabaseSleep(30);
preparedStatement = conn.prepareStatement("select * from Test order by
Id;");
resultSet = preparedStatement.executeQuery();
resultSet.next();
assertEquals(resultSet.getInt("Id"), 1);
assertEquals(resultSet.getString("Name"), "Hello");
}
catch(ClassNotFoundException | SQLException | InterruptedException e) {
fail(e.getLocalizedMessage());
}
finally {
try {
if(resultSet != null)
resultSet.close();
if(preparedStatement != null)
preparedStatement.close();
} catch(Exception e) {
}
}
conn.close();
}
If I stop on a breakpoint and try to access my in-memory database from SQL
Workbench, it never connects. If I uncomment debuggingDatabaseSleep(30)
and let the test run, then I can connect from SQL Workbench as long as the
test is running with my Thread.sleep() call.
Should the database still be available after the test has completed? It
doesn't seem like it should since once the test is complete, the app is no
longer running. So is there a better method?
--
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 post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.