[OPENPJA-2742] Rollback before DDL can be disabled - default behavior is kept

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1834818 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Francesco Chicchiriccò 2018-07-02 06:06:45 +00:00
parent e5f3551a81
commit 0303fb28f8
5 changed files with 83 additions and 15 deletions

View File

@ -40,6 +40,7 @@ import org.apache.openjpa.util.MultiLoaderClassResolver;
* <li><code>action</code></li>
* <li><code>meta</code></li>
* <li><code>schemaAction</code></li>
* <li><code>rollbackBeforeDDL</code></li>
* <li><code>dropTables</code></li>
* <li><code>ignoreErrors</code></li>
* <li><code>readSchema</code></li>
@ -101,6 +102,13 @@ public class MappingToolTask
flags.dropTables = dropTables;
}
/**
* Set whether the MappingTool should rollback will be performed before each DDL statement is executed.
*/
public void setRollbackBeforeDDL(boolean rollbackBeforeDDL) {
flags.rollbackBeforeDDL = rollbackBeforeDDL;
}
/**
* Set whether to drop OpenJPA tables.
*/

View File

@ -129,6 +129,7 @@ public class MappingTool
private boolean _seqs = true;
private boolean _dropUnused = true;
private boolean _ignoreErrors = false;
private boolean _rollbackBeforeDDL = false;
private File _file = null;
private Writer _mappingWriter = null;
private Writer _schemaWriter = null;
@ -320,6 +321,20 @@ public class MappingTool
public boolean getIgnoreErrors() {
return _ignoreErrors;
}
/**
* If true, rollback will be performed before each DDL statement is executed. Defaults to true.
*/
public boolean getRollbackBeforeDDL() {
return _rollbackBeforeDDL;
}
/**
* If true, rollback will be performed before each DDL statement is executed. Defaults to true.
*/
public void setRollbackBeforeDDL(boolean rollbackBeforeDDL) {
_rollbackBeforeDDL = rollbackBeforeDDL;
}
/**
* Return the schema tool to use for schema modification.
@ -333,6 +348,7 @@ public class MappingTool
tool.setForeignKeys(getForeignKeys());
tool.setIndexes(getIndexes());
tool.setSequences(getSequences());
tool.setRollbackBeforeDDL(getRollbackBeforeDDL());
return tool;
}
@ -532,6 +548,7 @@ public class MappingTool
// configure the tool with additional settings
if (flags != null) {
tool.setDropTables(flags.dropTables);
tool.setRollbackBeforeDDL(flags.rollbackBeforeDDL);
tool.setDropSequences(flags.dropSequences);
tool.setWriter(flags.sqlWriter);
tool.setOpenJPATables(flags.openjpaTables);
@ -1011,6 +1028,8 @@ public class MappingTool
flags.schemaAction);
flags.dropTables = opts.removeBooleanProperty
("dropTables", "dt", flags.dropTables);
flags.rollbackBeforeDDL = opts.removeBooleanProperty
("rollbackBeforeDDL", "rbddl", flags.rollbackBeforeDDL);
flags.openjpaTables = opts.removeBooleanProperty
("openjpaTables", "ot", flags.openjpaTables);
flags.dropSequences = opts.removeBooleanProperty
@ -1128,6 +1147,7 @@ public class MappingTool
tool.setForeignKeys(flags.foreignKeys);
tool.setIndexes(flags.indexes);
tool.setSequences(flags.sequences || flags.dropSequences);
tool.setRollbackBeforeDDL(flags.rollbackBeforeDDL);
// and run the action
for (int i = 0; i < act.length; i++) {
@ -1178,6 +1198,7 @@ public class MappingTool
public boolean ignoreErrors = false;
public boolean readSchema = false;
public boolean dropTables = false;
public boolean rollbackBeforeDDL = false;
public boolean openjpaTables = false;
public boolean dropSequences = false;
public boolean sequences = true;

View File

@ -114,6 +114,7 @@ public class SchemaTool {
private boolean _fks = true;
private boolean _indexes = true;
private boolean _seqs = true;
private boolean _rollbackBeforeDDL = true;
private PrintWriter _writer = null;
private SchemaGroup _group = null;
private SchemaGroup _db = null;
@ -234,6 +235,20 @@ public class SchemaTool {
setSequences(true);
}
/**
* If true, rollback will be performed before each DDL statement is executed. Defaults to true.
*/
public boolean getRollbackBeforeDDL() {
return _rollbackBeforeDDL;
}
/**
* If true, rollback will be performed before each DDL statement is executed. Defaults to true.
*/
public void setRollbackBeforeDDL(boolean rollbackBeforeDDL) {
_rollbackBeforeDDL = rollbackBeforeDDL;
}
/**
* Whether sequences should be manipulated. Defaults to true.
*/
@ -1335,23 +1350,28 @@ public class SchemaTool {
Statement statement = null;
boolean wasAuto = true;
try {
wasAuto = conn.getAutoCommit();
if (!wasAuto)
conn.setAutoCommit(true);
if (_rollbackBeforeDDL) {
wasAuto = conn.getAutoCommit();
if (!wasAuto) {
conn.setAutoCommit(true);
}
}
for (int i = 0; i < sql.length; i++) {
try {
// some connections require that rollback be
// called on the connection before any DDL statements
// can be run on it, even when autocommit is on.
// This is sometimes because the connection does not
// allow DDL statements when there are multiple
// commands issued on the connection, and the
// connection pool may have issued some validation SQL.
try {
conn.rollback();
} catch (Exception e) {
if (_rollbackBeforeDDL) {
// some connections require that rollback be
// called on the connection before any DDL statements
// can be run on it, even when autocommit is on.
// This is sometimes because the connection does not
// allow DDL statements when there are multiple
// commands issued on the connection, and the
// connection pool may have issued some validation SQL.
try {
conn.rollback();
} catch (Exception e) {
}
}
statement = conn.createStatement();
statement.executeUpdate(sql[i]);
@ -1377,7 +1397,7 @@ public class SchemaTool {
}
}
finally {
if (!wasAuto) {
if (_rollbackBeforeDDL && !wasAuto) {
conn.setAutoCommit(false);
}
@ -1445,6 +1465,9 @@ public class SchemaTool {
* to true to drop sequences that appear to be unused during
* <code>retain</code> and <code>refresh</code> actions. Defaults to
* <code>true</code>.</li>
* <li><i>-rollbackBeforeDDL/-rbddl &lt;true/t | false/f&gt;</i>: Set this option
* to true to send an initail rollback on the connection before any DDL statement
* is sent</li>
* <li><i>-primaryKeys/-pk &lt;true/t | false/f&gt;</i>: Whether primary
* keys on existing tables are manipulated. Defaults to true.</li>
* <li><i>-foreignKeys/-fk &lt;true/t | false/f&gt;</i>: Whether foreign
@ -1539,6 +1562,8 @@ public class SchemaTool {
("dropTables", "dt", flags.dropTables);
flags.dropSequences = opts.removeBooleanProperty
("dropSequences", "dsq", flags.dropSequences);
flags.rollbackBeforeDDL = opts.removeBooleanProperty
("rollbackBeforeDDL", "rbddl", flags.rollbackBeforeDDL);
flags.ignoreErrors = opts.removeBooleanProperty
("ignoreErrors", "i", flags.ignoreErrors);
flags.openjpaTables = opts.removeBooleanProperty
@ -1654,6 +1679,7 @@ public class SchemaTool {
tool.setDropTables(flags.dropTables);
tool.setSequences(flags.sequences); // set before dropseqs
tool.setDropSequences(flags.dropSequences);
tool.setRollbackBeforeDDL(flags.rollbackBeforeDDL);
tool.setPrimaryKeys(flags.primaryKeys);
tool.setForeignKeys(flags.foreignKeys);
tool.setIndexes(flags.indexes);
@ -1692,6 +1718,7 @@ public class SchemaTool {
public String action = ACTION_ADD;
public Writer writer = null;
public boolean dropTables = true;
public boolean rollbackBeforeDDL = true;
public boolean dropSequences = true;
public boolean ignoreErrors = false;
public boolean openjpaTables = false;

View File

@ -5448,6 +5448,12 @@ dropped. Defaults to <literal>false</literal>.
</listitem>
<listitem>
<para>
<literal>-rollbackBeforeDDL/-rbddl &lt;true/t | false/f&gt;</literal>: Set this option to
<literal>true</literal> to send an initail rollback on the connection before any DDL statement is sent.
</para>
</listitem>
<listitem>
<para>
<literal>-dropTables/-dt &lt;true/t | false/f&gt;</literal>: Set this option to
<literal>true</literal> to drop tables that appear to be unused during <literal>
retain</literal> and <literal>refresh</literal> actions. Defaults to <literal>

View File

@ -133,6 +133,12 @@ different character instead of a semicolon.
<listitem>
<para>
<literal>-dropTables/-dt &lt;true/t | false/f&gt;</literal>: Corresponds to the
same-named option on the schema tool.
</para>
</listitem>
<listitem>
<para>
<literal>-rollbackBeforeDDL/-rbddl &lt;true/t | false/f&gt;</literal>: Corresponds to the
same-named option on the schema tool.
</para>
</listitem>