.
This the Hibernate's default transaction strategy.
| Method from org.hibernate.transaction.JDBCTransaction Detail: |
public void begin() throws HibernateException {
if (begun) {
return;
}
if (commitFailed) {
throw new TransactionException("cannot re-start transaction after failed commit");
}
log.debug("begin");
try {
toggleAutoCommit = jdbcContext.connection().getAutoCommit();
if ( log.isDebugEnabled() ) {
log.debug("current autocommit status: " + toggleAutoCommit);
}
if (toggleAutoCommit) {
log.debug("disabling autocommit");
jdbcContext.connection().setAutoCommit(false);
}
}
catch (SQLException e) {
log.error("JDBC begin failed", e);
throw new TransactionException("JDBC begin failed: ", e);
}
callback = jdbcContext.registerCallbackIfNecessary();
begun = true;
committed = false;
rolledBack = false;
if ( timeout >0 ) {
jdbcContext.getConnectionManager()
.getBatcher()
.setTransactionTimeout(timeout);
}
jdbcContext.afterTransactionBegin(this);
}
|
public void commit() throws HibernateException {
if (!begun) {
throw new TransactionException("Transaction not successfully started");
}
log.debug("commit");
if ( !transactionContext.isFlushModeNever() && callback ) {
transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback()
}
notifyLocalSynchsBeforeTransactionCompletion();
if ( callback ) {
jdbcContext.beforeTransactionCompletion( this );
}
try {
commitAndResetAutoCommit();
log.debug("committed JDBC Connection");
committed = true;
if ( callback ) {
jdbcContext.afterTransactionCompletion( true, this );
}
notifyLocalSynchsAfterTransactionCompletion( Status.STATUS_COMMITTED );
}
catch (SQLException e) {
log.error("JDBC commit failed", e);
commitFailed = true;
if ( callback ) {
jdbcContext.afterTransactionCompletion( false, this );
}
notifyLocalSynchsAfterTransactionCompletion( Status.STATUS_UNKNOWN );
throw new TransactionException("JDBC commit failed", e);
}
finally {
closeIfRequired();
}
}
|
public boolean isActive() {
return begun && ! ( rolledBack || committed | commitFailed );
}
|
public void registerSynchronization(Synchronization sync) throws HibernateException {
if (sync==null) throw new NullPointerException("null Synchronization");
if (synchronizations==null) {
synchronizations = new ArrayList();
}
synchronizations.add(sync);
}
|
public void rollback() throws HibernateException {
if (!begun && !commitFailed) {
throw new TransactionException("Transaction not successfully started");
}
log.debug("rollback");
if (!commitFailed) {
/*notifyLocalSynchsBeforeTransactionCompletion();
if ( callback ) {
jdbcContext.notifyLocalSynchsBeforeTransactionCompletion( this );
}*/
try {
rollbackAndResetAutoCommit();
log.debug("rolled back JDBC Connection");
rolledBack = true;
notifyLocalSynchsAfterTransactionCompletion(Status.STATUS_ROLLEDBACK);
}
catch (SQLException e) {
log.error("JDBC rollback failed", e);
notifyLocalSynchsAfterTransactionCompletion(Status.STATUS_UNKNOWN);
throw new TransactionException("JDBC rollback failed", e);
}
finally {
if ( callback ) {
jdbcContext.afterTransactionCompletion( false, this );
}
closeIfRequired();
}
}
}
|
public void setTimeout(int seconds) {
timeout = seconds;
}
|
public boolean wasCommitted() {
return committed;
}
|
public boolean wasRolledBack() {
return rolledBack;
}
|