1.Spring 的事务抽象
Spring提供了一致的事务模型
- JDBC/Hibernate/Mybatis 操作数据
- DataSource/JTA 事务
2.事务抽象的核心接口
PlatformTransactionManage
- DateSourceTransactionManage
- HibernateTransactionManage
- JtaTransactionManage
TransactionDefinition
- Propagation
- lsolation
- Timeout
- Read-only status
3.事务传播特性7个
4.事务隔离特性
5.编程式事务
在spring 提供的事务里面经常用下面两种进行事务的操作
TransactionTemplate
- TransactionCallback
- TransactionCallbackWithoutResult
PlatformTransactionManage
- 可以传入TransactionDefinition
6.测试编程式事务
@SpringBootApplication public class DemoApplication implements CommandLineRunner { @Autowired private TransactionTemplate transactionTemplate; @Autowired private JdbcTemplate jdbcTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("事务执行前---》"+getCount()); transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) { //事务执行中 jdbcTemplate.execute("insert into user(id,username,password) values(1,1,1)"); System.out.println("事务执行中---》"+getCount()); //回滚W transactionStatus.setRollbackOnly(); } }); System.out.println("事务执行后---》"+getCount()); } //统计数据条数 public long getCount() { return jdbcTemplate.queryForObject("select count(*) from user", Long.class); } }
7.申明式事务
8.基于注解的配置方式
开启事务注解的方式 @EnableTransactionManagement
@Transactional
- transactionManage
- propagation
- isolation
- timeout
- readOnly
- 判断回滚