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
On Sun, Oct 12, 2008 at 12:32 PM, sproket <[EMAIL PROTECTED]> wrote:
>
> 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;
> }
> }
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---