I have developed a web service that I'm running in WebSphere
application server. I want to test a method that simply retrieves all
values from a database table and returns them as part of a payload in
a response. I want to use an in-memory database for testing so the
tests will be easily repeatable.
I create the database and populate the table in a @BeforeClass
method. I set up the database connection as follows....
try {
Class.forName("org.h2.Driver");
} catch (Exception e) {
e.printStackTrace();
}
// Set up table in h2
Connection con = DriverManager.getConnection("jdbc:h2:mem:" +
DATABASE_NAME + ";DB_CLOSE_DELAY=-1");
The database is created correctly, because I can write some temporary
code at the very start of my test case to prove the database is still
alive and well...
// is my database still alive
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:h2:mem:" + DATABASE_NAME +
";IFEXISTS=TRUE");
PreparedStatement stmt1 = con.prepareStatement("SELECT * FROM
BLAH.BLAH2");
ResultSet blah = stmt1.executeQuery();
while (blah.next()) {
System.out.println(blah.getString(1));
System.out.println(blah.getString(2));
}
blah.close();
stmt1.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And I get output in my console...
ID1
NAME1
ID2
NAME2
Then I actually make the real web services call... I pass the name of
the database in the query string.
In the web services method, I attempt to get a connection to the
database as follows...
Connection con = null;
if (testing != null) {
try {
Class.forName("org.h2.Driver");
con =
DriverManager.getConnection("jdbc:h2:tcp://localhost/mem:" +
testing + ";IFEXISTS=TRUE");
} catch (SQLException e) {
e.printStackTrace();
}
}
... and it waits several seconds and ultimately bombs out with...
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R
org.h2.jdbc.JdbcSQLException: Connection is broken: "session
closed" [90067-137]
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.message.DbException.get(DbException.java:167)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.message.DbException.get(DbException.java:144)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.engine.SessionRemote.checkClosed(SessionRemote.java:470)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.engine.SessionRemote.connectServer(SessionRemote.java:331)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:
223)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.engine.SessionRemote.createSession(SessionRemote.java:217)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:111)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:95)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
org.h2.Driver.connect(Driver.java:58)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
java.sql.DriverManager.getConnection(DriverManager.java:572)
[6/24/10 17:25:08:406 CDT] 00000023 SystemErr R at
java.sql.DriverManager.getConnection(DriverManager.java:218)
Is it possible to do what I am trying to accomplish... or am I hitting
JVM/classloader issues? (Note that I am using tcp://localhost in the
web service connection. I was using version 1.1.112 which also tossed
a broken connection message. This latest version (1.2.137) mentions
the session is closed in addition to the broken connection message.
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en.