亲宝软件园·资讯

展开

jdbctemplate事务 spring实现jdbctemplate添加事务支持示例

人气:0
想了解spring实现jdbctemplate添加事务支持示例的相关内容吗,在本文为您仔细讲解jdbctemplate事务的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:jdbctemplate,事务,下面大家一起来学习吧。

复制代码 代码如下:

public interface JdbcTemplate extends JdbcOperations {
public abstract void beginTranstaion();

public abstract void commit();

public abstract void rollback();
}

复制代码 代码如下:

public class JdbcTemplateImpl extends org.springframework.jdbc.core.JdbcTemplate implements JdbcTemplate {

 private PlatformTransactionManager platformTransactionManager;
 private DefaultTransactionDefinition transactionDefinition;
 private ThreadLocal<TransactionStatus> transcationStatus = new ThreadLocal<TransactionStatus>();

 public void beginTranstaion() {
  TransactionStatus tmp = platformTransactionManager.getTransaction(transactionDefinition);
  transcationStatus.set(tmp);
 }

 public void commit() {
  TransactionStatus tmp = transcationStatus.get();
  if (tmp == null) {
   throw new RMSRuntimeException("no transcation");
  }
  platformTransactionManager.commit(tmp);
  transcationStatus.remove();
 }

 public void rollback() {
  TransactionStatus tmp = transcationStatus.get();
  if (tmp == null) {
   throw new RMSRuntimeException("no transcation");
  }
  platformTransactionManager.rollback(tmp);
  transcationStatus.remove();

 }

 @Override
 public void afterPropertiesSet() {
  super.afterPropertiesSet();
  transactionDefinition = new DefaultTransactionDefinition();
  transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
  transactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  platformTransactionManager = new DataSourceTransactionManager(getDataSource());

 }

 public PlatformTransactionManager getPlatformTransactionManager() {
  return platformTransactionManager;
 }

 public DefaultTransactionDefinition getTransactionDefinition() {
  return transactionDefinition;
 }

 public ThreadLocal<TransactionStatus> getTranscationStatus() {
  return transcationStatus;
 }

}

加载全部内容

相关教程
猜你喜欢
用户评论