事務管理
資料庫事務(Database Transaction) ,是指作為單個邏輯工作單元執行的一系列操作,要麼完全地執行,要麼完全地不執行。 事務處理可以確定除非事務性單元内的所有操作都成功完成,否則不會永久更新面向資料的資源。通過将一組相關操作組合為一個要麼全部成功要麼全部失敗的單元,可以簡化錯誤恢複并使應用程式更加可靠。一個邏輯工作單元要成為事務,必須滿足所謂的ACID(原子性、一緻性、隔離性和持久性)屬性。事務是資料庫運作中的邏輯工作機關,由DBMS中的事務管理子系統負責事務的處理。
案例準備
Dao
@Repository
public class UserDao {
@Autowired
JdbcTemplate jdbcTemplate;
/**
* 添加資料
*/
public void add(){
int i = jdbcTemplate.update("insert into t_sysuser(id,uname,nickname)values(seq_t_sysuser.nextval,?,?)","aaa","測試");
System.out.println("添加資料.."+i);
}
/**
* 修改資料
*/
public void udpate(){
int i = jdbcTemplate.update("update t_sysuser set uname=? ,nickname=? where id=?","bbb","呵呵",133);
System.out.println("修改資料.."+i);
}
}
Service層
@Service
public class UserService {
@Autowired
private UserDao dao;
public void fun(){
dao.add();
dao.udpate();
}
}
配置檔案配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 開啟掃描 -->
<context:component-scan base-package="com.dpb.*"></context:component-scan>
<!-- 配置資料源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="username" value="pms"/>
<property name="password" value="pms"/>
</bean>
<!-- 配置JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
</beans>
測試
兩個資料庫操作方法都沒問題的情況下都可以
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5iN3ATNhdDNhJmNklzNlNWYwImZ0AzYiVmYjJTYyYWZm9CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
當第二個方法出錯的情況下
事務的使用
xml配置聲明式事務
Spring事務的傳播行為
Spring事務的隔離級别
配置檔案
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 開啟掃描 -->
<context:component-scan base-package="com.dpb.*"></context:component-scan>
<!-- 配置資料源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="username" value="pms"/>
<property name="password" value="pms"/>
</bean>
<!-- 配置JdbcTemplate -->
<bean class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<!--
Spring中,使用XML配置事務三大步驟:
1. 建立事務管理器
2. 配置事務方法
3. 配置AOP
-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="fun*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- aop配置 -->
<aop:config>
<aop:pointcut expression="execution(* *..service.*.*(..))" id="tx"/>
<aop:advisor advice-ref="advice" pointcut-ref="tx"/>
</aop:config>
</beans>
注解的方式使用