spring+mybatis,Support事务传播级别下无法及时拿到新插入数据的有关问题
spring+mybatis,Support事务传播级别下无法及时拿到新插入数据的问题
Spring+Mybatis,事务配置是按照官网上配的
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
</bean>
<!-- 把数据源通过spring注入mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- ScanMapperFiles,自动扫描所有Mapper映射文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bigplayer.test.mapper" />
</bean>
<!-- 事务处理管理器 ,事务管理器指定的 DataSource 必须和用来创建 SqlSessionFactoryBean 的 是同一个数据源,否则事务管理器就无法工作了-->
<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="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="select*" propagation="SUPPORTS"/>
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点 -->
<aop:config>
<!--把事务控制在Service层-->
<aop:pointcut id="bussinessService"
expression="execution(public
* com.bigplayer.test.service.*.*(..))" />
<!-- 指定bussinessService切入点应用txAdvice处理器,即该切入点的所有符合特征的方法均具备了事务性 -->
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
注意这句<tx:method name="*" propagation="SUPPORTS" />
然后我先弄了一个TestService接口,只有一个方法testDao()
而后弄了一个UserInfoService接口
public abstract void insertUserRegister(UserInfo userInfo);
public abstract UserInfo selectUserLogin(Integer id);
public abstract List<UserInfo> selectAllUser();
public abstract void updateUserInfo(UserInfo userInfo);
然后分别实现了TestService接口和UserInfoService接口,其中UserInfoService接口的实现类UserInfoServiceImpl的四个方法在独立工作时都是正常的
TestService的TestServiceImpl实现类的方法内容是这样的
private UserInfoService userInfoService;
public UserInfoService getUserInfoService() {
return userInfoService;
}
@Resource
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
@Override
public UserInfo testDao() {
UserInfo userInfo = userInfoService.selectUserLogin(1); //此时数据库时空的,查到的数据为null
Spring+Mybatis,事务配置是按照官网上配的
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
</bean>
<!-- 把数据源通过spring注入mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- ScanMapperFiles,自动扫描所有Mapper映射文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bigplayer.test.mapper" />
</bean>
<!-- 事务处理管理器 ,事务管理器指定的 DataSource 必须和用来创建 SqlSessionFactoryBean 的 是同一个数据源,否则事务管理器就无法工作了-->
<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="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.RuntimeException" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="select*" propagation="SUPPORTS"/>
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!-- 配置事务切入点 -->
<aop:config>
<!--把事务控制在Service层-->
<aop:pointcut id="bussinessService"
expression="execution(public
* com.bigplayer.test.service.*.*(..))" />
<!-- 指定bussinessService切入点应用txAdvice处理器,即该切入点的所有符合特征的方法均具备了事务性 -->
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
注意这句<tx:method name="*" propagation="SUPPORTS" />
然后我先弄了一个TestService接口,只有一个方法testDao()
而后弄了一个UserInfoService接口
public abstract void insertUserRegister(UserInfo userInfo);
public abstract UserInfo selectUserLogin(Integer id);
public abstract List<UserInfo> selectAllUser();
public abstract void updateUserInfo(UserInfo userInfo);
然后分别实现了TestService接口和UserInfoService接口,其中UserInfoService接口的实现类UserInfoServiceImpl的四个方法在独立工作时都是正常的
TestService的TestServiceImpl实现类的方法内容是这样的
private UserInfoService userInfoService;
public UserInfoService getUserInfoService() {
return userInfoService;
}
@Resource
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
@Override
public UserInfo testDao() {
UserInfo userInfo = userInfoService.selectUserLogin(1); //此时数据库时空的,查到的数据为null