我经常收到此错误:org.javalite.activejdbc.DBException: 无法打开新连接
I got this error quite often: org.javalite.activejdbc.DBException: Cannot open a new connection
当我离开页面 10 分钟然后刷新或当我快速多次刷新页面(当然是控制器启动查询的页面)时出现此错误。
Tahe 与此处相同的错误:https://github.com/javalite/activejdbc/issues/268
我使用ActiveJdbc的1.4.13.j7和连接池的c3p0-0.9.1.2
这是我的代码:连接的单例
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:无法打开新连接,因为现有连接仍在当前线程上,名称:默认,连接实例: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.
我认为您的代码存在一些问题。以下是我的建议:
- 请将此用作自定义池配置的示例:https://github.com/javalite/activejdbc/blob/8f09292c8cd36271e7b20f996224d0b54cd06af9/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java
- 通过将连接 open/close 调用移动到 servlet 过滤器来清理您的代码:https://github.com/javalite/activejdbc/blob/765f9c9583dfaae5893bbe476e240bbac56e8975/activejdbc/src/main/java/org/javalite/activejdbc/web/ActiveJdbcFilter.java#L45-L45
- 您可能想要查看 ActiveWeb,它与 ActiveJDBC 完美集成并提供了高度的生产力:http://javalite.io/activeweb
当我离开页面 10 分钟然后刷新或当我快速多次刷新页面(当然是控制器启动查询的页面)时出现此错误。
Tahe 与此处相同的错误:https://github.com/javalite/activejdbc/issues/268
我使用ActiveJdbc的1.4.13.j7和连接池的c3p0-0.9.1.2
这是我的代码:连接的单例
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:无法打开新连接,因为现有连接仍在当前线程上,名称:默认,连接实例: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.
我认为您的代码存在一些问题。以下是我的建议:
- 请将此用作自定义池配置的示例:https://github.com/javalite/activejdbc/blob/8f09292c8cd36271e7b20f996224d0b54cd06af9/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java
- 通过将连接 open/close 调用移动到 servlet 过滤器来清理您的代码:https://github.com/javalite/activejdbc/blob/765f9c9583dfaae5893bbe476e240bbac56e8975/activejdbc/src/main/java/org/javalite/activejdbc/web/ActiveJdbcFilter.java#L45-L45
- 您可能想要查看 ActiveWeb,它与 ActiveJDBC 完美集成并提供了高度的生产力:http://javalite.io/activeweb