gpt4 book ai didi

java - 在执行之前无法从 CallableStatement 检索结果

转载 作者:行者123 更新时间:2023-11-29 14:24:18 24 4
gpt4 key购买 nike

我在我的应用程序中使用此方法来获取特定组织的子组织。它调用一个存储过程(创建一个临时表)。

StoredProcedureDAO.java

private Connection dbConnection = null;
private CallableStatement cstmt = null;
private ResultSet results = null;

在 getChildrens(int siteId) 方法中

public ArrayList<String> getChildrens(int siteId)
{
ArrayList<String> childrenSites = new ArrayList<String>();
Connection pgConnection = getFreshDBConnection();
if (pgConnection == null) {
cat.error("getChildrens got a null connection");
return null;
}
try{
pgConnection.setAutoCommit(false);
cstmt = pgConnection.prepareCall("{ ? = call \"spGetChildSitesInfo\" ( ? ) }");
cstmt.registerOutParameter(1, Types.OTHER);
cstmt.setInt(2, siteId);
cstmt.execute();
results = (ResultSet) cstmt.getObject(1);
if(results != null){
while(results.next()){
childrenSites.add(results.getString(1));
}
}
cstmt.close();
pgConnection.commit();
pgConnection.close();
} catch (SQLException e){
try {
pgConnection.rollback();
cat.error("SQL Exception has occured while retriving the children", e);
} catch (Exception ex) {
cat.error("Error while connection rollback: ", e);
}

} finally {
try {
if(!pgConnection.isClosed()){
pgConnection.setAutoCommit(true);
pgConnection.close();
}
pgConnection = null;
} catch (Exception e) {
cat.error("Error while closing the connection: "+e);
}
}

return childrenSites;
}

我在单击 jsp 中的链接时调用此函数,一切正常。 当我在操作之间多次单击链接时会出现问题。在那种情况下,我得到以下异常

ERROR [StoredProcedureDAO] SQL Exception has occured while retriving the children
org.postgresql.util.PSQLException: Results cannot be retrieved from a CallableStatement before it is executed.
at org.postgresql.jdbc2.AbstractJdbc2Statement.checkIndex(AbstractJdbc2Statement.java:2417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.checkIndex(AbstractJdbc2Statement.java:2400)
at org.postgresql.jdbc2.AbstractJdbc2Statement.getObject(AbstractJdbc2Statement.java:2084)
at com.mycompany.ejb.session.StoredProcedureDAO.getChildrens(StoredProcedureDAO.java:120)
at com.mycompany.struts.action.SessionFacadeManager.getSelectedSite(SessionFacadeManager.java:495)
at org.apache.jsp.Clinical.BrowseCases_jsp._jspService(BrowseCases_jsp.java:383)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
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:654)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
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:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:524)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)

实验后添加更多信息

这是我的构造函数(私有(private))

private StoredProcedureDAO() {
DBManager dbManager = DBManager.getInstance();
dbConnection = dbManager.getConnection();
}

这里是 getInstance()

public static StoredProcedureDAO getInstance()
{
if (myself == null)
myself = new StoredProcedureDAO();
return myself;
}

我正在使用以下方法调用 getChildrens()

StoredProcedureDAO dao = StoredProcedureDAO.getInstance();
children = dao.getChildrens(site_id);

现在,如果我删除 if 语句

//if (myself == null)

在那种情况下,我不会得到异常(exception)。我正在使用 postgresql-9.0-801.jdbc4.jar 驱动程序。谁能解释一下原因?

最佳答案

您遇到了多线程问题。似乎您在不同线程之间共享 ResultSet 成员变量(一个 servlet(或 JSP)可以由多个线程共享),因此出现错误。您确定要创建 StoredProcedureDAO 的多个实例吗?

关于java - 在执行之前无法从 CallableStatement 检索结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6746437/

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