当前位置:编程学习 > JAVA >>

spring mybatis 整合事务问题!

项目思路:
spring+mybatis+struts2
1、struts2负责封装数据信息map传到service。
2、soa反射调用service方法
3、service 方法规定单参数(map)
4、service继承SqlSessionDAOSupport
5、每一个sqlsession创建一个事务,不使用spring管理事务
1-5都实现了,但是差强人意的是 第五步。使用mybatis的ManagedTransactionFactory 程序走完后没有提交数据到数据库 。把代码贴出来大家帮我分析分析。
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 数据源配置 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="defaultAutoCommit" value="false"></property>
</bean>
<!--配置mybatis的 sqlSessionFactory工厂配置 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionFactory">
<bean
class="org.apache.ibatis.transaction.managed.ManagedTransactionFactory" />
</property>
<property name="configLocation"
value="classpath:mybatis-config.xml" />
<!-- <property  name="mapperLocations"  value="classpath*:com/xxt/ibatis/dbcp/domain/user.map.xml"/   >  -->
</bean>
<!-- 采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession。 -->
<bean id="sqlSession" class="org.mybatis.spring.support.SqlSessionDaoSupport"
lazy-init="true">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
</beans> MyBatis Spring 事务 --------------------编程问答--------------------
还是spring的事务好用点


<!-- transaction manager, use JtaTransactionManager for global tx -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dynamicDataSource" />
</bean>

<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="proxyTargetClass" value="false" />
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="txManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="modify*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
--------------------编程问答-------------------- 不用spring的事务,我是想搭建一个快速的开发基本框架,只要了解基本的业务就行了!  不要跑题哦 --------------------编程问答-------------------- 自己顶 !! 
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,