
??事務是資料庫操作最基本的單元,是邏輯上的一組操作,這一組操作在同一個會話中要么都執行成功,要么都失敗,這也是事務的最基本特性--原子性,事務的作用是為了保證系統資料的正確性,因此,資料庫應用程式中是會經常用到事務,下面就說一下在Spring里怎么做事務操作,
Spring事務使用方式
Spring事務使用方式分兩類,分別是編程式事務、宣告式事務,
編程式事務
??使用編程式事務,事務操作的代碼跟業務邏輯代碼耦合度高,這樣會造成代碼的維護成本較大,因此,除非需要細粒度的控制各個事務的邊界,否則一般不會用到這種方式,
宣告式事務
??宣告式事務控制粒度就比較粗糙,但使用這種方式可以做到全域配置,能實作與業務邏輯代碼解耦,代碼的維護成本較小,這也是經常用到的一種方式,宣告式事務的實作完全依賴于Spring的AOP機制,其本質就是通過AOP在目標方法執行之前加入事務,在目標方法執行之后根據方法執行結果選擇是執行回滾操作還是執行提交操作,
宣告式事務配置的5種方式
??這里主要說一下宣告式事務的配置方式,Spring事務配置總是由三個部分組成:分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分,DataSource、TransactionManager這兩部分只是會根據資料訪問方式有所變化,比如使用Hibernate進行資料訪問 時,DataSource實際為SessionFactory,TransactionManager的實作為 HibernateTransactionManager,
??根據代理機制的不同,總結了五種Spring事務的配置方式,組態檔如下:
第一種方式:每個Bean都有一個代理
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
>
<property name="configLocation" value="https://www.cnblogs.com/ayic/archive/2022/10/04/classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="https://www.cnblogs.com/ayic/archive/2022/10/04/org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器 -->
<bean id="transactionManager"
>
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="goodsDaoTarget" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="goodsDao"
>
<!-- 配置事務管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="goodsDaoTarget" />
<property name="proxyInterfaces" value="https://www.cnblogs.com/ayic/archive/2022/10/04/com.lab.spring.dao.GeneratorDao" />
<!-- 配置事務屬性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
第二種方式:所有Bean共享一個代理基類
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
>
<property name="configLocation" value="https://www.cnblogs.com/ayic/archive/2022/10/04/classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="https://www.cnblogs.com/ayic/archive/2022/10/04/org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器 -->
<bean id="transactionManager"
>
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionBase"
lazy-init="true" abstract="true">
<!-- 配置事務管理器 -->
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事務屬性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 配置DAO -->
<bean id="goodsDaoTarget" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="goodsDao" parent="transactionBase" >
<property name="target" ref="goodsDaoTarget" />
</bean>
</beans>
第三種方式:使用攔截器
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="sessionFactory"
>
<property name="configLocation" value="https://www.cnblogs.com/ayic/archive/2022/10/04/classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="https://www.cnblogs.com/ayic/archive/2022/10/04/org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器 -->
<bean id="transactionManager"
>
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor"
>
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事務屬性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean >
<property name="beanNames">
<list>
<value>*Dao</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 配置DAO -->
<bean id="goodsDao" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
第四種方式:使用tx標簽配置的攔截器
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.lab" />
<bean id="sessionFactory"
>
<property name="configLocation" value="https://www.cnblogs.com/ayic/archive/2022/10/04/classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="https://www.cnblogs.com/ayic/archive/2022/10/04/org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器 -->
<bean id="transactionManager"
>
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.lab.spring.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
第五種方式:@Transactional注解
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.lab" />
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sessionFactory"
>
<property name="configLocation" value="https://www.cnblogs.com/ayic/archive/2022/10/04/classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="https://www.cnblogs.com/ayic/archive/2022/10/04/org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器 -->
<bean id="transactionManager"
>
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
此時在DAO上需加上@Transactional注解,
@Transactional
@Component("goodsDao")
public class GoodsDaoImpl extends HibernateDaoSupport implements GoodsDao {
public List<Goods> goodsList() {
return this.getSession().createQuery("from Goods").list();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/510901.html
標籤:其他
下一篇:Django 出現 frame because it set X-Frame-Options to deny 錯誤
