Spring中的声明式事物

xiaoxiao2026-08-19  19

Nerver :         不参与事务,如果参与产生RemoteException   NotSupported:    不能参与   Supports:        如果调用者正在参与事务,相应的EJB调用也可以参与事务,否则不能   Mandatory        如果调用者有一个事务,相应的EJB可以参与事务,否则,TransactionRequiredException   Required         如果调用者有一个事务,相应的EJB可以参与事务,否则,容器将在调用相应的EJB之前,开始一个事务.                    当方法调用完成以后,即提交该事务.   RequiresNew      在调用相应的EJB之前,开始一个新的事务,当方法调用返回时,即提交这个事务.           前六个策略类似于EJB CMT:常量名相同,因此,对EJB开发人员来说,应该立刻就感到熟悉。第七个(PROPAGATION_NESTED)是Spring所提供的一个特殊 变量。它要求事务管理器或者使用JDBC 3.0 Savepoint API提供嵌套事务行为(如Spring的DataSourceTransactionManager),或者通过JTA支持嵌套事务。         事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这是一个最优化提示。在一些情况下,一些事务策略能够起到显著的最优化效果, 例如在使用Object/Relational映射工具(如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。       在事务属性中还有定义“timeout”值的选项,指定事务超时为几秒。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的解释。  

在实际工作中Required是用的最多的!

最多不过有些查询需要用的Supports

 

我用aspectj写了一个Spring的声明式事物!当然Spring也有一个TransactionProxyFactory这样的配置可以简单实现声明事物!

 

 

以下的上传的代码是用来以后自己可以回忆用的!

 

主要的配置是:

 

 

 

 <?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: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/aop                            http://www.springframework.org/schema/aop/spring-aop.xsd                   http://www.springframework.org/schema/tx                            http://www.springframework.org/schema/tx/spring-tx.xsd"><aop:config>  <aop:pointcut id="transMgr" expression="execution(@com.lovo.trans.Trans * *.*(..))"/>  <aop:advisor pointcut-ref="transMgr" advice-ref="txManager"/></aop:config><tx:advice id="txManager" transaction-manager="transactionManager">  <tx:attributes>    <tx:method name="create*" propagation="REQUIRED" rollback-for="SQLException" />    <tx:method name="find*" propagation="SUPPORTS"/>  </tx:attributes></tx:advice>

<!-- dataSource --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">       <property name="driverClassName">         <value>oracle.jdbc.driver.OracleDriver</value>       </property>       <property name="url">         <value>jdbc:oracle:thin:@localhost:1521:orcl</value>       </property>       <property name="defaultAutoCommit">         <value>false</value>       </property>       <property name="username"><value>scott</value></property>       <property name="password"><value>wl</value></property> </bean>

<!-- 事物管理器 --><bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource">         <ref local="dataSource"/>       </property> </bean>           <bean id="bookService1" class="com.lovo.trans.BookService">      <property name="dataSource">         <ref bean="dataSource"/>      </property>      <property name="bs2">         <ref bean="bookService2"/>      </property>  </bean>       <bean id="bookService2" class="com.lovo.trans.BookService2">      <property name="dataSource">          <ref bean="dataSource"/>      </property>  </bean>               </beans>    

转载请注明原文地址: https://www.6miu.com/read-5051498.html

最新回复(0)