Occasional, Intermittent "java.sql.SQLException: Closed Statement" When Connection Shared Between Threads (Doc ID 784343.1)

Last updated on AUGUST 16, 2016

Applies to:

JDBC - Version to [Release 11.1 to 11.2]
Information in this document applies to any platform.
***Checked for relevance on 08-May-2012***


A multi-threaded, standalone Java application runs in a version 1.6 JVM and connects to a database using JDBC/Thin ojdbc6.jar.

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 ... 
    ... code ...

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


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