java.sql.SQLException:ORA-01000:超出最大打开游标数

时间:2015-01-30 12:09:38

标签: java oracle oracle11g

我在ORA-01000 SQL异常之上得到了这个。

在我的数据库中有一个包含1500条记录的表,我正在尝试插入这些值。插入另一个表时我遇到了这个错误。

  

java.sql.SQLException:ORA-01000:超出了最大打开游标数           at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)           at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)           at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)           at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)           at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:213)           at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:796)           at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1031)           at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:836)           at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1124)           at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)           at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3368)           在com.exceloid.newdb.NewDBInsertion.insertCLocation(NewDBInsertion.java:1239)           在com.exceloid.dbmigration.CBPartnerImpl.getCLocationData(CBPartnerImpl.java:1198)           在com.exceloid.serviceimpl.CBPartnerServiceImpl.doProcess(CBPartnerServiceImpl.java:22)           在com.exceloid.controller.HomeController.cBPartner(HomeController.java:272)           在com.exceloid.controller.HomeController.welcomePage(HomeController.java:37)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:606)           在org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)           在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)           在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)           在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)           在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)           在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)           在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)           在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)           在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)           在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)           在javax.servlet.http.HttpServlet.service(HttpServlet.java:617)           在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)           在javax.servlet.http.HttpServlet.service(HttpServlet.java:723)           在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)           在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)           在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)           在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)           在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)           在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)           在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)           在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)           在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:606)           在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)           在java.lang.Thread.run(Thread.java:745)

这是我的代码:

Connection connection = null;    
PreparedStatement pst = null;     
ResultSet result = null;    

try {      
            connection = newDbConnection(connection);     
            if(connection != null){      
                 for (CLocation cpayment : list) {    

                        cLocationId = cpayment.getcRegionID();    
                        adClient = cpayment.getAdClientID();    
                        adOrg = cpayment.getAdOrgID();    
                        updated = cpayment.getUpdated();     
                        updatedBy = cpayment.getUpdatedBy();    
                        created = cpayment.getCreated();    
                        createdBy = cpayment.getCreatedBy();    
                        isActive = cpayment.isActive();    
                        city = cpayment.getCity();   
                        postal = cpayment.getPostal();    
                        postalAdd = cpayment.getPostalAdd();    
                        cCountryId = cpayment.getcCountryID();    
                        cCityId = cpayment.getcCityID();    
                        address1 = cpayment.getAddress1();    
                        address2 = cpayment.getAddress2();    
                        cRegionId = cpayment.getcRegionID();   
                        regionName = cpayment.getRegionName();   

                        if(isActive == true) {    
                            InsertIsActive = "Y";    
                        } else {   
                            InsertIsActive = "N";    
                        } 

                        if(ad.equals(adClient)){    
                            final String sql = "SELECT * FROM c_location  where c_location_id = '"+cLocationId+"'";    
                            pst = connection.prepareStatement(sql);    
                            result = pst.executeQuery();    
                            boolean exist=result.next();    
                            if(exist){   
                                 System.out.println("already table data exists");    
                                 flag = true;    
                            } else {    
                                 String sql1="INSERT INTO c_location " + "    VALUES ('"+cLocationId+"', '"+client+"', '"+org+"', '"+InsertIsActive+"', '"+created+"', '"+createdBy+"', '"+updated+"', '"+updatedBy+"', '"+address1+"', '"+address2+"', '"+city+"', '"+postal+"', '"+postalAdd+"', '"+cCountryId+"', '"+cRegionId+"', '"+cCityId+"', '"+regionName+"')";   
                                 System.out.println(sql1);    
                                 pst=connection.prepareStatement(sql);    
                                 pst.executeUpdate();   
                                 flag = true;   
                            }    
                        } else {    
                            System.out.println("new db does not match adclient ID");    
                        }    
                 }     
            }     
        } catch (SQLException e) {    
            e.printStackTrace();     
        } finally {     
            if(result != null){     
                try{   
                    result.close();    
                }catch(Exception e){}    
            }    
            if(pst != null){    
                try{    
                    pst.close();    
                }catch(Exception e){}     
            }    
            if(connection != null){   
                try {    
                    connection.close();    
                } catch (SQLException e){}   
            }    
        }   

我关闭了所有连接但是我还是面临这个错误。

我已经在我的oracle db控制台中执行了此命令,以增加游标的数量。

ALTER SYSTEM SET OPEN_CURSORS=1337 SID='*' SCOPE=BOTH;

但同样的错误再次发生。

3 个答案:

答案 0 :(得分:2)

您只关闭最后一条SQL语句。

您正在遍历所有CLocation,为每个CLocation打开一个Preparedstatement而不关闭单个CLocation。尝试在执行后关闭循环中的Preparedstatement,因为每个都在数据库端创建一个新的数据库光标。

答案 1 :(得分:2)

更改您的SQL语句以使用绑定参数,并且移动不在循环中

final String sql = "SELECT * FROM c_location  where c_location_id = ?";    
pst = connection.prepareStatement(sql);

在循环内部设置绑定参数

pst.setString(1, cLocationId);

你应该对INSERT语句做同样的事情。

答案 2 :(得分:0)

重写您的代码以使用try-with-resources构造。

请参阅:https://blogs.oracle.com/WebLogicServer/entry/using_try_with_resources_with

还使用绑定变量。它会更短,更安全,甚至更快。