Skip to content

[OPENJPA-3100] Run a stored procedure in the datastore transaction when a transaction is active - #326

Open
rzo1 wants to merge 3 commits into
masterfrom
OPENJPA-3100
Open

rzo1 wants to merge 3 commits into
masterfrom
OPENJPA-3100

Conversation

@rzo1

@rzo1 rzo1 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/OPENJPA-3100

Reported on users@: since 4.2.0, the changes of a stored procedure with an OUT parameter are not committed by EntityTransaction.commit() when the transaction is optimistic (the default), unless OpenJPAEntityManager.beginStore() is called first.

tx.begin();
StoredProcedureQuery proc = em.createStoredProcedureQuery("delete_marked_files");
proc.registerStoredProcedureParameter(1, Long.class, ParameterMode.OUT);
proc.execute();
proc.getOutputParameterValue(1);
tx.commit(); // the deleted rows are still there

Cause: in an optimistic transaction there is no datastore transaction until the first flush, and StoredProcedureQuery.executeQuery() did not start one, so the procedure ran 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 (88e7709) the connection leaves auto-commit mode when the procedure has OUT parameters and is only committed in XROP.close(), which does not run before the EntityManager closes the connection, so the changes were rolled back.

Fix: when a transaction is active, call beginStore() before taking the connection, as native queries (SQLStoreQuery) and bulk updates (JDBCStoreQuery) already do. The procedure then runs on the connection of the transaction, and commit and rollback apply to it. Outside a transaction nothing changes, including the REF_CURSOR handling.

Reproduced on PostgreSQL 18 with a PL/pgSQL procedure that deletes rows and has a BIGINT OUT parameter:

without beginStore(), after commit after rollback
4.1.1 deleted deleted
4.2.0 / master not deleted not deleted
this PR deleted not deleted

Tests: new TestStoredProcedureTransaction (commit and rollback of a procedure with an OUT parameter in an optimistic transaction) creates the procedure on Derby, PostgreSQL, MySQL, MariaDB, SQL Server, Oracle and HSQLDB, so the database builds run it; H2 is skipped (no SQL procedures with OUT parameters). Run locally on all of them: it passes with the fix and fails without it (on SQL Server the changes even survived the rollback). On Derby, the stored procedure tests in org.apache.openjpa.jdbc.procedure, TestNativeQueryProcedures, TestEntityManagerTCKCompliance and the openjpa-lib, openjpa-kernel and openjpa-jdbc suites pass.

TCK and the database builds pass on this branch: TCK https://github.com/apache/openjpa/actions/runs/45953368744, Databases https://github.com/apache/openjpa/actions/runs/45953372759 (on 42c1601; the later commit only changes how the test closes its entity managers).

…en 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.
@rzo1
rzo1 requested review from cristof and solomax November 27, 2026 06:31
…ases of the database builds

Create the procedure for PostgreSQL, MySQL, MariaDB, SQL Server, Oracle and
HSQLDB as well as for Derby, so that the database builds run the test.
H2 is skipped, it has no SQL procedures with OUT parameters.

@Test
void changesAreCommittedWithTheTransaction() {
EntityManager em = emf.createEntityManager();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe try-with-resource should be used in this test?

@rzo1
rzo1 marked this pull request as ready for review November 27, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants