java.sql.SQLException: - ORA-01000: maximum open cursors exceeded

后端 未结 13 1397
鱼传尺愫
鱼传尺愫 2020-11-22 08:36

I am getting an ORA-01000 SQL exception. So I have some queries related to it.

  1. Are maximum open cursors exactly related to number of JDBC connections, or are t
相关标签:
13条回答
  • 2020-11-22 08:56

    Correct your Code like this:

    try
    { //method try starts  
      String sql = "INSERT into TblName (col1, col2) VALUES(?, ?)";
      pStmt = obj.getConnection().prepareStatement(sql);
      pStmt.setLong(1, subscriberID);
      for (String language : additionalLangs) {
        pStmt.setInt(2, Integer.parseInt(language));
        pStmt.execute();
      }
    } //method/try ends
    finally
    { //finally starts
       pStmt.close()
    } 
    

    Are you sure, that you're really closing your pStatements, connections and results?

    To analyze open objects you can implment a delegator pattern, which wraps code around your statemant, connection and result objects. So you'll see, if an object will successfully closed.

    An Example for: pStmt = obj.getConnection().prepareStatement(sql);

        class obj{ 
    
        public Connection getConnection(){
        return new ConnectionDelegator(...here create your connection object and put it into ...);
    
        } 
    }
    
    
    class ConnectionDelegator implements Connection{
        Connection delegates;
    
        public ConnectionDelegator(Connection con){
           this.delegates = con;
        }
    
        public Statement prepareStatement(String sql){
            return delegates.prepareStatement(sql);
        }
    
        public void close(){
            try{
               delegates.close();
            }finally{
               log.debug(delegates.toString() + " was closed");
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:58

    query to find sql that opened.

    SELECT s.machine, oc.user_name, oc.sql_text, count(1) 
    FROM v$open_cursor oc, v$session s
    WHERE oc.sid = s.sid
    and S.USERNAME='XXXX'
    GROUP BY user_name, sql_text, machine
    HAVING COUNT(1) > 2
    ORDER BY count(1) DESC
    
    0 讨论(0)
  • 2020-11-22 08:59

    I too had faced this issue.The below exception used to come

    java.sql.SQLException: - ORA-01000: maximum open cursors exceeded
    

    I was using Spring Framework with Spring JDBC for dao layer.

    My application used to leak cursors somehow and after few minutes or so, It used to give me this exception.

    After a lot of thorough debugging and analysis, I found that there was the problem with the Indexing, Primary Key and Unique Constraints in one of the Table being used in the Query i was executing.

    My application was trying to update the Columns which were mistakenly Indexed. So, whenever my application was hitting the update query on the indexed columns, The database was trying to do the reindexing based on the updated values. It was leaking the cursors.

    I was able to solve the problem by doing Proper Indexing on the columns which were used to search in the query and applying appropriate constraints wherever required.

    0 讨论(0)
  • 2020-11-22 09:00

    I had this problem with my datasource in WildFly and Tomcat, connecting to a Oracle 10g.

    I found that under certain conditions the statement wasn't closed even when the statement.close() was invoked. The problem was with the Oracle Driver we were using: ojdbc7.jar. This driver is intended for Oracle 12c and 11g, and it seems has some issues when is used with Oracle 10g, so I downgrade to ojdbc5.jar and now everything is running fine.

    0 讨论(0)
  • 2020-11-22 09:01

    Did you set autocommit=true? If not try this:

    { //method try starts  
        String sql = "INSERT into TblName (col1, col2) VALUES(?, ?)";
        Connection conn = obj.getConnection()
        pStmt = conn.prepareStatement(sql);
    
        for (String language : additionalLangs) {
            pStmt.setLong(1, subscriberID);
            pStmt.setInt(2, Integer.parseInt(language));
            pStmt.execute();
            conn.commit();
        }
    } //method/try ends { 
        //finally starts
        pStmt.close()
    } //finally ends 
    
    0 讨论(0)
  • 2020-11-22 09:01

    In our case, we were using Hibernate and we had many variables referencing the same Hibernate mapped entity. We were creating and saving these references in a loop. Each reference opened a cursor and kept it open.

    We discovered this by using a query to check the number of open cursors while running our code, stepping through with a debugger and selectively commenting things out.

    As to why each new reference opened another cursor - the entity in question had collections of other entities mapped to it and I think this had something to do with it (perhaps not just this alone but in combination with how we had configured the fetch mode and cache settings). Hibernate itself has had bugs around failing to close open cursors, though it looks like these have been fixed in later versions.

    Since we didn't really need to have so many duplicate references to the same entity anyway, the solution was to stop creating and holding onto all those redundant references. Once we did that the problem when away.

    0 讨论(0)
提交回复
热议问题