spring事务管理分为代码管理和配置文件管理
配置文件管理分为5种方法
详细参考:http://www.blogjava.net/robbie/archive/2009/04/05/264003.htm
http://sunlf.iteye.com/blog/584769
针对ssh项目的说明:
spring事务管理是在service层进行事务管理
对于面向切面编程要注意的问题
<aop:pointcut id="allManagerMethod"
expression="execution(* org.ethip.catalog.service.*.*(..))"/> 中第一个星号表示方法的可见性,public那种,第二个星号表示具体的类,第三个表示方法。
http://www.blogjava.net/algz/articles/262941.html
<bean id="fooService" class="x.y.service.DefaultFooService"/>
<!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/>
bean below) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get'
are read-only -->
<tx:method name="get*" read-only="true"/>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- ensure that the above transactional advice runs for any executionof an operation defined by the FooService
interface -->
<aop:config>
<aop:pointcut id="fooServiceOperation" expression="execution(* x.y.service.FooService.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
</aop:config>
<!-- ******************************************************************-->
<!-- don't forget the DataSource
-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@rj-t42:1521:elvis"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</bean>
<!-- similarly, don't forget the (particular) PlatformTransactionManager
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- other <bean/>
definitions here -->
</beans>
我们来分析一下上面的配置。我们要把一个服务对象('fooService'
bean)做成事务性的。我们想施加的事务语义封装在<tx:advice/>
定义中。<tx:advice/>
“把所有以 'get'
开头的方法看做执行在只读事务上下文中,其余的方法执行在默认语义的事务上下文中”。 其中的 'transaction-manager'
属性被设置为一个指向 PlatformTransactionManager
bean的名字(这里指 'txManager'
),该bean将实际上实施事务管理。
<aop:config/>
的定义,它确保由
'txAdvice'
bean定义的事务通知在应用中合适的点被执行。首先我们定义了 一个切面,它匹配
FooService
接口定义的所有操作,我们把该切面叫做
'fooServiceOperation'
。然后我们用一个通知器(advisor)把这个切面与
'txAdvice'
绑定在一起,表示当
'fooServiceOperation'
执行时,
'txAdvice'
定义的通知逻辑将被执行。
一个普遍性的需求是让整个服务层成为事务性的。满足该需求的最好方式是让切面表达式匹配服务层的所有操作方法。例如:
<aop:config>
<aop:pointcut id="fooServiceMethods" expression="execution(* x.y.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/>
</aop:config>