Spring与Mybatis整合,在开发导入功能时,在Mybatis中增加了一个insertBath方法。
由于原项目的代码结构划分不是很清晰,左右操作都是放到Service下,新功能开发时,将代码结构做了细化,
导致在insert数据时,系统表象是执行成功,而却Mybatis打出的日志也显示成功,并且显示成功条数,但是在后台查看,数据库中并没有数据,从而进行了问题排查。
首先先到的是Mybatis事务没提交,但是在PLSQL中查看有没有存在没提交的事务,显示并没有待提交的事务。
后来想到可能是Spring事务管理配置中没有将新增代码的事务加进去:
原来是事务配置如下:
<!-- 配置Spring的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 拦截器方式配置事务 -->
<tx:advice id="transactionAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="remove*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="(execution(* com.xxx.xxx.xxx.*.*.*.service..*.*(..))) or (execution(* com.xxx.xxx.framework.service.*.*(..))) or (execution(* com.xxx.xxx.*.*..service.*.*(..)))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" order="2"/>
</aop:config>
原有事务配置中并没有新增结构的事务声明,需要将其加入:
<!-- 配置Spring的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 拦截器方式配置事务 -->
<tx:advice id="transactionAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="remove*" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="execute" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="process" propagation="REQUIRED"
rollback-for="Exception" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="(execution(* com.xxx.xxx.xxx.*.*.*.service..*.*(..))) or (execution(* com.xxx.xxx.xxx.*.*.*.rule..*.*(..))) or (execution(* com.xxx.xxx.xxx.*.*.*.executer..*.*(..))) or (execution(* com.xxx.xxx.framework.service.*.*(..))) or (execution(* com.ronhe.romp.*.*..service.*.*(..)))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" order="2"/>
</aop:config>
重启系统,插入成功,这只是一个小问题排查的记录,如以后遇到类似情况可作为参考。