基于Spring注解的事务管理
步骤:
Spring注解式事务编程:
- 1)导入数据源、数据库驱动、以及Spring管理数据库操作的工具类JdbcTemplate
- 2)向spring容器中注入dataSource和JdbcTemplate
- 3)在业务方法上面添加@@Transactional注解
- 4)还需要开启的基于spring注解的方式@EnableTransactionManagement
- 5)还需要向spring容器注入PlatformTransactionManager事务管理器他是一个接口类需要通过他的实现类DataSourceTransactionManager获取到实例
主配置类
@EnableTransactionManagement
//进行包扫描
@ComponentScan("com.ldp.Srping_Annotation.tx")
@Configuration
public class MyCinfigTx {
@Bean
public DataSource dataSource() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/temp");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() throws Exception {
//注意:spring对配置文件会有特殊处理,所以在这里调用别的注入bean的方法,并不是创建的bean而是对此的一种引用而已
JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource());
return jdbcTemplate;
}
@Bean
public PlatformTransactionManager platformTransactionManager() throws Exception {
return new DataSourceTransactionManager(dataSource());
}
}
Dao层
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert() {
String sql="insert into user(name,email) values(?,?)";
//获取随机的名称
String name =UUID.randomUUID().toString().substring(0, 5);
jdbcTemplate.update(sql,name,name+"@qq.com");
}
}
Service层
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Transactional
public void insert() {
userDao.insert();
System.out.println("插入完成");
}
}
测试类
package com.ldp.Srping_Annotation;
//这是一个测试事务管理的测试类
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.ldp.Srping_Annotation.config.MyCinfigTx;
import com.ldp.Srping_Annotation.config.MyConfigAutowride;
import com.ldp.Srping_Annotation.tx.UserService;
public class TxTest {
private ApplicationContext applicationContext;
private UserService userService;
@Before
public void init() {
applicationContext=new AnnotationConfigApplicationContext(MyCinfigTx.class);
userService=applicationContext.getBean(UserService.class);
}
@Test
public void testPrintf() {
//获取容器当中所有的id名称
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String string : beanDefinitionNames) {
System.out.println(string);
}
}
@Test
public void testTX() {
userService.insert();
}
}
原理
* 原理:
* 1)、@EnableTransactionManagement
* 利用TransactionManagementConfigurationSelector给容器中会导入组件
* 导入两个组件
* AutoProxyRegistrar
* ProxyTransactionManagementConfiguration
* 2)、AutoProxyRegistrar:
* 给容器中注册一个 InfrastructureAdvisorAutoProxyCreator 组件;
* InfrastructureAdvisorAutoProxyCreator:?
* 利用后置处理器机制在对象创建以后,包装对象,返回一个代理对象(增强器),代理对象执行方法利用拦截器链进行调用;
*
* 3)、ProxyTransactionManagementConfiguration 做了什么?
* 1、给容器中注册事务增强器;
* 1)、事务增强器要用事务注解的信息,AnnotationTransactionAttributeSource解析事务注解
* 2)、事务拦截器:
* TransactionInterceptor;保存了事务属性信息,事务管理器;
* 他是一个 MethodInterceptor;
* 在目标方法执行的时候;
* 执行拦截器链;
* 事务拦截器:
* 1)、先获取事务相关的属性
* 2)、再获取PlatformTransactionManager,如果事先没有添加指定任何transactionmanger
* 最终会从容器中按照类型获取一个PlatformTransactionManager;
* 3)、执行目标方法
* 如果异常,获取到事务管理器,利用事务管理回滚操作;
* 如果正常,利用事务管理器,提交事务