框架用的ibatis和springMVCspring配置如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wms_db" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="initialPoolSize" value="20" />
<property name="minPoolSize" value="20" />
<property name="maxPoolSize" value="20" />
<property name="acquireIncrement" value="5" />
<property name="maxIdleTime" value="10" />
<property name="maxStatements" value="0" />
<property name="testConnectionOnCheckin" value="true" />
<property name="testConnectionOnCheckout" value="true" />
<property name="idleConnectionTestPeriod" value="30" />
</bean><!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager ">
<property name="dataSource" ref="dataSource" /></bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="/WEB-INF/ibatis/sqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"> <ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean><bean name="beanNameAutoProxy"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" /><bean id="transactionProxy" parent="beanNameAutoProxy"><property name="beanNames">
<list>
<value>*Impl</value>
</list>
</property><property name="interceptorNames">
<value>transactionInterceptor</value>
</property></bean></beans>现在我需要执行一批insert 和一批update ,想把他们都放在一个事务里,要成功都成功,有一个异常,都rollback。 但总是执行完一个spring就给自动提交一个,有异常了就只回滚出现异常的。
何解?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"><property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wms_db" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="initialPoolSize" value="20" />
<property name="minPoolSize" value="20" />
<property name="maxPoolSize" value="20" />
<property name="acquireIncrement" value="5" />
<property name="maxIdleTime" value="10" />
<property name="maxStatements" value="0" />
<property name="testConnectionOnCheckin" value="true" />
<property name="testConnectionOnCheckout" value="true" />
<property name="idleConnectionTestPeriod" value="30" />
</bean><!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager ">
<property name="dataSource" ref="dataSource" /></bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="/WEB-INF/ibatis/sqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager"> <ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean><bean name="beanNameAutoProxy"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" /><bean id="transactionProxy" parent="beanNameAutoProxy"><property name="beanNames">
<list>
<value>*Impl</value>
</list>
</property><property name="interceptorNames">
<value>transactionInterceptor</value>
</property></bean></beans>现在我需要执行一批insert 和一批update ,想把他们都放在一个事务里,要成功都成功,有一个异常,都rollback。 但总是执行完一个spring就给自动提交一个,有异常了就只回滚出现异常的。
何解?
你的切入点配置在了dao层中了。所以每次回滚的事物只有一个对象,如果在service层中,回滚的事物就是所有的对象。
另外如果你的xml是service层中的话,那么少了一个sessionFactory。
怎么才能把切入点设置在service层呢
今天工作真不好找。。<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置service层对象(目标对象) -->
<bean name="bookServiceTarget" class="com.briup.service.BookServiceImpl">
<property name="bookDao" ref="bookDao"></property>
</bean>
<!-- 配置service层对象(目标对象) -->
<bean name="userServiceTarget" class="com.briup.service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 配置hibernate的事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 注入sessionFactory ref引用的这个资源配置在dao.xml文件中 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务拦截器 -->
<bean name="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 注入事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 注入事务属性 -->
<property name="transactionAttributes">
<!-- PROPAGATION_REQUIRED事务属性的一种 也是最常用的一种.
如果当前有事务,那么就把操作放到这个事务里,如果当前没有事务那么就新建一个事务然后进行操作
-Execption表示如果在当前这个事务里面的操作过程中,方法抛出异常(Exception类型或者其子类),
那么这事务就回滚
-->
<props>
<prop key="*">
PROPAGATION_REQUIRED,-Exception
</prop>
</props>
</property>
</bean>
<!-- 配置代理对象 -->
<bean name="bookService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 注入目标对象 -->
<property name="target" ref="bookServiceTarget"></property>
<!-- 注入代理对象需要实现的接口 可以有多个 -->
<property name="proxyInterfaces">
<list>
<value>com.briup.service.IBookService</value>
</list>
</property>
<!-- 注入advice或者advisor 但是我们这里注入的是事务拦截器 -->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<!-- 配置代理对象 -->
<bean name="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 注入目标对象 -->
<property name="target" ref="userServiceTarget"></property>
<!-- 注入代理对象需要实现的接口 可以有多个 -->
<property name="proxyInterfaces">
<list>
<value>com.briup.service.IUserService</value>
</list>
</property>
<!-- 注入advice或者advisor 但是我们这里注入的是事务拦截器 -->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
</beans>