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

Lillian ·
更新时间:2024-11-15
· 507 次阅读

代码如下:
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;
 }

}

您可能感兴趣的文章:springboot使用JdbcTemplate完成对数据库的增删改查功能详解spring boot中使用JdbcTemplateSpringBoot JdbcTemplate批量操作的示例代码spring的jdbctemplate的crud的基类daoJava 使用JdbcTemplate 中的queryForList发生错误解决办法使用jdbcTemplate查询返回自定义对象集合代码示例Spring 中jdbcTemplate 实现执行多条sql语句示例Spring Boot中使用jdbctemplate 操作MYSQL数据库实例SpringBoot使用JdbcTemplate操作数据库JdbcTemplate方法介绍与增删改查操作实现



jdbctemplate spring 示例 事务

需要 登录 后方可回复, 如果你还没有账号请 注册新账号