spring-注解----transaction
package com.zwj.config; import java.beans.PropertyVetoException; import javax.sql.DataSource; import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator; import org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator; 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.AnnotationTransactionAttributeSource; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration; import org.springframework.transaction.annotation.TransactionManagementConfigurationSelector; import org.springframework.transaction.annotation.Transactional; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * 声明式事务: * * 环境搭建: * 1、导入相关依赖 * 数据源、数据库驱动、Spring-jdbc模块 * 2、配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)操作数据 * 3、给方法上标注 @Transactional 表示当前方法是一个事务方法; * 4、 @EnableTransactionManagement 开启基于注解的事务管理功能; * @EnableXXX * 5、配置事务管理器来控制事务; * @Bean * public PlatformTransactionManager transactionManager() * * * 原理: * 1)、@EnableTransactionManagement * 利用TransactionManagementConfigurationSelector给容器中会导入组件 * 导入两个组件 * AutoProxyRegistrar * ProxyTransactionManagementConfiguration * 2)、AutoProxyRegistrar: * 给容器中注册一个 InfrastructureAdvisorAutoProxyCreator 组件; * InfrastructureAdvisorAutoProxyCreator:? * 利用后置处理器机制在对象创建以后,包装对象,返回一个代理对象(增强器),代理对象执行方法利用拦截器链进行调用; * * 3)、ProxyTransactionManagementConfiguration 做了什么? * 1、给容器中注册事务增强器; * 1)、事务增强器要用事务注解的信息,AnnotationTransactionAttributeSource解析事务注解 * 2)、事务拦截器: * TransactionInterceptor;保存了事务属性信息,事务管理器; * 他是一个 MethodInterceptor; * 在目标方法执行的时候; * 执行拦截器链; * 事务拦截器: * 1)、先获取事务相关的属性 * 2)、再获取PlatformTransactionManager,如果事先没有添加指定任何transactionmanger * 最终会从容器中按照类型获取一个PlatformTransactionManager; * 3)、执行目标方法 * 如果异常,获取到事务管理器,利用事务管理回滚操作; * 如果正常,利用事务管理器,提交事务 * */ @EnableTransactionManagement @ComponentScan("com.zwj.tx") @Configuration public class TxConfig { //数据源 @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/test"); return dataSource; } // @Bean public JdbcTemplate jdbcTemplate() throws Exception{ //Spring对@Configuration类会特殊处理;给容器中加组件的方法,多次调用都只是从容器中找组件 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); return jdbcTemplate; } //注册事务管理器在容器中 @Bean public PlatformTransactionManager transactionManager() throws Exception{ return new DataSourceTransactionManager(dataSource()); } }
package com.zwj.tx; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class UserDao { @Autowired private JdbcTemplate jdbcTemplate; public void insert(){ String sql = "INSERT INTO `tbl_user`(username,age) VALUES(?,?)"; String username = UUID.randomUUID().toString().substring(0, 5); jdbcTemplate.update(sql, username,19); } }
package com.zwj.tx; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class UserService { @Autowired private UserDao userDao; @Transactional public void insertUser(){ userDao.insert(); //otherDao.other();xxx System.out.println("插入完成..."); int i = 10/0; } }
package com.zwj.test; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.zwj.config.TxConfig; import com.zwj.tx.UserService; public class IOCTest_Tx { @Test public void test01(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TxConfig.class); UserService userService = applicationContext.getBean(UserService.class); userService.insertUser(); applicationContext.close(); } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zwj</groupId> <artifactId>spring-annotation---transactions</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> </dependencies> </project>