Richard Zowalla created OPENJPA-3100:
----------------------------------------
Summary: Stored procedure changes are not committed by
EntityTransaction.commit() in an optimistic transaction
Key: OPENJPA-3100
URL: https://issues.apache.org/jira/browse/OPENJPA-3100
Project: OpenJPA
Issue Type: Bug
Components: jdbc
Affects Versions: 4.2.0
Reporter: Richard Zowalla
Fix For: 4.3.0
Since 4.2.0, a stored procedure that changes data and has an OUT parameter is
not committed by {{EntityTransaction.commit()}} when the transaction is
optimistic (the default), unless {{OpenJPAEntityManager.beginStore()}} is
called first. The changes are rolled back when the EntityManager is closed.
Reported by [~alexanderschuetz97] on users@ (PostgreSQL):
{code:java}
transaction.begin();
StoredProcedureQuery proc =
em.createStoredProcedureQuery("delete_marked_files");
proc.registerStoredProcedureParameter(1, Long.class, ParameterMode.OUT);
proc.execute();
long orphansWiped = ((Number) proc.getOutputParameterValue(1)).longValue();
transaction.commit(); // the deleted rows are still there
{code}
Reproduced on PostgreSQL 18 with a PL/pgSQL PROCEDURE with one BIGINT OUT
parameter that deletes rows, resource-local:
||Version||beginStore()||after commit()||after rollback()||
|4.1.1 (OUT not registered)|no|deleted|deleted|
|4.2.0 / master|no|*not deleted*|not deleted|
|4.2.0 / master|yes|deleted|not deleted|
*Cause:* with an optimistic transaction there is no datastore transaction until
the first flush or {{beginStore()}}. Native queries ({{SQLStoreQuery}}) and
bulk updates ({{JDBCStoreQuery}}) call {{getContext().beginStore()}} before
they take the connection, {{StoredProcedureQuery.executeQuery()}} does not, so
the procedure runs on a separate connection. In 4.1.1 that connection was in
auto-commit mode, so the changes were committed at once (and could not be
rolled back). Since OPENJPA-2940 (88e770947) the connection is switched to
{{autoCommit=false}} when the procedure has OUT parameters (for REF_CURSOR on
PostgreSQL) and is only committed in {{XROP.close()}}, which does not run in
this case. {{commit()}} does not touch that connection, and closing it rolls
the changes back.
The spec expects the procedure to take part in the transaction: a
resource-local EntityManager "is always automatically joined to any
resource-local transaction that is begun for that entity manager" (3.4.1), and
that transaction "is mapped to a resource transaction over the resource"
(7.5.2).
The TCK does not cover this:
{{StoredProcedureQuery/Client1.executeUpdateOfADeleteTest}} only checks the
update count, and its procedure has no OUT parameter.
*Fix:* call {{beginStore()}} in {{StoredProcedureQuery.executeQuery()}} when a
transaction is active, like native and bulk updates do. Outside a transaction
nothing changes.
Note: having to register the OUT parameter with
{{registerStoredProcedureParameter}} in 4.2.0 is correct, all parameters of a
dynamically specified stored procedure query must be registered (3.11.12.2).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)