PROPAGATION_REQUIRED:支持當前事務,如果當前沒有事務,就新建一個事務。這是最常見的選擇。spring默認的事務策略
PROPAGATION_SUPPORTS:支持當前事務,如果當前沒有事務,就以非事務方式執行。
PROPAGATION_MANDATORY:支持當前事務,如果當前沒有事務,就拋出異常。
PROPAGATION_REQUIRES_NEW:新建事務,如果當前存在事務,把當前事務掛起。
PROPAGATION_NOT_SUPPORTED:以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
PROPAGATION_NEVER:以非事務方式執行,如果當前存在事務,則拋出異常。
PROPAGATION_NESTED:如果當前存在事務,則在嵌套事務內執行。如果當前沒有事務,則進行與PROPAGATION_REQUIRED類似的操作。
-Exception表示有Exception拋出時,事務回滾. -代表回滾+就代表提交
readonly 就是read only, 設置操作權限爲只讀,一般用於查詢的方法,優化作用.
1:每個Bean都有一個代理(個人不贊同這種配置,代碼太臃腫了,例子就網上拉一個下來,感覺還是配置在service層比較好哈)
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
</bean>
<!-- 定義事務管理器(聲明式的事務) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置DAO -->
<bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置事務管理器 -->
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="userDaoTarget" />
<property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
<!-- 配置事務屬性 -->
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
2:所有Bean共享一個代理基類(自己項目中正使用的)
<!-- 定義事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--transactionProxy-->
<bean id="transactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userDao" class="com.yeshun.dao.impl.UserDaoImpl" parent="baseDAO" />
<bean id="userService" parent="transactionProxy">
<property name="target">
<bean class="com.yeshun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
</property>
</bean>
3:使用攔截器
<!-- 定義事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="userService" class="com.yeshun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
4:使用tx標籤配置的攔截器(這個方式不錯)
<!-- 定義事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<!-- 其他方法使用默認的事務設置 -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="aopPointcut"
expression="execution(* com.yeshun.service.impl.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="aopPointcut" />
</aop:config>
<bean id="userService" class="com.yeshun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
5:註解方式
<!-- 採用註釋的方式配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.yeshun" />
<!-- 採用註釋的方式配置 aop -->
<aop:aspectj-autoproxy />
<!-- 採用annotation的方式配置事務 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 定義事務管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
此時在Service上需加上@Transactional註解,如下:
@Transactional配置修改該規則(有時候不需要修改該規則,比如save,update....)
@Transactional(noRollbackFor=RuntimeException.class)方法事物說明運行時異常進行回滾
@Transactional(RollbackFor=Exception.clas)
@Transactional(readyOnly=true)
@Transactional(timeout=100)默認30
@Transactional(isolation)數據庫的隔離級別 (沒怎麼用過,也不懂)
@Transactional
@Service("UserServiceImpl")
public class UserServiceImpl implements UserService{
private @Resource(name = "UserDaoImpl")
UserDao userDao;
public List<User> findAllUser() {
// TODO Auto-generated method stub
return userDao.findAllUser();
}
}