简体   繁体   English

如何避免MaxOpenPreparedStatements异常?

[英]How to avoid the MaxOpenPreparedStatements Exception?

In my code I am using 4 prepared statements to (select,insert,update) my tables also I have to use these statements inside a for loop because I need to process large amount of data, I am using only one db connection and the statements opened only once before the for loop and I am using the addBatch() in each iteration and after the end of loop the statements execute with executeBatch() and statements closed in the finally block , but still the MaxOpenPreparedStatements exception thrown after second iteration !!! 在我的代码中,我正在使用4个准备好的语句来(选择,插入,更新)我的表,而且我还必须在for循环内使用这些语句,因为我需要处理大量数据,我仅使用一个db连接和这些语句在for循环之前仅打开了一次,并且我在每次迭代中都使用addBatch(),并且在循环结束之后,语句以executeBatch()执行,并且语句在finally块中关闭,但是在第二次迭代后仍然抛出MaxOpenPreparedStatements异常! ! , What can I do to avoid that ? ,我该怎么做才能避免这种情况?

try{
     pStat1 = conn.prepareStatement("insert into...");
     pStat2 = conn.prepareStatement("update...");
     pStat3 =  conn.prepareStatement("insert into...");
     pStat4 = conn.prepareStatement("update...");


     for (.....) {
          //set parameters
          pStat1.addBatch();
          //set parameters
          pStat2.addBatch();
          //set parameters
          pStat3.addBatch();
          //set parameters
          pStat4.addBatch();

          }      
            pStat1.executeBatch();
            pStat2.executeBatch();
            pStat3.executeBatch();
            pStat4.executeBatch();
          } catch (Exception e) {
                 e.printStackTrace();
          }finally {
                 if(pStat1!= null){
                       try {
                              pStat1.close();
                       } catch (SQLException e) {
                              e.printStackTrace();
                       }
                 }
                 if(pStat2!= null){
                       try {
                              pStat2.close();
                       } catch (SQLException e) {
                              e.printStackTrace();
                       }
                 }

                 if(pStat3!= null){
                       try {
                              pStat3.close();
                       } catch (SQLException e) {
                              e.printStackTrace();
                       }
                 }

                 if(pStat4!= null){
                       try {
                              pStat4.close();
                       } catch (SQLException e) {
                              e.printStackTrace();
                       }
                 }

Thanks in advance. 提前致谢。

I am not sure it will solve the problem.But may be.Try this once 我不确定它是否可以解决问题,但是可能会。尝试一次

        pStat1.executeBatch();
          //close pStat1 here
        pStat2.executeBatch();
          //close pStat2 here
        pStat3.executeBatch();
          //close pStat3 here
        pStat4.executeBatch();
          //close pStat4 here

Unroll it into four loops and process each PreparedStatement completely from preparation to closure before starting on the next one. 将其展开为四个循环,并从准备到结束完全处理每个PreparedStatement,然后再开始下一个。

If you can. 如果你可以的话。 If the statements are interdependent you're just going to have to change that. 如果语句是相互依赖的,则只需要更改它即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM