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

这篇文章主要介绍了spring实现jdbctemplate添加事务支持示例,重写JdbcTemplate增加beginTranstaion,commit,rollback方法,需要的朋友可以参考下

复制代码 代码如下:


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;
 }

}

您可能感兴趣的文章:

相关文章

最新评论

站长推荐

正版 Windows 10

正版Windows 10 家庭/专业版,操作系统限时抢购[¥1088→¥248]

站长推荐

正版 Office 软件

Microsoft Office 2016/2019/365 正版最低价仅需[ ¥148元]

大家感兴趣的内容

最近更新的内容

常用在线小工具

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfgsj.html