[
https://issues.apache.org/jira/browse/OPENJPA-3100?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151874#comment-18151874
]
ASF subversion and git services commented on OPENJPA-3100:
----------------------------------------------------------
Commit 20ff0912e38c24a7c73a469b6d973e293d9b4844 in openjpa's branch
refs/heads/OPENJPA-3100 from Richard Zowalla
[ https://gitbox.apache.org/repos/asf?p=openjpa.git;h=20ff0912e ]
[OPENJPA-3100] Run a stored procedure in the datastore transaction when a
transaction is active
In an optimistic transaction there is no datastore transaction until the
first flush, so a stored procedure ran on a separate connection. Since
4.2.0 that connection leaves auto-commit mode when the procedure has OUT
parameters and is only committed when the result provider is closed,
which does not happen before the EntityManager closes the connection, so
the changes of the procedure were rolled back. Begin the datastore
transaction first, as native queries and bulk updates do, so that commit
and rollback of the transaction apply to the procedure.
> 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
> Priority: Major
> 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)