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..f6936ee8cc --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/procedure/TestStoredProcedureTransaction.java @@ -0,0 +1,154 @@ +/* + * 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.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.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; + +/** + * 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 deletes rows and returns their number in an + * OUT parameter, which made the connection it ran on leave auto-commit mode. + */ +class TestStoredProcedureTransaction extends SingleEMFTestCase { + + private static final String PROCEDURE = "SPQ_DELETE_MARKED"; + + @Override + protected boolean isSupported(DBDictionary dict) { + return createProcedureSQL(dict) != null; + } + + @BeforeEach + public void setUp() throws Exception { + setUp("openjpa.Optimistic", "true"); + 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(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 + void changesAreCommittedWithTheTransaction() { + try (EntityManager em = emf.createEntityManager()) { + em.getTransaction().begin(); + assertEquals(2L, deleteMarked(em)); + em.getTransaction().commit(); + } + + assertEquals(1, countRows()); + } + + @Test + void changesAreRolledBackWithTheTransaction() { + try (EntityManager em = emf.createEntityManager()) { + em.getTransaction().begin(); + assertEquals(2L, deleteMarked(em)); + em.getTransaction().rollback(); + } + + assertEquals(3, countRows()); + } + + private long deleteMarked(EntityManager em) { + StoredProcedureQuery proc = em.createStoredProcedureQuery(PROCEDURE); + proc.registerStoredProcedureParameter(1, Long.class, ParameterMode.OUT); + proc.execute(); + return ((Number) proc.getOutputParameterValue(1)).longValue(); + } + + private int countRows() { + try (EntityManager em = emf.createEntityManager()) { + return ((Number) em.createNativeQuery("SELECT COUNT(*) FROM SPQ_TX_ROW").getSingleResult()).intValue(); + } + } + + private void exec(String sql, boolean ignoreExceptions) throws Exception { + 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; + } + } + } + } +} 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");