From 20ff0912e38c24a7c73a469b6d973e293d9b4844 Mon Sep 17 00:00:00 2001 From: Richard Zowalla Date: Fri, 27 Nov 2026 07:30:06 +0100 Subject: [PATCH 1/3] [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. --- .../jdbc/kernel/StoredProcedureQuery.java | 5 + .../TestStoredProcedureTransaction.java | 118 ++++++++++++++++++ .../jdbc/procedure/derby/Procedures.java | 13 ++ 3 files changed, 136 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/StoredProcedureQuery.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/StoredProcedureQuery.java index cfc28bfedc..9791cd8968 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/StoredProcedureQuery.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/StoredProcedureQuery.java @@ -174,6 +174,11 @@ public ResultObjectProvider executeQuery(StoreQuery q, Object[] params, Range ra boolean resetAutoCommit = false; try { DBDictionary dict = _store.getDBDictionary(); + // inside a transaction, run the procedure on the connection of the datastore + // transaction, as native and bulk updates do, so that commit and rollback apply to it + if (_store.getContext().isActive()) { + _store.getContext().beginStore(); + } conn = _store.getConnection(); // PostgreSQL requires auto-commit off for procedures diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java new file mode 100644 index 0000000000..2e0d46dbdb --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.openjpa.jdbc.procedure; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityTransaction; +import jakarta.persistence.ParameterMode; +import jakarta.persistence.StoredProcedureQuery; + +import org.apache.openjpa.jdbc.procedure.derby.Procedures; +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.jdbc.sql.DerbyDictionary; +import org.apache.openjpa.persistence.test.DatabasePlatform; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests that the changes of a stored procedure executed in an optimistic + * transaction are committed and rolled back with that transaction + * (OPENJPA-3100). The procedure has an OUT parameter, which made the + * connection it ran on leave auto-commit mode. + */ +@DatabasePlatform("org.apache.derby.jdbc.EmbeddedDriver") +class TestStoredProcedureTransaction extends SingleEMFTestCase { + + @Override + protected boolean isSupported(DBDictionary dict) { + return dict instanceof DerbyDictionary; + } + + @BeforeEach + public void setUp() throws Exception { + setUp("openjpa.Optimistic", "true"); + exec("DROP PROCEDURE SPQ_DELETE_MARKED", true); + exec("CREATE PROCEDURE SPQ_DELETE_MARKED(OUT DELETED BIGINT) " + + "PARAMETER STYLE JAVA LANGUAGE JAVA MODIFIES SQL DATA EXTERNAL NAME " + + "'" + Procedures.class.getName() + ".deleteMarked'", false); + exec("DROP TABLE SPQ_TX_ROW", true); + exec("CREATE TABLE SPQ_TX_ROW (ID INTEGER NOT NULL PRIMARY KEY, MARKED SMALLINT NOT NULL)", false); + exec("INSERT INTO SPQ_TX_ROW VALUES (1, 1), (2, 1), (3, 0)", false); + } + + @Test + void changesAreCommittedWithTheTransaction() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + assertEquals(2L, deleteMarked(em)); + em.getTransaction().commit(); + em.close(); + + assertEquals(1, countRows()); + } + + @Test + void changesAreRolledBackWithTheTransaction() { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + assertEquals(2L, deleteMarked(em)); + em.getTransaction().rollback(); + em.close(); + + assertEquals(3, countRows()); + } + + private long deleteMarked(EntityManager em) { + StoredProcedureQuery proc = em.createStoredProcedureQuery("SPQ_DELETE_MARKED"); + proc.registerStoredProcedureParameter(1, Long.class, ParameterMode.OUT); + proc.execute(); + return ((Number) proc.getOutputParameterValue(1)).longValue(); + } + + private int countRows() { + EntityManager em = emf.createEntityManager(); + try { + return ((Number) em.createNativeQuery("SELECT COUNT(*) FROM SPQ_TX_ROW").getSingleResult()).intValue(); + } finally { + em.close(); + } + } + + private void exec(String sql, boolean ignoreExceptions) throws Exception { + EntityManager em = emf.createEntityManager(); + EntityTransaction tx = em.getTransaction(); + tx.begin(); + try { + em.createNativeQuery(sql).executeUpdate(); + tx.commit(); + } catch (Exception e) { // DROP of an object that does not exist + if (tx.isActive()) { + tx.rollback(); + } + if (!ignoreExceptions) { + throw e; + } + } finally { + em.close(); + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/derby/Procedures.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/derby/Procedures.java index 7433942a27..0e53f07b8d 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/derby/Procedures.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/derby/Procedures.java @@ -21,6 +21,8 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import org.junit.jupiter.api.Assertions; @@ -47,6 +49,17 @@ public static void inout(int[] p) { p[0] = p[0] * 2; } + /** + * Deletes the marked rows of SPQ_TX_ROW through the connection of the caller + * and returns their number. + */ + public static void deleteMarked(long[] deleted) throws SQLException { + try (Connection c = DriverManager.getConnection("jdbc:default:connection"); + Statement s = c.createStatement()) { + deleted[0] = s.executeUpdate("DELETE FROM SPQ_TX_ROW WHERE MARKED = 1"); + } + } + public static void mapping(ResultSet[] r0, ResultSet[] r1) { Assertions.assertDoesNotThrow(() -> { Connection c = DriverManager.getConnection("jdbc:default:connection"); From 42c1601d502a135ba7f14799728b201054351c3e Mon Sep 17 00:00:00 2001 From: Richard Zowalla Date: Fri, 27 Nov 2026 07:40:27 +0100 Subject: [PATCH 2/3] [OPENJPA-3100] Run the stored procedure transaction test on all databases 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. --- .../TestStoredProcedureTransaction.java | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java index 2e0d46dbdb..71a795ccbd 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java @@ -26,9 +26,13 @@ import jakarta.persistence.StoredProcedureQuery; import org.apache.openjpa.jdbc.procedure.derby.Procedures; +import org.apache.openjpa.jdbc.sql.AbstractMySQLDictionary; +import org.apache.openjpa.jdbc.sql.AbstractSQLServerDictionary; import org.apache.openjpa.jdbc.sql.DBDictionary; import org.apache.openjpa.jdbc.sql.DerbyDictionary; -import org.apache.openjpa.persistence.test.DatabasePlatform; +import org.apache.openjpa.jdbc.sql.HSQLDictionary; +import org.apache.openjpa.jdbc.sql.OracleDictionary; +import org.apache.openjpa.jdbc.sql.PostgresDictionary; import org.apache.openjpa.persistence.test.SingleEMFTestCase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -36,27 +40,63 @@ /** * Tests that the changes of a stored procedure executed in an optimistic * transaction are committed and rolled back with that transaction - * (OPENJPA-3100). The procedure has an OUT parameter, which made the - * connection it ran on leave auto-commit mode. + * (OPENJPA-3100). The procedure deletes rows and returns their number in an + * OUT parameter, which made the connection it ran on leave auto-commit mode. */ -@DatabasePlatform("org.apache.derby.jdbc.EmbeddedDriver") class TestStoredProcedureTransaction extends SingleEMFTestCase { + private static final String PROCEDURE = "SPQ_DELETE_MARKED"; + @Override protected boolean isSupported(DBDictionary dict) { - return dict instanceof DerbyDictionary; + return createProcedureSQL(dict) != null; } @BeforeEach public void setUp() throws Exception { setUp("openjpa.Optimistic", "true"); - exec("DROP PROCEDURE SPQ_DELETE_MARKED", true); - exec("CREATE PROCEDURE SPQ_DELETE_MARKED(OUT DELETED BIGINT) " - + "PARAMETER STYLE JAVA LANGUAGE JAVA MODIFIES SQL DATA EXTERNAL NAME " - + "'" + Procedures.class.getName() + ".deleteMarked'", false); + DBDictionary dict = getDBDictionary(); + exec("DROP PROCEDURE " + PROCEDURE, true); exec("DROP TABLE SPQ_TX_ROW", true); exec("CREATE TABLE SPQ_TX_ROW (ID INTEGER NOT NULL PRIMARY KEY, MARKED SMALLINT NOT NULL)", false); - exec("INSERT INTO SPQ_TX_ROW VALUES (1, 1), (2, 1), (3, 0)", false); + exec(createProcedureSQL(dict), false); + exec("INSERT INTO SPQ_TX_ROW (ID, MARKED) VALUES (1, 1)", false); + exec("INSERT INTO SPQ_TX_ROW (ID, MARKED) VALUES (2, 1)", false); + exec("INSERT INTO SPQ_TX_ROW (ID, MARKED) VALUES (3, 0)", false); + } + + /** + * Returns the statement that creates the procedure on the given database, + * or null if the test does not support the database. + */ + private static String createProcedureSQL(DBDictionary dict) { + String delete = "DELETE FROM SPQ_TX_ROW WHERE MARKED = 1"; + if (dict instanceof DerbyDictionary) { + return "CREATE PROCEDURE " + PROCEDURE + "(OUT DELETED BIGINT) " + + "PARAMETER STYLE JAVA LANGUAGE JAVA MODIFIES SQL DATA EXTERNAL NAME " + + "'" + Procedures.class.getName() + ".deleteMarked'"; + } + if (dict instanceof PostgresDictionary) { + return "CREATE PROCEDURE " + PROCEDURE + "(OUT DELETED BIGINT) LANGUAGE plpgsql AS $$ BEGIN " + + delete + "; GET DIAGNOSTICS DELETED = ROW_COUNT; END $$"; + } + if (dict instanceof AbstractMySQLDictionary) { + return "CREATE PROCEDURE " + PROCEDURE + "(OUT DELETED BIGINT) BEGIN " + + delete + "; SET DELETED = ROW_COUNT(); END"; + } + if (dict instanceof AbstractSQLServerDictionary) { + return "CREATE PROCEDURE " + PROCEDURE + " @DELETED BIGINT OUTPUT AS BEGIN SET NOCOUNT ON; " + + delete + "; SET @DELETED = @@ROWCOUNT; END"; + } + if (dict instanceof OracleDictionary) { + return "CREATE OR REPLACE PROCEDURE " + PROCEDURE + "(DELETED OUT NUMBER) AS BEGIN " + + delete + "; DELETED := SQL%ROWCOUNT; END;"; + } + if (dict instanceof HSQLDictionary) { + return "CREATE PROCEDURE " + PROCEDURE + "(OUT DELETED BIGINT) MODIFIES SQL DATA BEGIN ATOMIC " + + delete + "; GET DIAGNOSTICS DELETED = ROW_COUNT; END"; + } + return null; } @Test @@ -82,7 +122,7 @@ void changesAreRolledBackWithTheTransaction() { } private long deleteMarked(EntityManager em) { - StoredProcedureQuery proc = em.createStoredProcedureQuery("SPQ_DELETE_MARKED"); + StoredProcedureQuery proc = em.createStoredProcedureQuery(PROCEDURE); proc.registerStoredProcedureParameter(1, Long.class, ParameterMode.OUT); proc.execute(); return ((Number) proc.getOutputParameterValue(1)).longValue(); From c5a16bf7167e7c7008dd8f5397f4c650b052a200 Mon Sep 17 00:00:00 2001 From: Richard Zowalla Date: Fri, 27 Nov 2026 09:51:39 +0100 Subject: [PATCH 3/3] [OPENJPA-3100] Close the entity managers of the stored procedure transaction test with try-with-resources --- .../TestStoredProcedureTransaction.java | 52 +++++++++---------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java index 71a795ccbd..f6936ee8cc 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java @@ -101,22 +101,22 @@ private static String createProcedureSQL(DBDictionary dict) { @Test void changesAreCommittedWithTheTransaction() { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals(2L, deleteMarked(em)); - em.getTransaction().commit(); - em.close(); + try (EntityManager em = emf.createEntityManager()) { + em.getTransaction().begin(); + assertEquals(2L, deleteMarked(em)); + em.getTransaction().commit(); + } assertEquals(1, countRows()); } @Test void changesAreRolledBackWithTheTransaction() { - EntityManager em = emf.createEntityManager(); - em.getTransaction().begin(); - assertEquals(2L, deleteMarked(em)); - em.getTransaction().rollback(); - em.close(); + try (EntityManager em = emf.createEntityManager()) { + em.getTransaction().begin(); + assertEquals(2L, deleteMarked(em)); + em.getTransaction().rollback(); + } assertEquals(3, countRows()); } @@ -129,30 +129,26 @@ private long deleteMarked(EntityManager em) { } private int countRows() { - EntityManager em = emf.createEntityManager(); - try { + try (EntityManager em = emf.createEntityManager()) { return ((Number) em.createNativeQuery("SELECT COUNT(*) FROM SPQ_TX_ROW").getSingleResult()).intValue(); - } finally { - em.close(); } } private void exec(String sql, boolean ignoreExceptions) throws Exception { - EntityManager em = emf.createEntityManager(); - EntityTransaction tx = em.getTransaction(); - tx.begin(); - try { - em.createNativeQuery(sql).executeUpdate(); - tx.commit(); - } catch (Exception e) { // DROP of an object that does not exist - if (tx.isActive()) { - tx.rollback(); - } - if (!ignoreExceptions) { - throw e; + try (EntityManager em = emf.createEntityManager()) { + EntityTransaction tx = em.getTransaction(); + tx.begin(); + try { + em.createNativeQuery(sql).executeUpdate(); + tx.commit(); + } catch (Exception e) { // DROP of an object that does not exist + if (tx.isActive()) { + tx.rollback(); + } + if (!ignoreExceptions) { + throw e; + } } - } finally { - em.close(); } } }