SQL ERROR: closed connection in java

The connection closes automatically, even if I do not close the 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 call method search,

 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 get the output as below, I'm not sure what is wrong with the connection? I also added system outputs.

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) 
+4
source share
2 answers

JDBC communications can be closed for many reasons, not just intentionally by you. This is why there are so many connection pool implementations.

I saw that the connections were being closed because the port through which the connection was made was closed. I saw the connections closed only because the database was similar to it (mainly with Oracle for this).

Of course, the most obvious possibility is that you accidentally close the connection somewhere else - perhaps in a different thread.

Moral - Use the correct connection pool. It will save you so much grief.

+1
source

Something must be happening with your SQL query that fires a SQLException, causing the connection to close.

0
source

All Articles