Connection pool with embedded h2 - Login timeout

270 views
Skip to first unread message

sproket

unread,
Oct 12, 2008, 6:32:00 AM10/12/08
to H2 Database
Hi All,

I'm getting the following exception in a web app I'm writing.

java.sql.SQLException: Login timeout
at
org.h2.jdbcx.JdbcConnectionPool.getConnection(JdbcConnectionPool.java:
182)

I think it might be because of the way I'm using the
JdbcConnectionPool.

Because my application is designed to be deployed automatically I
can't use the app server's connection pool.

What I'm trying to do is get a connection at the beginning of the
request and return it to the pool at the end of the request. The
object containing the connection is retrieved at the start of my
struts action and the clean up is called in a finally block. Code is
below. Is this the right approach? TIA

public class ContextInfo implements IContextInfo {

private static final Logger log =
LogManager.getLogger(ContextInfo.class);

private User user;
private static JdbcDataSource ds;
private static JdbcConnectionPool cp;
private Connection con;


private ContextInfo() {
Object o = new Object();
synchronized (o) {
if (ds == null) {
ds = new JdbcDataSource();
ds.setURL("jdbc:h2:kbdata;"); //
DB_CLOSE_DELAY=-1;MULTI_THREADED=1 ?
ds.setUser("sa");
ds.setPassword("");
cp = JdbcConnectionPool.create(ds);
cp.setMaxConnections(5);
}
}
try {
con = cp.getConnection();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}

public static ContextInfo getInstance(HttpServletRequest request)
{

if (request.getAttribute("contextInfo") == null) {
ContextInfo info = new ContextInfo();
info.user = (User)
request.getSession().getAttribute("user");

request.setAttribute("contextInfo", info);
}

return (ContextInfo) request.getAttribute("contextInfo");
}

public void cleanUp() {
try {
con.close();
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}


public Connection getConnection() {
return con;
}

public User getUser() {
return user;
}


public void setUser(User user) {
this.user = user;
}
}

Thomas Mueller

unread,
Oct 14, 2008, 4:02:46 PM10/14/08
to h2-da...@googlegroups.com
Hi,

The code synchronizes on a new object each time - that's like no
synchronization at all. You probably want: synchronized
(ContextInfo.class) { ... }

> What I'm trying to do is get a connection at the beginning of the
> request and return it to the pool at the end of the request.

The rest of the code you sent looks good. But you may want to log when
a connection was made, and when it was returned:
log.debug("Pool created");
private static int openConnectionCount;
log.debug("Connection opened: " + ++openConnectionCount);
log.debug("Connection closed: " + --openConnectionCount);

If the count goes higher than 5 you know there is a problem in your application.

Regards,
Thomas

sproket

unread,
Oct 14, 2008, 7:03:47 PM10/14/08
to H2 Database
Thanks! You're right. My sync code was wrong.
Reply all
Reply to author
Forward
0 new messages