com.jpattern.orm.jdbctemplate.JdbcTemplateTransaction Maven / Gradle / Ivy
package com.jpattern.orm.jdbctemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import com.jpattern.orm.exception.OrmException;
import com.jpattern.orm.transaction.Transaction;
import com.jpattern.orm.transaction.TransactionDefinition;
import com.jpattern.orm.transaction.TransactionIsolation;
import com.jpattern.orm.transaction.TransactionPropagation;
/**
*
* @author Francesco Cina
*
* 18/giu/2011
*/
public class JdbcTemplateTransaction implements Transaction {
/**
*
*/
private final TransactionStatus transactionStatus;
private final PlatformTransactionManager platformTransactionManager;
public JdbcTemplateTransaction(final PlatformTransactionManager platformTransactionManager, final TransactionDefinition transactionDefinition) {
this.platformTransactionManager = platformTransactionManager;
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
setIsolationLevel(definition , transactionDefinition.getIsolationLevel());
setPropagation( definition , transactionDefinition.getPropagation() );
setTimeout( definition , transactionDefinition.getTimeout() );
definition.setReadOnly( transactionDefinition.isReadOnly() );
this.transactionStatus = platformTransactionManager.getTransaction(definition);
}
private void setTimeout(final DefaultTransactionDefinition definition, final int timeout) {
if (timeout >= 0) {
definition.setTimeout(timeout);
}
}
private void setPropagation(final DefaultTransactionDefinition definition, final TransactionPropagation propagation) {
switch (propagation) {
case MANDATORY:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_MANDATORY );
break;
case NESTED:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_NESTED );
break;
case NEVER:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_NEVER );
break;
case NOT_SUPPORTED:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_NOT_SUPPORTED );
break;
case REQUIRED:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_REQUIRED );
break;
case REQUIRES_NEW:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW );
break;
case SUPPORTS:
definition.setPropagationBehavior( DefaultTransactionDefinition.PROPAGATION_SUPPORTS );
break;
default:
throw new OrmException("Unknown Transaction Propagation: " + propagation);
}
}
private void setIsolationLevel(final DefaultTransactionDefinition definition,
final TransactionIsolation isolationLevel) {
if (isolationLevel!=TransactionIsolation.DEFAULT) {
definition.setIsolationLevel(isolationLevel.getTransactionIsolation());
}
}
@Override
public void setRollbackOnly() throws OrmException {
try {
this.transactionStatus.setRollbackOnly();
} catch (Exception e) {
throw new OrmException(e);
}
}
@Override
public void commit() throws OrmException {
try {
if (!this.transactionStatus.isCompleted()) {
this.platformTransactionManager.commit(this.transactionStatus);
}
} catch (Exception e) {
throw new OrmException(e);
}
}
@Override
public void rollback() throws OrmException {
try {
if (!this.transactionStatus.isCompleted()) {
this.platformTransactionManager.rollback(this.transactionStatus);
}
} catch (Exception e) {
throw new OrmException(e);
}
}
@Override
public boolean isClosed() {
return this.transactionStatus.isCompleted();
}
}