diff --git a/hibernate-core/src/main/java/org/hibernate/loader/Loader.java b/hibernate-core/src/main/java/org/hibernate/loader/Loader.java index d95978412b..a9f6a38a9e 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/Loader.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/Loader.java @@ -2130,6 +2130,11 @@ public abstract class Loader { session.getJdbcCoordinator().afterStatementExecution(); throw sqle; } + catch (HibernateException he) { + session.getJdbcCoordinator().getResourceRegistry().release( st ); + session.getJdbcCoordinator().afterStatementExecution(); + throw he; + } } protected void autoDiscoverTypes(ResultSet rs) { diff --git a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/QueryTest.java b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/QueryTest.java index 16e6642436..f7e95d5d50 100644 --- a/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/QueryTest.java +++ b/hibernate-entitymanager/src/test/java/org/hibernate/jpa/test/query/QueryTest.java @@ -1055,4 +1055,20 @@ public class QueryTest extends BaseEntityManagerFunctionalTestCase { em.close(); } + + @Test + @TestForIssue( jiraKey = "HHH-10269") + public void testFailingNativeQuery() { + final EntityManager entityManager = getOrCreateEntityManager(); + // Tests that Oracle does not run out of cursors. + for (int i = 0; i < 1000; i++) { + try { + entityManager.createNativeQuery("Select 1 from NotExistedTable").getResultList(); + fail( "expected PersistenceException" ); + } catch (PersistenceException e) { + // expected + } + } + + } }