From 5eed1ecc90ee296fcb5c996887b006e5fb201f81 Mon Sep 17 00:00:00 2001 From: Jody Grassel Date: Mon, 3 Feb 2014 21:07:50 +0000 Subject: [PATCH] OPENJPA-2450: Option to disable execution of ALTER SEQUENCE...INCREMENT BY statement for sequences. git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.3.x@1564058 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/kernel/NativeJDBCSeq.java | 27 ++++++++++++++----- .../apache/openjpa/jdbc/sql/DBDictionary.java | 1 + .../openjpa/jdbc/kernel/localizer.properties | 11 +++++++- .../src/doc/manual/ref_guide_dbsetup.xml | 24 +++++++++++++++++ 4 files changed, 55 insertions(+), 8 deletions(-) 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 856fded65..11703f96d 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 @@ -83,6 +83,7 @@ public class NativeJDBCSeq private boolean alterIncrementBy = false; private boolean alreadyLoggedAlterSeqFailure = false; + private boolean alreadyLoggedAlterSeqDisabled = false; /** * The sequence name. Defaults to OPENJPA_SEQUENCE. @@ -219,17 +220,29 @@ public class NativeJDBCSeq try { if (!alterIncrementBy) { DBDictionary dict = _conf.getDBDictionaryInstance(); - // 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) { + if (!dict.disableAlterSeqenceIncrementBy) { + // 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)); + } + } + alreadyLoggedAlterSeqFailure = true; + _allocate = 1; + } + } else { + if (!alreadyLoggedAlterSeqDisabled) { Log log = _conf.getLog(OpenJPAConfiguration.LOG_RUNTIME); if (log.isWarnEnabled()) { - log.warn(_loc.get("fallback-no-seq-cache", _seqName)); + log.warn(_loc.get("alter-seq-disabled", _seqName)); } } - alreadyLoggedAlterSeqFailure = true; - _allocate = 1; + + alreadyLoggedAlterSeqDisabled = true; } } _nextValue = getSequence(conn); diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index 1e645ecc7..776dd2cd9 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -219,6 +219,7 @@ public class DBDictionary public boolean setStringRightTruncationOn = true; // sql + public boolean disableAlterSeqenceIncrementBy=false; public String validationSQL = null; public String closePoolSQL = null; public String initializationSQL = null; diff --git a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties index d40a27e3f..2422caf4d 100644 --- a/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties +++ b/openjpa-jdbc/src/main/resources/org/apache/openjpa/jdbc/kernel/localizer.properties @@ -175,4 +175,13 @@ exclude-pagination: Query "{0}" is not cached because it uses pagination. fallback-no-seq-cache: Unable to cache sequence values for sequence "{0}". \ Your application does not have permission to run an ALTER SEQUENCE \ command. Ensure that it has the appropriate permission to run an \ - ALTER SEQUENCE command. \ No newline at end of file + ALTER SEQUENCE command. +alter-seq-disabled: The property "openjpa.jdbc.DBDictionary=disableAlterSeqenceIncrementBy" \ + is set to true. This means that the ''ALTER SEQUENCE...INCREMENT BY'' SQL statement \ + will not be executed for sequence "{0}". OpenJPA executes this command to ensure that \ + the sequence''s INCREMENT BY value defined in the database matches the allocationSize \ + which is defined in the entity''s sequence. With this SQL statement disabled, it is the \ + responsibility of the user to ensure that the entity''s sequence definition matches the \ + sequence defined in the database. + + \ No newline at end of file diff --git a/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml b/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml index fe5ead58e..65161d432 100644 --- a/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml +++ b/openjpa-project/src/doc/manual/ref_guide_dbsetup.xml @@ -1453,6 +1453,30 @@ generated by the mappingtool. about identifiers that have been delimited. It defaults to preserving the case of the originally specified name. Available values are: upper, lower, preserve. + + + + + + + SQL + + + DisableAlterSeqenceIncrementBy + + +DisableAlterSeqenceIncrementBy: OpenJPA attempts to execute +an ALTER SEQUENCE....INCREMENT BY SQL statement for a user defined sequence. This +is done to ensure that the 'allocationSize' value defined by the entity's sequence, +or default value, matches the sequence defined in the database. For example, with +an allocationSize of 1000 for a sequence named 'SEQ_JPASAMPLE', the following SQL +will be generated (the SQL might vary slightly depending on the databases): +ALTER SEQUENCE SEQ_JPASAMPLE INCREMENT BY 1000. If the user +executing this command doesn't have permissions to execute the command, it will +fail and in turn OpenJPA will disable sequence caching. If a user wants to disable +this SQL command, this property can be set to true. However, the user must ensure +that the entities defined sequence is kept in synch with the sequence defined in the +database. Defaults to false.