ENH: Add Transaction.rollbackAndContinue()

Typically useful for handling DuplicateKeyException where we expect
DuplicateKeyException to be thrown and catch it with the intention of
continuing processing using the same transaction.

Note that some databases like Oracle do not require this explicit
rollback() and would work without the rollbackAndContinue(). Postgres
in particular requires the rollback() call on the underlying connection
such that we can continue using that transaction/java.sql.Connection.

Note that in the existing test we can see that rollbackAndContinue()
is pretty close to being syntactic sugar. I think adding rollbackAndContinue()
is justified and complements the existing commitAndContinue().
This commit is contained in:
Rob Bygrave committed 2022-09-01 17:08:11 +12:00
1 parent 563340edbd
commit 7df168f232
6 files changed
+96 -28

No files matched your search

@@ -240,7 +240,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
@Override
public boolean isReadOnly() {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -252,7 +252,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
@Override
public void setReadOnly(boolean readOnly) {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -403,7 +403,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
*/
@Override
public void setPersistenceContext(SpiPersistenceContext context) {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
this.persistenceContext = context;
@@ -465,7 +465,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
*/
@Override
public Connection getInternalConnection() {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
return connection;
@@ -508,7 +508,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
*/
@Override
public void commit() {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
// expect AutoCommit so just deactivate / put back into pool
@@ -532,6 +532,11 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
throw new IllegalStateException(notExpectedMessage);
}
@Override
public void rollbackAndContinue() {
// do nothing
}
/**
* Rollback the transaction.
*/
@@ -546,7 +551,7 @@ final class ImplicitReadOnlyTransaction implements SpiTransaction, TxnProfileEve
*/
@Override
public void rollback(Throwable cause) throws PersistenceException {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
// expect AutoCommit so it really has already committed
@@ -424,7 +424,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
@Override
public boolean isReadOnly() {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -436,7 +436,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
@Override
public void setReadOnly(boolean readOnly) {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -459,7 +459,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
@Override
public final void setBatchMode(boolean batchMode) {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
this.batchMode = batchMode;
@@ -472,7 +472,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
@Override
public final void setBatchOnCascade(boolean batchMode) {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
this.batchOnCascadeMode = batchMode;
@@ -666,7 +666,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
*/
@Override
public final void flush() {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
internalBatchFlush();
@@ -707,7 +707,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
*/
@Override
public final void setPersistenceContext(SpiPersistenceContext context) {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
this.persistenceContext = context;
@@ -776,7 +776,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
*/
@Override
public Connection getInternalConnection() {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
return connection;
@@ -920,7 +920,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
if (rollbackOnly) {
return;
}
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -945,7 +945,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
rollback();
return;
}
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -1014,6 +1014,22 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
this.nestedUseSavepoint = true;
}
@Override
public void rollbackAndContinue() {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
internalBatchClear();
if (changeLogHolder != null) {
changeLogHolder.clear();
}
try {
performRollback();
} catch (SQLException ex) {
throw new PersistenceException(ex);
}
}
/**
* Rollback the transaction.
*/
@@ -1028,7 +1044,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
*/
@Override
public void rollback(Throwable cause) throws PersistenceException {
if (!isActive()) {
if (!active) {
throw new IllegalStateException(illegalStateMessage);
}
try {
@@ -1066,7 +1082,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
*/
@Override
public void end() throws PersistenceException {
if (isActive()) {
if (active) {
rollback();
}
}
@@ -1075,7 +1091,7 @@ class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
* Return true if the transaction is active.
*/
@Override
public boolean isActive() {
public final boolean isActive() {
return active;
}
@@ -96,4 +96,7 @@ public final class TChangeLogHolder {
owner.sendChangeLog(changes);
}
void clear() {
changes.getChanges().clear();
}
}