java元婴期(21)----java进阶(spring(5)---事务管理AOP事务管理(全自动)spring整合Junit)
事務管理
事務:一組業務操作ABCD,要么全部成功,要么全部不成功。
特性:ACID
- ?????? 原子性:整體
- ?????? 一致性:完成
- ?????? 隔離性:并發
- ?????? 持久性:結果
隔離問題:
?????? 臟讀:一個事務讀到另一個事務沒有提交的數據
?????? 不可重復讀:一個事務讀到另一個事務已提交的數據(update)
隔離級別:
了解類比
事務管理介紹
導入jar包
transaction? -->? tx
三個頂級接口
PlatformTransactionManager? 平臺事務管理器,spring要管理事務,必須使用事務管理器
?????? 進行事務配置時,必須配置事務管理器。
TransactionDefinition:事務詳情(事務定義、事務屬性),spring用于確定事務具體詳情,
?????? 例如:隔離級別、是否只讀、超時時間等
?????? 進行事務配置時,必須配置詳情。spring將配置項封裝到該對象實例。
TransactionStatus:事務狀態,spring用于記錄當前事務運行狀態。例如:是否有保存點,事務是否完成。
?????? spring底層根據狀態進行相應操作。
?
PlatformTransactionManager? 事務管理器
?
- 導入jar包:需要時平臺事務管理器的實現類(idea maven開發時找對應依賴的坐標)
- 常見的事務管理器
DataSourceTransactionManager? ,jdbc開發時事務管理器,采用JdbcTemplate
- api詳解
TransactionStatus getTransaction(TransactionDefinition definition) ,事務管理器 通過“事務詳情”,獲得“事務狀態”,從而管理事務。
void commit(TransactionStatus status)? 根據狀態提交
void rollback(TransactionStatus status) 根據狀態回滾
TransactionStatus
TransactionDefinition
?
?
- 傳播行為:在兩個業務之間如何共享事務。
PROPAGATION_REQUIRED , required , 必須? 【默認值】
?????? 支持當前事務,A如果有事務,B將使用該事務。
?????? 如果A沒有事務,B將創建一個新的事務。
PROPAGATION_SUPPORTS ,supports ,支持
?????? 支持當前事務,A如果有事務,B將使用該事務。
?????? 如果A沒有事務,B將以非事務執行。
PROPAGATION_MANDATORY,mandatory ,強制
?????? 支持當前事務,A如果有事務,B將使用該事務。
?????? 如果A沒有事務,B將拋異常。
PROPAGATION_REQUIRES_NEW , requires_new ,必須新的
?????? 如果A有事務,將A的事務掛起,B創建一個新的事務
?????? 如果A沒有事務,B創建一個新的事務
PROPAGATION_NOT_SUPPORTED ,not_supported ,不支持
?????? 如果A有事務,將A的事務掛起,B將以非事務執行
?????? 如果A沒有事務,B將以非事務執行
PROPAGATION_NEVER ,never,從不
?????? 如果A有事務,B將拋異常
?????? 如果A沒有事務,B將以非事務執行
PROPAGATION_NESTED ,nested ,嵌套
?????? A和B底層采用保存點機制,形成嵌套事務。
案例:轉賬
創建表
create database ee19_spring_day03; use ee19_spring_day03; create table account(id int primary key auto_increment,username varchar(50),money int ); insert into account(username,money) values('jack','10000'); insert into account(username,money) values('rose','10000');?
?
?
?
導入jar包(idea里的maven工程還是找相應的坐標)
- 核心:4+1
- aop : 4 (aop聯盟、spring aop、aspectj規范、spring aspect)
- 數據庫:2? (jdbc/tx)
- 驅動:mysql
- 連接池:c3p0
AOP 配置基于xml【掌握】
在spring xml 配置aop 自動生成代理,進行事務的管理
- 配置管理器
- 配置事務詳情
- 配置aop
?
?
dao層
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {@Overridepublic void out(String outer, Integer money) {this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);}@Overridepublic void in(String inner, Integer money) {this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);}}?
?
service層
用 int i = 1 / 0來測試事務配置是否成功
public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void transfer(String outer, String inner, Integer money) {accountDao.out(outer, money);//斷電 // int i = 1/0;accountDao.in(inner, money);}}?
?
?
?
?
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"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 創建數據源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study"></property><property name="user" value="root"></property><property name="password" value="10086"></property></bean><!-- 配置dao --><bean id="accountDaoImple" class="cn.lm.tx03_xml.AccountDaoImple"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置service --><bean id="accountServiceImplId" class="cn.lm.tx03_xml.AccountServiceImpl"><property name="accountDao" ref="accountDaoImple"></property></bean><!-- 配置事務管理器 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置 事務詳情(事務通知) , 在aop篩選基礎上,對ABC三個確定使用什么樣的事務。例如:AC讀寫、B只讀 等<tx:attributes> 用于配置事務詳情(屬性屬性)<tx:method name=""/> 詳情具體配置propagation 傳播行為 , REQUIRED:必須;REQUIRES_NEW:必須是新的isolation 隔離級別 --><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="transfer" propagation="REQUIRED" isolation="DEFAULT"/></tx:attributes></tx:advice><!-- AOP編程,目標類有ABCD(4個連接點),切入點表達式 確定增強的連接器,從而獲得切入點:ABC --><aop:config><aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.lm.tx03_xml.*.*(..))"/></aop:config> </beans>測試
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {String xmlPath = "cn/lm/tx03_xml/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);AccountService accountService = (AccountService) applicationContext.getBean("accountServiceImplId");accountService.transfer("jack", "rose", 1000);} }?
?
AOP配置基于注解【掌握】
- 配置事務管理器,將并事務管理器交予spring
- 在目標類或目標方法添加注解即可 @Transactional
?
?
Service層
import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT) public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void transfer(String outer, String inner, int money) {accountDao.out(outer, money);//int i = 1 / 0;accountDao.in(inner, money);} }spring配置(前半部分與基于xml相同)
<!-- 4 事務管理 --><!-- 4.1 事務管理器 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 4.2 將管理器交予spring * transaction-manager 配置事務管理器* proxy-target-classtrue : 底層強制使用cglib 代理--><tx:annotation-driven transaction-manager="txManager"/>?
?
?
?
?
事務詳情配置
@Transactional注解可寫于方法與類上
@Transactional(propagation=Propagation.REQUIRED , isolation = Isolation.DEFAULT) public class AccountServiceImpl implements AccountService {整合Junit
導入jar包
- ?????? 基本 :4+1
- ?????? 測試:spring-test...jar
1.讓Junit通知spring加載配置文件
2.讓spring容器自動進行注入
修改測試類
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class TestApp {@Autowired //與junit整合,不需要在spring xml配置掃描private AccountService accountService;@Testpublic void demo01(){ // String xmlPath = "applicationContext.xml"; // ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); // AccountService accountService = (AccountService) applicationContext.getBean("accountService");accountService.transfer("jack", "rose", 1000);}}?
總結
以上是生活随笔為你收集整理的java元婴期(21)----java进阶(spring(5)---事务管理AOP事务管理(全自动)spring整合Junit)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java元婴期(20)----java进
- 下一篇: java元婴期(22)----java进