certain EntityManager operations (e.g. StoredProcedureQuery.execute()) should rollback the TX for certain exceptions

Description

org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:188)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:91)
at org.hibernate.procedure.internal.ProcedureCallImpl.buildOutputs(ProcedureCallImpl.java:299)
at org.hibernate.procedure.internal.ProcedureCallImpl.getResult(ProcedureCallImpl.java:270)
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.outputs(StoredProcedureQueryImpl.java:177)
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:194)

StoredProcedureQueryImpl probably needs something like:

catch ( HibernateException he) { entityManager().throwPersistenceException( he ); }

3.10.7 Query Execution
...
Runtime exceptions other than the NoResultException, NonUniqueResultException, QueryTimeoutException, and LockTimeoutException thrown by the methods of the Query, TypedQuery, and StoredProcedureQuery interfaces other than those methods specified below cause the current transaction to be marked for rollback if the persistence context is joined to the transaction. On database platforms on which a query timeout causes transaction rollback, the persistence provider must throw the PersistenceException instead of the QueryTimeoutException.

Runtime exceptions thrown by the following methods of the Query, TypedQuery, and StoredProcedureQuery interfaces do not cause the current transaction to be marked for rollback: getParameters, getParameter, getParameterValue, getOutputParameterValue, getLockMode.

Runtime exceptions thrown by the methods of the Tuple, TupleElement, and Parameter interfaces do not cause the current transaction to be marked for rollback

Activity

Show:

Steve Ebersole November 3, 2015 at 9:59 PM

Thanks Scott.

Scott Marlow November 3, 2015 at 9:18 PM

Javadoc in JPA 2.1 section 3.1.1 mentions that a IAE will be thrown. Also see additional quote below from text following javadoc that explains what happens when runtime exceptions are thrown (other than LockTimeoutException):

/** * Create an instance of Query for executing a named query * (in the Java Persistence query language or in native SQL). * @param name the name of a query defined in metadata * @return the new query instance * @throws IllegalArgumentException if a query has not been * defined with the given name or if the query string is * found to be invalid */ public Query createNamedQuery(String name);

Runtime exceptions thrown by the methods of the EntityManager interface other than the Lock-
TimeoutException will cause the current transaction to be marked for rollback if the persistence
context is joined to that transaction.

This also came up in https://hibernate.atlassian.net/browse/HHH-8837#icft=HHH-8837

Steve Ebersole November 3, 2015 at 8:22 PM

Scott, I am working on https://hibernate.atlassian.net/browse/HHH-8996#icft=HHH-8996 which claims we are being overly aggressive in one cases in terms of marking a transaction for rollback only stemming from this issue.

Specifically a call to EntityManager#createNamedQuery(String). This change reverted a previous fix to not mark the transaction for rollback only when this situation happens; the previous fix did not include a test case unfortunately which is why we did not see it.

Anyway, can you point out the section in the spec where it says that calling EntityManager#createNamedQuery(String) where that name is not known is supposed to result in the transaction being marked for rollback?

Scott Marlow September 6, 2013 at 10:29 PM

Steve Ebersole September 6, 2013 at 10:07 PM

Scott, is there still more to do here?

Fixed

Details

Assignee

Reporter

Time tracking

0.62h logged

Fix versions

Affects versions

Priority

Created August 15, 2013 at 5:54 PM
Updated November 3, 2015 at 9:59 PM
Resolved September 11, 2013 at 4:48 PM