程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> spring實現jdbctemplate添加事務支持示例

spring實現jdbctemplate添加事務支持示例

編輯:關於JSP

復制代碼 代碼如下:
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;
 }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved