#805 - ENH: Add BeanState.resetForInsert() ... This resets bean state such that a save() results in an insert

This commit is contained in:
Robin Bygrave
2016-08-04 22:49:07 +12:00
parent 876476fdc1
commit 08e46d6bf3
11 changed files with 114 additions and 17 deletions
@@ -68,11 +68,11 @@ public abstract class BeanRequest {
public void rollbackTransIfRequired() {
if (createdTransaction) {
try {
transaction.rollback();
transaction.rollbackIfActive();
} catch (Exception e) {
// Just log this and carry on. A previous exception has been
// thrown and if this rollback throws exception it likely means
// that the connection is broken (and the datasource and db will cleanup)
// that the connection is broken (and the dataSource and db will cleanup)
log.error("Error trying to rollback a transaction (after a prior exception thrown)", e);
}
}
@@ -84,4 +84,9 @@ public class DefaultBeanState implements BeanState {
public boolean isDisableLazyLoad() {
return intercept.isDisableLazyLoad();
}
@Override
public void resetForInsert() {
intercept.setNew();
}
}
@@ -821,7 +821,7 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
}
/**
* Add the bean to the TransactionEvent. This will be used by TransactionManager to synch Cache,
* Add the bean to the TransactionEvent. This will be used by TransactionManager to sync Cache,
* Cluster and text indexes.
*/
private void addEvent() {
@@ -41,7 +41,7 @@ final class TransWrapper {
void rollbackIfCreated() {
if (wasCreated){
transaction.rollback();
transaction.rollbackIfActive();
}
}
@@ -913,28 +913,27 @@ public class JdbcTransaction implements SpiTransaction {
throw new IllegalStateException(illegalStateMessage);
}
firePreCommit();
try {
if (queryOnly) {
// can rollback or just close for performance
connectionEndForQueryOnly();
} else {
// commit
if (batchControl != null && !batchControl.isEmpty()) {
batchControl.flush();
}
firePreCommit();
// only performCommit can throw an exception
performCommit();
firePostCommit();
notifyCommit();
}
} catch (Exception e) {
doRollback(e);
throw new RollbackException(e);
} finally {
// these will not throw an exception
firePostCommit();
deactivate();
notifyCommit();
}
}
@@ -959,6 +958,16 @@ public class JdbcTransaction implements SpiTransaction {
this.rollbackOnly = true;
}
/**
* Perform rollback is the transaction is still active.
*/
@Override
public void rollbackIfActive() {
if (isActive()) {
rollback(null);
}
}
/**
* Rollback the transaction.
*/
@@ -976,17 +985,26 @@ public class JdbcTransaction implements SpiTransaction {
if (!isActive()) {
throw new IllegalStateException(illegalStateMessage);
}
try {
doRollback(cause);
} finally {
deactivate();
}
}
/**
* Perform the jdbc rollback and fire any registered callbacks.
*/
private void doRollback(Throwable cause) {
firePreRollback();
try {
performRollback();
} catch (Exception ex) {
} catch (SQLException ex) {
throw new PersistenceException(ex);
} finally {
// these will not throw an exception
firePostRollback();
deactivate();
notifyRollback(cause);
}
}