This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openjpa.git


The following commit(s) were added to refs/heads/master by this push:
     new c5b9734  OPENJPA-2614 run ALTER SEQUENCE in a separate tx
c5b9734 is described below

commit c5b97348fcfcb77d1a0c00813bc4c18bf85140f7
Author: Mark Struberg <strub...@apache.org>
AuthorDate: Wed Jun 16 19:54:25 2021 +0200

    OPENJPA-2614 run ALTER SEQUENCE in a separate tx
    
    Quite a lot databases force a commit on DDL changes.
    For sequences we try to run an ALTER SEQUENCE to make the sequence
    reflect the allocationSize. Doing this will commit all outstanding open
    DB changes in Oracle and a few other databases.
    We now open a new connection.
---
 .../apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java  | 13 +++++++++++--
 .../apache/openjpa/jdbc/kernel/NativeJDBCSeq.java    | 20 +++++++++++++-------
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git 
a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
 
b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
index ef364a4..98de566 100644
--- 
a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
+++ 
b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/AbstractJDBCSeq.java
@@ -149,6 +149,14 @@ public abstract class AbstractJDBCSeq
         return (JDBCStore) ctx.getStoreManager().getInnermostDelegate();
     }
 
+
+    /**
+     * @see #getConnection(JDBCStore, boolean) but without forcing a 
connection.
+     */
+    protected Connection getConnection(JDBCStore store) throws SQLException {
+        return getConnection(store, false);
+    }
+
     /**
      * <P>Return the connection to use based on the type of sequence. This
      * connection will automatically be closed; do not close it.</P>
@@ -160,10 +168,11 @@ public abstract class AbstractJDBCSeq
      * <P>Otherwise a new connection will be obtained using DataSource2 from 
the
      * current configuration. In this case autocommit is set to false prior to
      * returning the connection.</P>
+     * @param forceNewConnection if {@code true} a new connection will be 
forced
      */
-    protected Connection getConnection(JDBCStore store)
+    protected Connection getConnection(JDBCStore store, boolean 
forceNewConnection)
         throws SQLException {
-        if (type == TYPE_TRANSACTIONAL || type == TYPE_CONTIGUOUS) {
+        if (!forceNewConnection && (type == TYPE_TRANSACTIONAL || type == 
TYPE_CONTIGUOUS)) {
             // Also increments ref count.
             return store.getConnection();
         }
diff --git 
a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java 
b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
index 8369b4a..d15852d 100644
--- 
a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
+++ 
b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/NativeJDBCSeq.java
@@ -227,15 +227,21 @@ public class NativeJDBCSeq
                     // If this fails, we will warn the user at most one time 
and set _allocated and _increment to 1 so
                     // as to not potentially insert records ahead of what the 
database thinks is the next sequence
                     // value.
-                    if (updateSql(conn, dict.getAlterSequenceSQL(_seq)) == -1) 
{
-                        if (!alreadyLoggedAlterSeqFailure) {
-                            Log log = 
_conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
-                            if (log.isWarnEnabled()) {
-                                log.warn(_loc.get("fallback-no-seq-cache", 
_seqName));
+
+                    // first we have to allocate a new connection as some 
databases do an implicit commit
+                    // if a DDL gets changed. Others do blow up on a DDL change
+                    try (Connection newConn = getConnection(store, true)) {
+                        if (updateSql(newConn, dict.getAlterSequenceSQL(_seq)) 
== -1) {
+                            newConn.commit(); // new connection has 
autoCommit=false
+                            if (!alreadyLoggedAlterSeqFailure) {
+                                Log log = 
_conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
+                                if (log.isWarnEnabled()) {
+                                    log.warn(_loc.get("fallback-no-seq-cache", 
_seqName));
+                                }
                             }
+                            alreadyLoggedAlterSeqFailure = true;
+                            _allocate = 1;
                         }
-                        alreadyLoggedAlterSeqFailure = true;
-                        _allocate = 1;
                     }
                 } else {
                     if (!alreadyLoggedAlterSeqDisabled) {

Reply via email to