spring-tx : 声明式事务的框架
声明式事务是指使用注解或 XML 配置的方式来控制事务的提交和回滚
开发者只需要添加配置即可, 具体事务的实现由第三方框架实现
程序员:
配置文件即可(注解、xml)
指定哪些方法需要添加事务
以及事务的属性
编程式事务与声明式事事务 区别:
使用声明式事务可以将事务的控制和业务逻辑分离开来
Spring声明式事务对应依赖

现在要使用的事务管理器是org.springframework.jdbc.datasource.DataSourceTransactionManager,将来整合 JDBC方式、JdbcTemplate方式、Mybatis方式的事务实现!
DataSourceTransactionManager类中的主要方法:

org.springframework spring-tx 6.0.6 org.springframework spring-jdbc 6.0.6
doug.url=jdbc:mysql://localhost:3306/studb doug.driver=com.mysql.cj.jdbc.Driver doug.username=root doug.password=root
spring配置文件
package com.doug.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
@Configuration
@PropertySource("classpath:jdbc.properties")
@ComponentScan("com.doug")
public class MyConfiguration {
@Value("${doug.url}")
private String url;
@Value("${doug.driver}")
private String driver;
@Value("${doug.username}")
private String username;
@Value("${doug.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setDriverClassName(driver);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
}
@Repository
public class StudentDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void updateNameById(String name,Integer id){
String sql = "update students set name = ? where id = ? ;";
int rows = jdbcTemplate.update(sql, name, id);
}
public void updateAgeById(Integer age,Integer id){
String sql = "update students set age = ? where id = ? ;";
jdbcTemplate.update(sql,age,id);
}
}
service
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
public void changeInfo(){
studentDao.updateAgeById(100,1);
System.out.println("-----------");
studentDao.updateNameById("test1",1);
}
}
@SpringJUnitConfig(MyConfiguration.class)
public class SpringTxTest {
@Autowired
private StudentService studentService;
@Test
public void TxTest(){
studentService.changeInfo();
}
}
@Configuration
@PropertySource("classpath:jdbc.properties")
@ComponentScan("com.doug")
@EnableTransactionManagement // 开启声明式事物管理
public class MyConfiguration {
@Value("${doug.url}")
private String url;
@Value("${doug.driver}")
private String driver;
@Value("${doug.username}")
private String username;
@Value("${doug.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
dataSource.setDriverClassName(driver);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
/**
* 装配事务管理实现对象
* @param dataSource
* @return
*/
@Bean
public TransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
return transactionManager;
}
}
service 层 类方法 添加事务
/**
* 添加事务
* @Transactional
* 位置: 方法 | 类上
* 方法: 当前方法有事务
* 类上: 类下的所有方法都有事务
*/
@Transactional
public void changeInfo(){
studentDao.updateAgeById(88,1);
// 开启事务后 , 这里报错 会进行回滚,前后功能都不会执行
int i = 88/0; // 搞一个报错
System.out.println("-----------");
studentDao.updateNameById("test2",1);
}
报错 且 数据库无变化,说明已经开启了事务


只读模式:

@Transactional
public class StudentService {
@Transactional(readOnly = true)
public void getStudentInfo(){
}
}
概括来说就是一句话:超时回滚,释放资源。
/**
* 超时时间:
* 默认:永远不超时 -1
* 设置timeout = 时间 秒数 超过时间,就会回滚事务和释放异常! org.springframework.transaction.TransactionTimedOutException: Transaction timed out: deadline
* 如果类上设置事务属性,方法也设置了事务注解!
* 不会生效!,方法上有默认的设置属性会覆盖类上的属性设置!
*/
@Transactional(timeout = 3)
public void changeInfo(){
studentDao.updateAgeById(88,1);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("-----------");
studentDao.updateNameById("test2",1);
}

超时,后回滚 数据库无变化

指定异常回滚 和 指定异常不回滚

数据库事务的隔离级别是指在多个事务并发执行时,数据库系统为了保证数据一致性所遵循的规定。常见的隔离级别包括:
isolation = 设置事务的隔离级别,mysql默认是repeatable read!
建议可以设置为:第二种级别 Read Committed
在被调用的子方法中设置传播行为,代表如何处理调用的事务! 是加入,还是新事务等!


| 名称 | 含义 |
|---|---|
| REQUIRED 默认值 | 如果父方法有事务,就加入,如果没有就新建自己独立! |
| REQUIRES_NEW | 不管父方法是否有事务,我都新建事务,都是独立的! |
声明两个独立修改数据库的事务业务方法:
注意:
在同一个类中,对于@Transactional注解的方法调用,事务传播行为不会生效。
这是因为Spring框架中使用代理模式实现了事务机制,在同一个类中的方法调用并不经过代理,而是通过对象的方法调用,因此@Transactional注解的设置不会被代理捕获,也就不会产生任何事务传播行为的效果。
| 核心点 | 掌握目标 |
|---|---|
| spring框架理解 | spring家族和spring framework框架 |
| spring核心功能 | ioc/di , aop , tx |
| spring ioc / di | 组件管理、ioc容器、ioc/di , 三种配置方式 |
| spring aop | aop和aop框架和代理技术、基于注解的aop配置 |
| spring tx | 声明式和编程式事务、动态事务管理器、事务注解、属性 |
