我经常遇到这个错误:org.javalite.activejdbc.DBException:无法打开新连接

时间:2017-02-21 14:20:37

标签: java activejdbc javalite

当我离开页面10分钟然后刷新或快速刷新页面多次(控制器启动查询的页面)时出现此错误。

与此处相同的错误:https://github.com/javalite/activejdbc/issues/268

我使用ActiveJdbc的1.4.13.j7和连接池的c3p0-0.9.1.2。

这是我的代码:连接的Singleton

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;
    }
}

以下是抛出异常的控制器的示例

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

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

Base.close();

return "addOnView";

我在网上搜索解决方案时看到有些人也有这个问题,但我没有找到任何人解决它。我使用open和close,所以我不知道问题来自哪里。

我做错了吗?

编辑:

这是整个statcktrace

HTTP状态500 - 请求处理失败;嵌套的例外是org.javalite.activejdbc.DBException:默认情况下,连接实例:com.mchange.v2.c3p0.impl.NewProxyConnection@50ed26eb因为现有的连接仍然是在当前线程,名称不能打开一个新的连接。这可能表示您的应用程序存在逻辑错误。

输入例外报告

消息请求处理失败;嵌套异常是org.javalite.activejdbc.DBException:无法打开新连接,因为现有连接仍在当前线程上,name:default,connection instance:com.mchange.v2.c3p0.impl.NewProxyConnection@50ed26eb。这可能表示您的应用程序存在逻辑错误。

说明服务器遇到内部错误,导致无法完成此请求。

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.

1 个答案:

答案 0 :(得分:0)

我认为您的代码存在一些问题。以下是我的建议:

  1. 请将此作为自定义池配置的示例:https://github.com/javalite/activejdbc/blob/8f09292c8cd36271e7b20f996224d0b54cd06af9/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java
  2. 通过将连接打开/关闭调用移动到servlet过滤器来清理代码:https://github.com/javalite/activejdbc/blob/765f9c9583dfaae5893bbe476e240bbac56e8975/activejdbc/src/main/java/org/javalite/activejdbc/web/ActiveJdbcFilter.java#L45-L45
  3. 您可能需要检查ActiveWeb,它与ActiveJDBC很好地集成并提供高效率:http://javalite.io/activeweb