spring如何实现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;
}

}

本文spring如何实现jdbctemplate添加事务支持示例到此结束。人生要有几个朋友:一个是年少受欺负时,帮你出头的;一个是落魄时,愿意借钱给你的;一个是失意时,愿意陪着你的;一个是落寞时,在事业上帮忙你的;一个是住院时,亲自来探望你的;一个是你走完时,愿意参加你葬礼的。最重要的一个是,喜欢真诚的你的自我。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
SpringBoot集成JWT

Spring MVC之DispatcherServlet_动力节点Java学院整理

Spring mvc 分步式session的实例详解

JSP 开发之Spring BeanUtils组件如何使用

详解Spring的核心机制依赖注入