com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭的解决方案
ERROR http-8080-13 org.dbforms.event.datalist.dao.AbstractDataSource - getCurrent
com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(Unknown Source)
at org.dbforms.conprovider.SingleConnectionWrapper.createStatement(SingleConnectionWrapper.java:379)
at org.dbforms.event.datalist.dao.DataSourceJDBC.open(DataSourceJDBC.java:487)
at org.dbforms.event.datalist.dao.AbstractDataSource.getCurrent(AbstractDataSource.java:104)
at org.dbforms.event.datalist.dao.DataSourceFactory.getCurrent(DataSourceFactory.java:95)
at org.dbforms.event.datalist.GotoEvent.processEvent(GotoEvent.java:267)
at org.dbforms.taglib.DbFormTag.doStartTag(DbFormTag.java:1757)
at org.apache.jsp.yy.car_005forder_005fmodify_jsp._jspService(car_005forder_005fmodify_jsp.java:876)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.dbforms.servlets.Controller.process(Controller.java:285)
at org.dbforms.servlets.base.AbstractServletBase.process_internal(AbstractServletBase.java:123)
at org.dbforms.servlets.base.AbstractServletBase.doPost(AbstractServletBase.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.dbforms.util.external.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:190)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)
Connection对象不能作为全局的参数。
这个问题是并发的问题.. 两个线程同时访问Connection对象,其中一个线程把connection关了,另外一个线程拿着已关闭的连接去用的时候就会报这个错误了
public static Connection GetConnection() {
Connection conn = null; //请注意这里设置为局部变量
try {
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {e.printStackTrace();}
return conn;
将Connection conn设置为局部变量,即可解决这个问题。
com
.
microsoft
.
sqlserver
.
jdbc
.
SQLServer
Exception
: 该
连接
已
关闭
。
at
com
.
microsoft
.
sqlserver
.
jdbc
.
SQLServer
Exception
.makeFromDriverError(
SQLServer
Exception
.java:130)
at
com
.
microsoft
.
sqlserver
...
新建了一个项目,数据库
连接
池采用默认配置,在这种情况下长时间不访问,然而再次访问会报异常:
com
.
microsoft
.
sqlserver
.
jdbc
.
SQLServer
Exception
: Connection reset...
网上搜索和问了同事,说是数据库
连接
池设置不当造成的,在DataSource中加入如下配置:
<!--
连接
池启动时的初始值 -->
Error getting generated key or setting result to parameter object. Cause:
com
.
microsoft
.
sqlserver
.
jdbc
.
SQLServer
Exception
: 必须执行该语句才能获得结果。
<bean id="infoDataSource" destroy-method="close" class="org.apache.
com
mons.dbcp.BasicDataSource"> <property name="driverClassName" value="${infodb.driverClassName}
com
.
microsoft
.
sqlserver
.
jdbc
.
SQLServer
Exception
: 该
连接
已
关闭
。出现这种错误一般是connection的唯一性问题。因为唯一,所以你擅自
关闭
了connection对象,后面的就没法用了,因此在
连接
类里面,不要将connection对象设置为static类型,将其设置为private一般类型即可。
当数据库
连接
池中的
连接
被创建而长时间不使用的情况下,该
连接
会自动回收并失效,但客户端并不知道,在进行数据库操作时仍然使用的是无效的数据库
连接
,这样,就导致客户端程序报“ java.sql.SQL
Exception
: Io 异常: Connection reset” 或“java.sql.SQL
Exception
关闭
的
连接
”异常。
在配置数据源后面加上
今天在写一个数据库语句的时候,出现了一个错误,话不多说,上图
nested
exception
is
com
.
microsoft
.
sqlserver
.
jdbc
.
SQLServer
Exception
: 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'Categories'中的标识列指定显式值。
出错原因可能就是:数据库表的 id 设置为 自增长,那么,写sql语句的时候,就不能写成
String sql =insert into category values(null..