先贴上web.xml和spring-jdbc.xml代码:

web.xml代码:

   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring-jdbc.xml,
classpath:config/spring-redis.xml
</param-value>
</context-param> <listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring-mvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>

spring-jdbc.xml代码:

   <?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd" default-autowire="byName" > <!-- 引入属性文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/jdbc.properties</value>
<value>classpath:config/redis.properties</value>
<value>classpath:config/contract-mail.properties</value>
</list>
</property>
</bean> <!-- 加载config.properties,为了使该注解生效:@Value("#{config['email.alarm.sender.email']}") -->
<bean id="config" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<!-- false表示当没找到这个配置文件时,应用程序应该报错 -->
<property name="ignoreResourceNotFound" value="false" />
<property name="locations">
<list>
<value>classpath:config/config.properties</value>
</list>
</property>
</bean> <!-- 配置数据源 -->
<bean name="dataSourceSpied" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<property name="initialSize" value="${jdbc_initialSize}" />
<property name="maxActive" value="${jdbc_maxActive}" />
<property name="minIdle" value="${jdbc_minIdle}" />
<property name="maxWait" value="${jdbc_maxWait}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="180" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 监控数据库 -->
<property name="filters" value="mergeStat" />
</bean> <bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">
<constructor-arg ref="dataSourceSpied"/>
</bean> <!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:mapper/*.xml" />
<property name="plugins">
<list>
<!-- 物理分页 -->
<bean class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
<property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect"></property>
</bean>
</list>
</property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceSpied" />
</bean>
<!-- 声明式事务管理,拦截多个包下面事物 and execution(* com.xxx.service.*Service.*(..)) -->
<aop:config>
<aop:advisor pointcut="execution(* com.xxx.service.*Service.*(..))"
advice-ref="myAdvice"/>
</aop:config>
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="addCommandExec" propagation="REQUIRES_NEW"/> <!-- 这个名字的service方法使用新事物,不使用继承事物 -->
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.RuntimeException" />
<!-- <tx:method name="*" read-only="true"/> -->
<!-- <tx:method name="*" read-only="true" rollback-for="com.smvc.util.DaoException"/> -->
</tx:attributes>
</tx:advice> <!-- 自动扫描组件,多个包用逗号隔开,需要把controller去掉,否则影响事务管理 -->
<context:component-scan base-package="com.xxx,com.xxx.command">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSource" />
</bean> <bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean> </beans>

bug问题表现:

在Service方法中,事物不起作用,bug表现如下:
1) 期望:Service方法中save()方法执行前开启事物,执行后提交事物,提交事物后才可以在数据库里看到那条新insert的数据。

2) 现象:事物不起作用,在save()方法未结束时,数据库中已经可以看到那条新insert的数据。save()里使用的是jdbcTemplate对象执行的SQL。

问题解决思路:

由于是springMVC项目,为了更快的debug,省去tomcat启动时间,建立了Junit测试类模拟spring容器启动,代码贴上如下:

 package com.xxx.service;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; @WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:config/spring-mybatis.xml"
,"classpath:config/spring-redis.xml","classpath:config/spring-mvc.xml"})
public class SystemUserServiceTest {
private final static Logger logger= Logger.getLogger(SystemUserServiceTest.class); @Autowired
private SystemUserService systemUserService;
@Test
public void test() {
int result2 = systemUserService.insertSystemUser();
logger.info("result2 == " + result2);
} }

第一次解决方法:百度了很多资料,都说spring-mvc.xml和spring-jdbc.xml的注解扫描器要注意互相排除,也就是controller层的要排除扫描service类,service层的要排除controller层,我仔细对比了两个xml配置文件,与网上写的解决方法一致。所以不是这个原因出现的问题。

第二次解决方法:我的spring-jdbc.xml配置文件里面,使用的是transactionManager对象来做事物管理类,所以跟踪spring事物类org.springframework.jdbc.datasource.DataSourceTransactionManager,看看到底有没有进入事物管理。分别在DataSourceTransactionManager类里的doBegin事物开启和doCommit事物提交方法里打上断点,debug开始跟踪。debug过程中发现,spring容器确实在save()方法之前执行的doBegin()事物开启,确实在save()执行完毕之后执行的doCommit()事物提交。可是当save()方法还未彻底结束之前,也就是doCommit()方法也尚未执行之前,数据库就有这条新insert记录了!所以不是事物配置没有起作用的原因。

第三次解决方法:这次换个思路,我在save()方法中执行sql语句的代码是:jdbcTemplate.update(insertSql);  我在这个update方法里打了断点,一直跟踪下去,终于发现了bug原因!

bug原因:

jdbcTemplate.update(insertSql)  中的update方法里的dataSource对象在debug过程中看到此对象ID是111,而在之前DataSourceTransactionManager类里面的dataSource对象在debug过程中此对象ID是81,说明jdbcTemplate对象拿到的dataSource对象和事物管理器里面的dataSource对象根本就不是同一个!

bug修正:

把spring-jdbc.xml里的实际sql 执行对象所引用的dataSource换成和事物管理器的dataSource同一个对象就好了。

     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name = "dataSource" ref="dataSourceSpied" />
</bean> <bean id="namedJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSourceSpied" />
</bean>

springJdbc(jdbcTemplate)事物拦截失效问题解决的更多相关文章

  1. 访问前台页面${pageContext.request.contextPath}/el表达式失效问题解决

    访问前台页面${pageContext.request.contextPath}/el表达式失效问题解决 2017年05月09日 10:54:18 AinUser 阅读数:922 标签: el表达式4 ...

  2. pycharm 2017 序列号失效问题解决(2016-2017版本都有效)

    pycharm 序列号失效问题解决   this license BIG3CLIK6F has been cancelled  具体如下: 对,没错,这个激活码本来可以使用到2018年的,但是,忽然间 ...

  3. sublime text的Ctrl+alt+up快捷键失效问题解决

    上周一入职了新公司,安装了sublime text3 之后发现多行光标定位快捷键"Ctrl+alt+up|down"不能使用了.最后发现是快捷键冲突.我的电脑安装的是win7专业版 ...

  4. IE下IFrame引用跨域站点页面时,Session失效问题解决

    问题场景:在一个应用(集团门户)的某个page中, 通过IFrame的方式嵌入另一个应用(集团实时监管系统)的某个页面. 当两个应用的domain 不一样时, 在被嵌入的页面中Session失效.(s ...

  5. .gitignore失效问题解决

    .gitignore失效背景: 本地Mac上使用Unity新建了一个项目,使用git init将项目初始化为仓库,此时commit.随后,加入.gitignore文件,再次commit.然后提交整个仓 ...

  6. [异常解决] ubuntu上安采用sudo启动的firefox,ibus输入法失效问题解决

    采用sudo启动的应用是root权限的应用, ibus失效是因为ibus的初始配置采用user权限: 而root下运行的firefox输入法的配置还是停留在默认情况~ 解决方案是在shell下以roo ...

  7. IE11里边form拦截失效,永远被弹回登录页

    现象描述: 1.在某些服务器上发布了程序以后,用IE11去浏览程序(试了多台电脑都一样),发现总是登录不进去,因为登录之后总是被立即反弹回登录页面,就像是登录后写入的票据瞬间丢失一样. 2.但是同一套 ...

  8. IE6、7 a链接内图片加滤镜后导致a标签链接失效问题解决

    今天在项目中遇到一个ie6.7浏览器下a链接失效的问题,查询大量资料,最终找到完美的解决方案,如下: 解决方法: 为a标签加样式{*background:url(#);*zoom:1;} 为img外部 ...

  9. reload(sys)后print失效问题解决

    python版本: python2.7.6 #查看python默认编码格式 >>> import sys >>> print sys.getdefaultencod ...

随机推荐

  1. string部分方法

    1.string.lastIndexOf() lastIndexOf 是从string末尾查找,但是返回值仍是首部的位置值. 2.string.replace() 放一个正则匹配会全部替换. 3.st ...

  2. MVC实战之排球计分软件(深入了解面向对象编程)

    在此篇博客之前,我已经写了一个实战系列的博客,虽然不太成熟但是相对比较实用,在这篇博客我将继续使用mvc编程此软件. 此篇博客会在一定的时间内完成,此次完成的软件的一个需求是提供给运动员的使用.我将在 ...

  3. python3练习-查找文件

    题: 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径 import os import os.path def find_file(root,pa ...

  4. decltype typename

    decltype((variable))总是引用类型,但是decltype(variable)只有当variable是引用类型时才是引用类型. #include <iostream> #i ...

  5. linux 搭建rap记录

    1 安装 jdk1.8版本及以上 2 安装 apache-tomcat 3 安装 redis 最后编辑session配置和数据库配置 <session-config><session ...

  6. 1分钟看懂log4j 配置自己想要的日志信息

    在开发的时候我们会希望 只将  sql信息的日志,已经自定义输出的日志进行打印 ,而一些框架级的日志不需要输出 如下    首先 rootLogger 设置日志级别    log4j.rootLogg ...

  7. web app、hybrid app和native app区别

  8. 第五周作业--测试与版本发布(Alpha版本)

    github传送门:https://github.com/Bubblegod/StardrewValley 一.BUG以及修复 a.修复的BUG: 1.存在着运行环境改变后,资源找不到问题 BUG描述 ...

  9. constructor&object 的对比

    Constructor vs object A constructor is a special member function in the class to allocate memory to ...

  10. int类型129转byte类型得到-127的解释

    package com.yygc.zhen.codeing_base; /** * @author zhen * @Date 2019/2/22 10:03 * 类型转换 */ public clas ...