Spring 事务

tech2023-10-28  107

Pom.xml 

数据库使用mysql,连接池使用c3p0

<!-- 添加c3p0 dataSource 连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- mysql的数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.44</version> </dependency> <!-- spring的数据库jdbc连接 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.0.6.RELEASE</version> </dependency>

数据库相关配置

DataSource

使用连接池 连接 数据库,则许向Spring容器中注入DataSource

@Bean public DataSource dataSource() throws PropertyVetoException { //c3p0提供的数据库连接池的实现类 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); return dataSource; }

JdbcTemplate

使用spring提供的JdbcTemplate来操作数据库,则许向Spring容器中注入JdbcTemplate

@Bean public JdbcTemplate jdbcTemplate() throws PropertyVetoException { return new JdbcTemplate(dataSource()); }

事务的相关配置

@Transactional

@Transactional可以添加在 类 或者 方法上, 如果想让事务生效必须启用事务管理器(@EnableTransactionManagement) @Service public class PersonService { @Autowired private PersonDao personDao; @Transactional public void insert(){ personDao.insert(); System.out.println("插入完成.................."); int a = 1/0; } }

@EnableTransactionManagement

启用事务管理器,只有表明了此注解,事务才可以生效

@Configuration @ComponentScan(value = {"com.lb.spring.day6.part19"}) @EnableTransactionManagement public class MyConfigration19 { }

PlatformTransactionManager接口

当在代码中只使用了注解@EnableTransactionManagement 和 @Transactional时,会报错缺少PlatformTransactionManager

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334) at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:394) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:284) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at com.lb.spring.day6.part19.service.PersonService$$EnhancerBySpringCGLIB$$dc956731.insert(<generated>) at day6.Part19Test.test(Part19Test.java:13)

则必须需要配置事务管理器

//注册事务管理器 @Bean public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException{ return new DataSourceTransactionManager(dataSource()); }

通过配置以下三项,则事务即可生效

 @EnableTransactionManagement @TransactionalPlatformTransactionManager

Demo:

执行前:

@Configuration @ComponentScan(value = {"com.lb.spring.day6.part19"}) @EnableTransactionManagement public class MyConfigration19 { @Bean public DataSource dataSource() throws PropertyVetoException { //c3p0提供的数据库连接池的实现类 ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() throws PropertyVetoException { return new JdbcTemplate(dataSource()); } @Bean public PlatformTransactionManager transactionManager() throws PropertyVetoException { return new DataSourceTransactionManager(dataSource()); } } @Repository public class PersonDao { @Autowired private JdbcTemplate jdbcTemplate; public void insert(){ String sql = "insert into person_test(name,birthday) values(?,?)"; jdbcTemplate.update(sql,"lb",new Date()); } } @Service public class PersonService { @Autowired private PersonDao personDao; @Transactional public void insert(){ personDao.insert(); System.out.println("插入完成.................."); int a = 1/0; } } public class Part19Test { @Test public void test() throws Exception { AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(MyConfigration19.class); PersonService personService = app.getBean(PersonService.class); personService.insert(); app.close(); } }

结果:因为事务中报错,则rollback未插入成功

插入完成.................. java.lang.ArithmeticException: / by zero

最新回复(0)