其他分享
首页 > 其他分享> > @Transactional、@EnableTransactionManagement事务

@Transactional、@EnableTransactionManagement事务

作者:互联网

/**
 * 声明式事务:
 *
 * 环境搭建:
 * 1、导入相关依赖
 *         数据源、数据库驱动、jdbc模块
 * 2、配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)操作数据
 * 3、给方法上标注 @Transactional 表示当前方法是一个事务方法;(也可以标注在类上)
 * 4、 @EnableTransactionManagement 开启基于注解的事务管理功能;
 *         @EnableXXX
 * 5、配置事务管理器来控制事务;
 *         @Bean
 *         public PlatformTransactionManager transactionManager()
 *     
 */    

配置类


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;

@EnableTransactionManagement @Configuration @ComponentScan("com.example.studywork.tx") public class TxConfig { @Bean public DataSource dataSource() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser("XXXXXXX"); dataSource.setPassword("XXXXXXXXX"); dataSource.setDriverClass("oracle.jdbc.OracleDriver"); dataSource.setJdbcUrl("jdbc:oracle:thin:@XXX.XX.XX.XXX:1521/ORCL"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() throws Exception{ return new JdbcTemplate(dataSource()); } @Bean public PlatformTransactionManager transactionManager() throws Exception { return new DataSourceTransactionManager(dataSource()); } }

业务类

@Repository
public class TxDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void saveData(String name,String age){
        StringBuffer sql = new StringBuffer("insert into test_tbl (id,username,age) values (?,?,?)");
        jdbcTemplate.update(sql.toString(), UUID.randomUUID().toString().substring(0,5),name,age);
    }
}
@Service
@Transactional
public class TxService {

    @Autowired
    private TxDao txDao;

    public void saveInfo(){// 随机生成name
        String name = UUID.randomUUID().toString().substring(2,6);
        txDao.saveData(name,"10");
        System.out.println("保存完成");
        // 模拟报错
        int i = 1/0;
    }
}

访问类

@Test
    public void test() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TxConfig.class);
        TxService txService = applicationContext.getBean(TxService.class);
        txService.saveInfo();
    }

pom

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>1.4.1</version>
        </dependency>

 

 

如果try和@Transactional一起用需要注意到事务不能回滚问题

例如1:

@Service
@Transactional
public class TxService {

    @Autowired
    private TxDao txDao;

    public void saveInfo(){
        try {
            // 随机生成name
            String name = UUID.randomUUID().toString().substring(2,6);
            txDao.saveData(name,"10");
            System.out.println("保存完成");
            // 模拟报错
            int i = 1/0;
        }catch (Exception e){
            e.printStackTrace();
            // 手动添加回滚操作
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }
}

需要在catch 中手动添加回滚操作

例如2:

@Service
@Transactional
public class TxService {

    @Autowired
    private TxDao txDao;

    public void saveInfo(){
        try {
            // 随机生成name
            String name = UUID.randomUUID().toString().substring(2,6);
            txDao.saveData(name,"10");
            System.out.println("保存完成");
            // 模拟报错
            int i = 1/0;
        }catch (Exception e){
            e.printStackTrace();
            //catch中抛出运行时异常
            throw new RuntimeException();
        }
    }
}

指定异常抛出

 

标签:事务,name,Transactional,springframework,public,dataSource,org,import,EnableTransac
来源: https://www.cnblogs.com/dabo-tian/p/16434516.html