读书人

事务的处理有关问题

发布时间: 2012-12-28 10:29:05 作者: rapoo

事务的处理问题
框架用的ibatis和springMVC

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就给自动提交一个,有异常了就只回滚出现异常的。
何解?


[解决办法]
Spring事务有传播性 可以用PROPAGATION_REQUIRED
[解决办法]
你只有一个事物回滚在于你的aop切入点不对。
你的切入点配置在了dao层中了。所以每次回滚的事物只有一个对象,如果在service层中,回滚的事物就是所有的对象。
另外如果你的xml是service层中的话,那么少了一个sessionFactory。

[解决办法]
事务的处理有关问题<value>*Impl</value

读书人网 >J2EE开发

热点推荐