Spring+Mybatis整合事务不起作用之解决方案汇总(一)

2014-11-24 09:06:59 · 作者: · 浏览: 5

前言:
公司最近一个项目用到Spring和Mybatis,发现用起来挺方便,比以前的那个struts+hibernate舒服多了。废话少说,直接摆问题,碰到的问题是,mybatis不在事务中运行,后台日志报 “Closing no transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19006c9]”错误。无论是加了@Transactional注解和是没加都报这个信息。一个方法中插入多条数据,某次插入失败也不回滚。


问题描述:
环境: Spring 3.1.0 + Mybatis 3.1.0 + mybatis-spring 1.0.0 RC3 + DB2 9.5 + Tomcat 6.0.35
web工程名称: isap

配置文件:applicationContext.xml + isap-servlet.xml
先看配置信息:
applicationContext.xml

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">





java:comp/env/jndi_isap








classpath:com/cosbulk/isap/*/dao/mapper/*Mapper.xml






















java:comp/env/jndi_smis







classpath:com/cosbulk/smis/dao/mapper/*Mapper.xml















在applicationContext.xml文件中,主要配置了数据源和dao接口以及mapper文件相关信息,其他的bean信息在isap-servlet.xml文件中定义。

配置文件: isap-servlet.xml

[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName">




























errorPages/error


500


org.springframework.web.servlet.handler.SimpleMappingExceptionResolver



errorPages/maxUploadExceeded











在这个配置文件里进行了所有的注解(@Controller、@Service等)扫描和mvc相关配置。

事务场景:
Service层通过注解@Transactional注入事务

[java]

@Service
//默认将类中的所有函数纳入事务管理.
@Transactional("isap")