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

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

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.

Pavlo ZNovember 30, 2015 at 9:34 AM

steve@hibernate.org
Issue reproduced on version "5.0.3.Final".

Pavlo ZNovember 18, 2015 at 8:42 AM

Verified, reproduced on "5.0.3.Final", method "org.hibernate.loader.Loader.doQuery" looks the same

Fixed

Details

Assignee

Reporter

Labels

Components

Fix versions

Affects versions

Priority

Created November 11, 2015 at 4:31 PM
Updated March 15, 2016 at 2:10 AM
Resolved March 11, 2016 at 10:13 PM