spring3.1聲明式事務管理

本文只介紹配置文件,注意掃描文件部分。spring

1 配置applicationContext.xml數據庫

<!-- 定義oracle數據庫鏈接-->express

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">oracle

<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />app

<property name="jdbcUrl" value="jdbc:oracle:thin:@ip:port:dbname" />ide

<property name="user" value="name" />ui

<property name="password" value="pwd" />spa

<property name="autoCommitOnClose" value="true" />component

<property name="initialPoolSize" value="5" />xml

<property name="minPoolSize" value="5" />

<property name="maxPoolSize" value="10" />

<property name="acquireIncrement" value="5" />

<property name="maxIdleTime" value="100" />

<property name="maxStatements" value="0" />

<property name="maxStatementsPerConnection" value="100"></property>

</bean>

<!-- 定義jdbcTemplate-->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource" />

</bean>


<!-- 事務  -->

<bean id="transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>


<!-- 配置方法事務隔離機制以及回退機制 -->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="get*" read-only="true" />

<tx:method name="query*" read-only="true" />

<tx:method name="find*" read-only="true" />

<tx:method name="setParameter" propagation="REQUIRES_NEW"

rollback-for="Exception" /><!-- setParameter方法使用新事務 -->

<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />

</tx:attributes>

</tx:advice>

<!-- 配置事務切入切面 對com.test.service下的文件使用事務 -->

<aop:config>

<aop:advisor

pointcut="execution(* com.test.service.*.*(..))"

advice-ref="txAdvice" />

</aop:config>


<!--

自動掃描組件,這裏要把

controller去除,他們是在spring3-servlet.xml中配置的,若是不去除會影響事務管理的。

對com.test下的非Controller註解進行掃描

-->

<context:component-scan base-package="com.test">

<context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

</context:component-scan>


主要

2  spring3-servlet.xml

<!--  對com.test.action下的Controller文件掃描

這裏只能對Controller掃描,對service掃描會使事務失效 -->

<context:component-scan base-package="com.test.action">

<context:include-filter type="annotation"

expression="org.springframework.stereotype.Controller" />

</context:component-scan>

相關文章
相關標籤/搜索