Occasional, Intermittent "java.sql.SQLException: Closed Statement" When Connection Shared Between Threads

(Doc ID 784343.1)

Last updated on JANUARY 31, 2018

Applies to:

JDBC - Version 11.1.0.6 to 12.2.0.0.0 [Release 11.1 to 12c]
Information in this document applies to any platform.

Symptoms

A multi-threaded, standalone Java application runs in a version 1.6 JVM and connects to a database using JDBC/Thin 11.1.0.7 ojdbc6.jar. This is also applicable to later versions.

Intermittently, queries from a particular method fail with:

java.sql.SQLException: Closed Statement
    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:445)
    at oracle.jdbc.driver.OracleClosedStatement.executeQuery(OracleClosedStatement.java:2566)
    at oracle.jdbc.driver.OracleStatementWrapper.executeQuery(OracleStatementWrapper.java:387)
   ... remaining code ...


The exception cannot be reproduced at will.

The method in which the exception occurs accepts a Connection as an argument, creates a Statement, uses the Statement to execute a query, returns the result set, does processing not associated with the Statement, and then closes the Statement. See following example with method "insertCustomer".

public void insertCustomer(Connection pConn, ... other arguments ...) throws SQLException 

    ... code ... 
    
    Statement stmt = pConn.createStatement();  
    
    ... code ... 
    
    ResultSet rs = stmt.executeQuery(selectSql); 
            
    ... code ... 
    
    stmt.close();           
    
    ... code ... 
}


Although the Connection object is shared between threads, the Statement object is specific to one thread.

Changes

 

Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms