Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f29d167
commit 68b771d
Showing
3 changed files
with
185 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
- [装配Bean](./装配Bean.md) | ||
- [高级装配](./高级装配.md) | ||
- [AOP](./AOP.md) | ||
- [事务](./事务.md) | ||
|
||
- ## SpringMVC | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# 事务 | ||
|
||
## 属性 | ||
|
||
- read-only:是否是只读事务。默认false,不只读。 | ||
- isolation:指定事务的隔离级别。默认值是使用数据库的默认隔离级别。 | ||
- propagation:指定事务的传播行为。 | ||
- timeout:指定超时时间。默认值为:-1。永不超时。 | ||
- rollback-for:用于指定一个异常,当执行产生该异常时,事务回滚。产生其他异常,事务不回滚。没有默认值,任何异常都回滚。 | ||
- no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时,事务回滚。没有默认值,任何异常都回滚。 | ||
|
||
### 传播行为 | ||
|
||
- PROPAGATION_REQUIRED | ||
|
||
![](https://docs.spring.io/spring/docs/5.2.1.RELEASE/spring-framework-reference/images/tx_prop_required.png) | ||
|
||
简单来说就是两个被事务管理的方法都将在同一个事务内执行 | ||
|
||
- PROPAGATION_REQUIRES_NEW | ||
|
||
![](https://docs.spring.io/spring/docs/5.2.1.RELEASE/spring-framework-reference/images/tx_prop_requires_new.png) | ||
|
||
而这个传播行为则是开启一个新事务 | ||
|
||
- PROPAGATION_NESTED | ||
|
||
该传播行为则是与JDBC的保存点一样,它使用了物理事务的保存点的概念 | ||
|
||
## 使用xml进行配置 | ||
|
||
- 声明式事务 | ||
- 声明式事务都是基于编程事务 | ||
|
||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
https://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/aop | ||
https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> | ||
|
||
<bean class="wang.ismy.spring.Dao"> | ||
<constructor-arg ref="template"/> | ||
</bean> | ||
|
||
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> | ||
<property name="username" value="root"/> | ||
<property name="password" value="123"/> | ||
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> | ||
<property name="url" value="jdbc:mysql:///mybatis"/> | ||
</bean> | ||
|
||
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate"> | ||
<constructor-arg name="dataSource" ref="dataSource"/> | ||
</bean> | ||
|
||
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> | ||
<property name="dataSource" ref="dataSource"/> | ||
</bean> | ||
|
||
<!--配置事务的通知--> | ||
<tx:advice id="txAdvice" transaction-manager="transactionManager"> | ||
<tx:attributes> | ||
<tx:method name="transfer" propagation="REQUIRED" read-only="false"/> | ||
</tx:attributes> | ||
</tx:advice> | ||
|
||
<!--配置aop--> | ||
<aop:config> | ||
<aop:pointcut id="txPt" expression="execution(* wang.ismy.spring.Dao.*(..))"/> | ||
<!--建立切入点表达式与事务通知的关系--> | ||
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPt"/> | ||
</aop:config> | ||
</beans> | ||
``` | ||
|
||
```java | ||
public class Dao { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
public Dao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void transfer(){ | ||
String sql = "UPDATE account SET money = money -200 WHERE uid = 41"; | ||
String sql1 = "UPDATE account SET money = money +200 WHERE uid = 45"; | ||
jdbcTemplate.update(sql); | ||
jdbcTemplate.update(sql1); | ||
} | ||
} | ||
``` | ||
|
||
## 注解配置 | ||
|
||
```java | ||
@Configuration | ||
@EnableTransactionManagement | ||
public class Config { | ||
|
||
@Bean | ||
public DataSource dataSource(){ | ||
DriverManagerDataSource dataSource = new DriverManagerDataSource(); | ||
dataSource.setUsername("root"); | ||
dataSource.setPassword("123"); | ||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); | ||
dataSource.setUrl("jdbc:mysql:///mybatis"); | ||
return dataSource; | ||
} | ||
|
||
@Bean | ||
public JdbcTemplate jdbcTemplate(DataSource dataSource){ | ||
return new JdbcTemplate(dataSource); | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager transactionManager(DataSource dataSource){ | ||
return new DataSourceTransactionManager(dataSource); | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
@Service | ||
public class Dao { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
public Dao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
@Transactional(rollbackFor = Exception.class) | ||
public void transfer(){ | ||
String sql = "UPDATE account SET money = money -200 WHERE uid = 41"; | ||
String sql1 = "UPDATE account SET money = money +200 WHERE uid = 45"; | ||
|
||
jdbcTemplate.update(sql); | ||
jdbcTemplate.update(sql1); | ||
} | ||
} | ||
``` | ||
|
||
- 编程式事务 | ||
|
||
```java | ||
@Bean | ||
public TransactionTemplate transactionTemplate(PlatformTransactionManager manager){ | ||
return new TransactionTemplate(manager); | ||
} | ||
``` | ||
|
||
```java | ||
@Service | ||
public class Dao { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
private TransactionTemplate transactionTemplate; | ||
|
||
public Dao(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
public void transfer(){ | ||
transactionTemplate.execute((TransactionCallback<Void>) status -> { | ||
String sql = "UPDATE account SET money = money -200 WHERE uid = 41"; | ||
String sql1 = "UPDATE account SET money = money +200 WHERE uid = 45"; | ||
jdbcTemplate.update(sql); | ||
jdbcTemplate.update(sql1); | ||
return null; | ||
}); | ||
} | ||
|
||
@Autowired | ||
public void setTransactionTemplate(TransactionTemplate transactionTemplate) { | ||
this.transactionTemplate = transactionTemplate; | ||
} | ||
} | ||
``` |