欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-13 18:13:28
复制代码 代码如下:public interface jdbctemplate extends jdbcoperations {public abstract void b...

复制代码 代码如下:

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

}