SQL ERROR: Closed Connection in java

user2246725 picture user2246725 · Apr 8, 2013 · Viewed 8.8k times · Source

Connection is closing automatically even if i'm not closing in finally block.

 public String look( long id, String codeName, Connection conn ) throws SQLException
    {
        try
        {
            StringBuffer sel = new StringBuffer().append(property);
            stmt = conn.prepareCall( sel.toString() );           /*   fileCode.java:194    */

            stmt.setString( 1, nameC );
            stmt.setLong( 2, valueI );
            stmt.registerOutParameter( 3, oracle.jdbc.OracleTypes.VARCHAR );
            stmt.execute();

            return stmt.getString( 3 );
        }
        catch ( SQLException e )
        {
            if ( e.getMessage().toUpperCase().contains( "NO DATA" ) )
            {
                return "Value not found";
            }
            throw e;
        }
        catch ( Exception e )
        {
            e.printStackTrace();

        }
        finally
        {
            System.out.println( " CONNNNNN closed ? : " + conn.isClosed() ); 
        }

    }

Method calls look method,

public class Verfication 
    {   

    public void verify ( , , , , , , ,conn )
    {
      try
      {
         if ( x ==1 )
         {
           ManageCode mCode = new ManageCode();
           System.out.println( "----- 1st Call -----" );
           String mCodeBlock = mCode.look( , , conn);
           String cCodeBlocked = checkBackup ( , , , , , , , , );
           /* connection is closed in this part */ 
           System.out.println( "----- 2nd Call -----" );
           String nCodeBlock = mCode.look ( , , conn );
         }

      }catch(    )
      {

      }

    }
}   

i do get output as mentioned below, I am not sure what is wrong in the connection ? i have also added system outs.

OutPut:

     ----- 1st Call -----
     CONNNNNN closed ? : false

     ----- 2nd Call -----
     CONNNNNN closed ? : true

    SEVERE: Line:71 CodePointChecker ERROR: java.sql.SQLException: Closed Connection
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
    at oracle.jdbc.driver.PhysicalConnection.prepareCall(PhysicalConnection.java:839)
    at oracle.jdbc.driver.PhysicalConnection.prepareCall(PhysicalConnection.java:802)
    at com.XXXXXXXXXXXXXXX.code.fileCode.look(fileCode.java:194)

Answer

OldCurmudgeon picture OldCurmudgeon · Apr 8, 2013

JDBC Connections can be closed for many reasons - not just deliberately by you. This is why there are so many connection pool implementations out there.

I have seen connections closing because the port that the connection was running through was closed. I have seen connections closed just because the database felt like it (mostly with Oracle for this one).

Of course - the most obvious possibility will be that you are closing the connection by accident somewhere else - perhaps in another thread.

Moral - use a proper connection pool. It will save you so much grief.