Spring——第四章 Spring與Dao(二)
阿新 • • 發佈:2018-12-12
4.2 Spring的事物管理
4.2.1 Spring事物管理API
(1)事物管理器介面
常用的實現類:
DataSourceTransactionManager
Spring的回滾方式,發生執行時異常回滾,發生受查異常提交。
受查異常可以丟擲,或者try,catch
執行時異常直接掛掉,更嚴重。
(2)事物定義介面
4.2.2
事先定義Service,從上往下寫,從需求開始寫 。
將事物從dao層提升到service層
4.2.3 使用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" 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"> <!-- ================================= IoC ==================================== --> <!-- 註冊資料來源:C3P0 --> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 註冊屬性檔案 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 註冊Dao --> <bean id="accountDao" class="com.bjpowernode.dao.AccountDaoImpl"> <property name="dataSource" ref="myDataSource"/> </bean> <bean id="stockDao" class="com.bjpowernode.dao.StockDaoImpl"> <property name="dataSource" ref="myDataSource"/> </bean> <!-- 註冊Service --> <bean id="buyStockService" class="com.bjpowernode.service.BuyStockServiceImpl"> <property name="adao" ref="accountDao"/> <property name="sdao" ref="stockDao"/> </bean> <!-- ================================= AOP ==================================== --> <!-- 註冊事務管理器 --> <bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="myDataSource"/> </bean> <!-- 生成事務代理物件 --> <bean id="serviceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="myTransactionManager"/> <property name="target" ref="buyStockService"/> <property name="transactionAttributes"> <props> <prop key="open*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED</prop> <!-- -異常:表示發生指定異常後回滾,這時的異常通常是受查異常 +異常:表示發生指定異常後提交,這時的異常通常是執行時異常 --> <prop key="buyStock">ISOLATION_DEFAULT,PROPAGATION_REQUIRED, -BuyStockException</prop> </props> </property> </bean> </beans>
4.2.5 使用AspectJ的AOP配置管理實務
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- ================================= IoC ==================================== --> <!-- 註冊資料來源:C3P0 --> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 註冊屬性檔案 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 註冊Dao --> <bean id="accountDao" class="com.bjpowernode.dao.AccountDaoImpl"> <property name="dataSource" ref="myDataSource"/> </bean> <bean id="stockDao" class="com.bjpowernode.dao.StockDaoImpl"> <property name="dataSource" ref="myDataSource"/> </bean> <!-- 註冊Service --> <bean id="buyStockService" class="com.bjpowernode.service.BuyStockServiceImpl"> <property name="adao" ref="accountDao"/> <property name="sdao" ref="stockDao"/> </bean> <!-- ================================= AOP ==================================== --> <!-- 註冊事務管理器 --> <bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="myDataSource"/> </bean> <!-- 註冊事務通知 --> <tx:advice id="txAdvice" transaction-manager="myTransactionManager"> <tx:attributes> <!-- 這裡指定的是:為每一個連線點指定所要應用的事務屬性 --> <tx:method name="open*" isolation="DEFAULT" propagation="REQUIRED"/> <tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BuyStockException"/> </tx:attributes> </tx:advice> <!-- AOP配置 --> <aop:config> <!-- 這裡指定的是切入點 --> <aop:pointcut expression="execution(* *..service.*.*(..))" id="myPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/> </aop:config> </beans>