0

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.
Jayendran
  • 9,638
  • 8
  • 60
  • 103
Rony
  • 101
  • 1
  • 8
  • can you please include a full stack trace of your exception? – ipolevoy Feb 21 '17 at 19:12
  • If you really want to use your pool (as opposed to a container pool), you can look at this test: https://github.com/javalite/activejdbc/blob/8f09292c8cd36271e7b20f996224d0b54cd06af9/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java – ipolevoy Feb 21 '17 at 19:17
  • another suggestion I have for you is not to pollute code inside the controller to open and close connections. Instead, write a servlet filter for that, here is the example: https://github.com/javalite/activejdbc/blob/765f9c9583dfaae5893bbe476e240bbac56e8975/activejdbc/src/main/java/org/javalite/activejdbc/web/ActiveJdbcFilter.java#L45-L45. This will also let you to not call explicit `games.size()`, as the view will have access to the connection on a current thread. – ipolevoy Feb 21 '17 at 19:21
  • ok, thanks for your suggestion! I'll do all your recommendation and I'll post if there is a problem. – Rony Feb 21 '17 at 20:35
  • I've update my post with the stack trace I had. I followed your recommendations, for the filter, which makes my code cleaner, and for the pool example. It's works fine after one day of testing. So thank you again. It was really helpful. – Rony Feb 22 '17 at 17:55
  • awesome, in that case, you can upvote my answer. – ipolevoy Feb 22 '17 at 18:22

1 Answers1

0

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
ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • I was thinking: "I've should used ActiveWeb" when I discovered it. But I was already using spring, it was too late to change. For my next project I'll definitely use ActiveWeb. And for the upvote, I can not do it, I need 15 point of reputation to vote :/ – Rony Feb 23 '17 at 17:55