学习Spring事务操作-学习

This commit is contained in:
2026-10-19 08:59:46 +08:00
parent c745b77dba
commit 3ad44dfc1c
5 changed files with 19 additions and 52 deletions
@@ -12,6 +12,6 @@ public class Main {
public static void main(String[] args) throws SQLException {
ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
DeptService deptService = context.getBean(DeptService.class);
deptService.update();
deptService.test();
}
}
@@ -1,12 +1,10 @@
package com.ycjy.config;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@@ -37,17 +35,6 @@ public class Application {
return dataSource;
}
@Bean
@Primary //主要
public DataSource transactionAwareDataSource(DataSource dataSource) {
return new TransactionAwareDataSourceProxy(dataSource);
}
@Bean
public QueryRunner qr(DataSource transactionAwareDataSource){
return new QueryRunner(transactionAwareDataSource);
}
@Bean
public PlatformTransactionManager txManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
@@ -1,27 +1,16 @@
package com.ycjy.dao;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
@Repository
public class DeptDao1 {
// @Autowired
// private JdbcTemplate jdbcTemplate;
//
// public void insert(String d1){
// String sql = "insert into a1 values('"+d1+"')";
// jdbcTemplate.update(sql);
// }
@Autowired
private QueryRunner qr;
private JdbcTemplate jdbcTemplate;
public void insert(String d1) throws SQLException {
public void insert(String d1){
String sql = "insert into a1 values('"+d1+"')";
qr.update(sql);
jdbcTemplate.update(sql);
}
}
@@ -1,28 +1,17 @@
package com.ycjy.dao;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
@Repository
public class DeptDao2 {
// @Autowired
// private JdbcTemplate jdbcTemplate;
//
// public void insert(String d2){
//// throw new RuntimeException("自抛异常测试========");
// String sql = "insert into userinfo(username) values('"+d2+"')";
// jdbcTemplate.update(sql);
// }
@Autowired
private QueryRunner qr;
private JdbcTemplate jdbcTemplate;
public void insert(String d2) throws SQLException {
String sql = "insert into userinfo(username) values('"+d2+"')";
qr.update(sql);
public void insert(String d2){
// 故意写错 sql(多了 where),触发异常,测试事务回滚
String sql = "insert into userinfo(username) values('"+d2+"') where";
jdbcTemplate.update(sql);
}
}
@@ -16,15 +16,17 @@ public class DeptService {
@Autowired
private DeptDao2 deptDao2;
@Transactional
@Transactional(rollbackFor = RuntimeException.class)
public void update() throws SQLException {
try{
deptDao1.insert("q===");
deptDao2.insert("q===" );
throw new RuntimeException("自抛异常");
} catch (Exception e) {
System.out.println("内容: " + e.getMessage());
}
deptDao1.insert("b1--");
deptDao2.insert("b2--");
}
// @Transactional 自调用(实际上是目标对象内的方法调用目标对象的另一个方法)在运行时不会导致实际的事务
// 所以想要让本方法得到调用就得重新写 @Transactional 原方法的失效
@Transactional
public void test() throws SQLException {
update();
}
}
// 事务传播