Search code examples
javaactivejdbcjavalite

I got this error quite often: org.javalite.activejdbc.DBException: Cannot open a new connection


I got this error when I leave a page 10 minutes and then refresh or when I refresh a page multiple times quickly (a page where the controller launches a query of course).

Tahe same error as here: https://github.com/javalite/activejdbc/issues/268

I use the 1.4.13.j7 of ActiveJdbc and c3p0-0.9.1.2 for connection pool.

Here is my code: a the Singleton for the connection

import com.mchange.v2.c3p0.DataSources;
import java.sql.SQLException;
import javax.sql.DataSource;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConnectionPool {

    private DataSource dataSourceUnpooled;
    private DataSource dataSourcePooled;
    private static ConnectionPool instance;

    private ConnectionPool() {
        init();
    }

    public static synchronized ConnectionPool getInstance() {
        if (instance == null) {
            instance = new ConnectionPool();
        }
        return instance;
    }

    private void init() {
        try {
            Class.forName("org.postgresql.Driver");
            dataSourceUnpooled = DataSources.unpooledDataSource("jdbc:postgresql://127.0.0.1:5432/mydb", "postgres", "");
            dataSourcePooled = DataSources.pooledDataSource(dataSourceUnpooled);
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(ConnectionPool.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public DataSource getConnection() {
        return dataSourcePooled;
    }
}

and here is an example of a controllers that throws the Exception

Base.open(ConnectionPool.getInstance().getConnection());

List<Game> games= Game.where("add_on_id = ?)", addOnId);
games.size();

Base.close();

return "addOnView";

I've seen some people had this problem as well when searching online for a solution, but I didn't find anyone who solved it. I use open and close, so I don't know where the problem comes from.

Is there something I do wrong?

edit:

here is the whole statcktrace

HTTP Status 500 - Request processing failed; nested exception is org.javalite.activejdbc.DBException: Cannot open a new connection because existing connection is still on current thread, name: default, connection instance: com.mchange.v2.c3p0.impl.NewProxyConnection@50ed26eb. This might indicate a logical error in your application.

type Exception report

message Request processing failed; nested exception is org.javalite.activejdbc.DBException: Cannot open a new connection because existing connection is still on current thread, name: default, connection instance: com.mchange.v2.c3p0.impl.NewProxyConnection@50ed26eb. This might indicate a logical error in your application.

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.javalite.activejdbc.DBException: Cannot open a new connection because existing connection is still on current thread, name: default, connection instance: com.mchange.v2.c3p0.impl.NewProxyConnection@50ed26eb. This might indicate a logical error in your application.
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.myapp.config.ActiveJdbcFilter.doFilter(ActiveJdbcFilter.java:46)
root cause

org.javalite.activejdbc.DBException: Cannot open a new connection because existing connection is still on current thread, name: default, connection instance: com.mchange.v2.c3p0.impl.NewProxyConnection@50ed26eb. This might indicate a logical error in your application.
    org.javalite.activejdbc.DB.checkExistingConnection(DB.java:235)
    org.javalite.activejdbc.DB.open(DB.java:186)
    org.javalite.activejdbc.Base.open(Base.java:106)
    com.myapp.controllers.AddOnController.addOns(AddOnController.java:145)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    com.myapp.config.ActiveJdbcFilter.doFilter(ActiveJdbcFilter.java:46)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.27 logs.

Solution

  • I think you have some issues with your code. Here are my suggestions:

    1. Please, use this as an example of custom pool config: https://github.com/javalite/activejdbc/blob/8f09292c8cd36271e7b20f996224d0b54cd06af9/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java
    2. Clean up your code by moving connection open/close calls to a servlet filter: https://github.com/javalite/activejdbc/blob/765f9c9583dfaae5893bbe476e240bbac56e8975/activejdbc/src/main/java/org/javalite/activejdbc/web/ActiveJdbcFilter.java#L45-L45
    3. You might want to check ActiveWeb, which is nicely integrated with ActiveJDBC and provides a high degree of productivity: http://javalite.io/activeweb