javascript
Spring事务操作-事务引入
目錄
Spring事務操作-事務引入
1.模擬異常
2.測試異常
3.沒有使用spring框架的時候異常該如何處理
4.使用spring框架的時候異常該如何處理
5.在spring 進行聲明式事務管理,底層使用AOP
6.spring 事務管理API
7.事務操作(注解聲明式事務管理)
(1)在spring的配置文件中配置事務管理器
(2)在spring 配置文件中,開啟事務注解
(3)在service 類上面(或者service類里面的方法上面) 添加事務注解
(4)@Transactional,這個注解添加到類上面,也可以添加到方法上面
(5)測試:
?
?上一章的代碼,如果正常執行,? ?不會出現問題
?
如果在代碼執行過程中出現異常,會出現問題
?
因此,我們引入事務這個概念
?
Spring事務操作-事務引入
?
1.模擬異常
在業務邏輯層中的service層中模擬異常
package org.example.spring.service;import org.example.spring.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service public class UserService {@Autowiredprivate UserDao userDao;// 轉賬方法調用public void accountMoney(){//lucy少100userDao.reduceMoney();//模擬異常int i=10/0;//mary多100userDao.addMoney();}}?
2.測試異常
原來的數據庫值:
后來的數據庫值:
分析:lucy的錢少了,mary 的錢卻沒有增加
?
3.沒有使用spring框架的時候異常該如何處理
package org.example.spring.service;import org.example.spring.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Service public class UserService {@Autowiredprivate UserDao userDao;// 轉賬方法調用public void accountMoney(){ // 1.開啟事務try{ // 2.進行業務操作//lucy少100userDao.reduceMoney();//模擬異常int i=10/0;//mary多100userDao.addMoney();// 3.沒有發生異常,提交事務}catch(Exception e){ // 4.如果發生了異常,從第3跳到第4,進行事務回滾}}}?
4.使用spring框架的時候異常該如何處理
(1)事務添加到javaEE三層結構里面的service層 (業務邏輯層)
(2)在spring 進行事務管理操作有兩種方式:
【第一種】編程式事務管理:相當于3. 里面的操作
【第二種】聲明式事務管理(常用):
1.基于注解方式(常用)
2.基于xml 配置方式
?
5.在spring 進行聲明式事務管理,底層使用AOP
?
6.spring 事務管理API
(1)spring 提供了一個接口,代表是事務管理器(事務操作都封裝在里面)
這個接口針對不同的框架提供不同的實現類,例如以下的紅框就是整合JDBC框架的子類 實現類
?
7.事務操作(注解聲明式事務管理)
(1)在spring的配置文件中配置事務管理器
<?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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 開啟組件掃描--><context:component-scan base-package="org.example"></context:component-scan> <!--數據庫連接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"><property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="sise"/><property name="driverClassName" value="com.mysql.jdbc.Driver"/></bean><!-- 創建jdbcTemplate對象--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--需要注入數據源信息--><property name="dataSource" ref="dataSource"></property></bean><!-- 創建事務管理器--> <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property> </bean> </beans>?
(2)在spring 配置文件中,開啟事務注解
引入名稱空間tx
開啟事務注解
?
?
(3)在service 類上面(或者service類里面的方法上面) 添加事務注解
?
(4)@Transactional,這個注解添加到類上面,也可以添加到方法上面
如果把這個注解添加類上面,這個類里面所有的方法都添加事務
如果把這個注解添加方法上面,僅僅為這個方法添加事務
package org.example.spring.service;import org.example.spring.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;@Service @Transactional public class UserService {@Autowiredprivate UserDao userDao;// 轉賬方法調用public void accountMoney(){ //1.開啟事務 // try{ // 2.進行業務操作//lucy少100userDao.reduceMoney();//模擬異常int i=10/0;//mary多100userDao.addMoney();// 3.沒有發生異常,提交事務// }catch(Exception e){ // 4.如果發生了異常,從第3跳到第4,進行事務回滾// }}}?
?
(5)測試:
原來數據:
后來數據(不變):
只是在控制臺提示報錯信息:
?
?
?
總結
以上是生活随笔為你收集整理的Spring事务操作-事务引入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux系统常用的基本命令【转载CSD
- 下一篇: Spring框架概述(快速入门)