gpt4 book ai didi

java - JDBC 查询执行

转载 作者:行者123 更新时间:2023-12-01 17:40:31 25 4
gpt4 key购买 nike

我在执行查询时遇到问题。我使用相同的结果集和语句来执行所有查询。现在我面临间歇性的 SQlException,表示连接已关闭。现在我们必须为每个查询拥有单独的结果集,或者有类似锁的结构。谁能告诉我哪个更好。我认为引入锁会减慢进程。我是对的吗?

更新:更清楚地说。发生错误的原因可能是在所有查询执行之前调用了finally block ,并且连接关闭并抛出异常。

这是我遇到的异常

java.sql.SQLException: Connection has already been closed. at weblogic.jdbc.wrapper.PoolConnection.checkConnection(PoolConnection.java:81) at weblogic.jdbc.wrapper.ResultSet.preInvocationHandler(ResultSet.java:68) at weblogic.jdbc.wrapper.ResultSet_com_informix_jdbc_IfxResultSet.next(Unknown Source) at com.test.test.execute(test.java:76)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:413) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:225) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:459) at javax.servlet.http.HttpServlet.service(HttpServlet.java:760) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1077) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:465) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:348) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:7047) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121) at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3902) at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2773) at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)

示例代码:

ResultSet rst=null; 
Statement stmt=null;
Connection con=DBConnection.getConnection();
stmt=con.createStatement();
rst=stmt.executeQuery("select * from dual");
while(rst.next())
{ : ://Some code }
rst=stmt.executeQuery("select * from doctor where degree="BM");
while(rst.next())
{ //blah blah }
finally
{
//close con,rst and stmt
}

最佳答案

您没有重用结果集,您正在泄漏结果集。rst=stmt.executeQuery... 生成一个新的结果集,并且以前的结果集永远不会关闭:(

关于java - JDBC 查询执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1321638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com