为了配置事务,在网上找了很多方法,几乎都是一样,心想应该不会有问题,照着网上一步一步来,最后失败一次又一次.没有办法.后来终于发现网上大多数都少了配置参数,废话一堆,直接上配置文件,应该一看就明白.
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312" />
<property name="username" value="dev01" />
<property name="password" value="123456" />
</bean>
<bean id="sqlClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:sqlmaps.xml</value>
</property>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置哪些方法,什么情况下需要回滚-->
<tx:advice id="serviceAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数-->
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<!--除了上面标识的方法,其他方法全是只读方法-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.joinintech.crm.service.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
<!--order配置 -->
<import resource="report/report.xml" />
<import resource="myfaq/myfaq.xml" />
<import resource="order/order.xml" />
<!--user配置-->
<import resource="user/user.xml" />
<!--faq配置-->
<import resource="faq/faq.xml" />
<!-- sales配置 -->
<import resource="sales/sales.xml" />
<import resource="trailcase/trailcase.xml" />
<import resource="help/help.xml" />
</beans>
附:
<1>如果是mysql数据库,一定把数据库的引擎改为innodb
<2>附件的内容为aop需要的额外jar包
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312" />
<property name="username" value="dev01" />
<property name="password" value="123456" />
</bean>
<bean id="sqlClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:sqlmaps.xml</value>
</property>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置哪些方法,什么情况下需要回滚-->
<tx:advice id="serviceAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数-->
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<!--除了上面标识的方法,其他方法全是只读方法-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.joinintech.crm.service.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
<!--order配置 -->
<import resource="report/report.xml" />
<import resource="myfaq/myfaq.xml" />
<import resource="order/order.xml" />
<!--user配置-->
<import resource="user/user.xml" />
<!--faq配置-->
<import resource="faq/faq.xml" />
<!-- sales配置 -->
<import resource="sales/sales.xml" />
<import resource="trailcase/trailcase.xml" />
<import resource="help/help.xml" />
</beans>
附:
<1>如果是mysql数据库,一定把数据库的引擎改为innodb
<2>附件的内容为aop需要的额外jar包