JDBC Statement is not closed if exception appeared during query execution
Description
JDBC Statement is not closed when exception appeared during query execution, JPA native query on Oracle v11 database.
If run statement in cycle
On iteration 300 error appeared: ORA-01000: maximum open cursors exceeded
I can provide sample project if required, but issue is obvious by code review, method "org.hibernate.loader.Loader.doQuery(SessionImplementor, QueryParameters, boolean, ResultTransformer)" Exception appeared on line with "executeQueryStatement", this block is not covered by "finally".
Also if enable logging, normal execution contains "JdbcCoordinatorImpl: Closing prepared statement", and absent in execution with exception.
iteration for normal query
iteration for query with exception
Solution: "executeQueryStatement" have to be covered with "finally".
Attachments
2
Activity
Former userMarch 11, 2016 at 10:13 PM
Fixed in master and 5.0 branches.
Former userMarch 11, 2016 at 9:50 PM
, thanks for pointing out where things go awry.
ResultSetReturnImp#extract is converting the SQLException into an org.hibernate.JDBCException, which is not caught until it's too late.
Adding a catch block for HibernateException to Loader#getResultSet, similar to what is done in Loader#prepareQueryStatement fixes this.
Scott MarlowFebruary 26, 2016 at 2:22 PM
Edited
From looking at the attached trace output, it looks like Oracle is deferring checking of the passed SQL until the getResultSet() call (which has been an important optimization for many years). However, this means that the SQL error is not detected as early as possible. We likely need to change the following code to save the PreparedStatement in a SqlStatementWrapper that is passed in, so that the caller can closed the prepared statement. Or something like that.
JDBC Statement is not closed when exception appeared during query execution, JPA native query on Oracle v11 database.
If run statement in cycle
On iteration 300 error appeared: ORA-01000: maximum open cursors exceeded
I can provide sample project if required,
but issue is obvious by code review, method "org.hibernate.loader.Loader.doQuery(SessionImplementor, QueryParameters, boolean, ResultTransformer)"
Exception appeared on line with "executeQueryStatement", this block is not covered by "finally".
org.hibernate.loader.Loader.doQuery(SessionImplementor, QueryParameters, boolean, ResultTransformer)
Also if enable logging, normal execution contains "JdbcCoordinatorImpl: Closing prepared statement", and absent in execution with exception.
iteration for normal query
iteration for query with exception
Solution: "executeQueryStatement" have to be covered with "finally".