Spring+Mybatis整合事務不起作用之…
前言:
公司最近一個專案用到Spring和Mybatis,發現用起來挺方便,比以前的那個struts+hibernate舒服多了。廢話少說,直接擺問題,碰到的問題是,mybatis不在事務中執行,後臺日誌報 “Closing no transactional SqlSession [[email protected]]”錯誤。無論是加了@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
檢視原始碼 列印?01 |
<? xml version = "1.0"
encoding = "UTF-8" ?> |
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xmlns:tx = "http://www.springframework.org/schema/tx" |
05 |
xmlns:context = "http://www.springframework.org/schema/context" |
06 |
xsi:schemaLocation=" |
07 |
http://www.springframework.org/schema/beans |
08 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
09 |
http://www.springframework.org/schema/tx |
10 |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd |
11 |
http://www.springframework.org/schema/context |
12 |
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
13 |
14 |
15 |
<!--
spring配置jndi --> |
16 |
< bean
id = "dataSource"
class = "org.springframework.jndi.JndiObjectFactoryBean" > |
17 |
< property
name = "jndiName" > |
18 |
< value >java:comp/env/jndi_isap</ value > |
19 |
</ property > |
20 |
</ bean > |
21 |
22 |
< bean
id = "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > |
23 |
< property
name = "configLocation" value = "classpath:config/mybatis-config.xml" ></ property > |
24 |
< property
name = "dataSource"
ref = "dataSource" /> |
25 |
< property
name = "mapperLocations" > |
26 |
< list > |
27 |
< value >classpath:com/cosbulk/isap |
13 |
public
Long getId()
{ |
14 |
return
userDao.getId(); |
15 |
} |
16 |
17 |
22 |
public
void updateUser(User
user,Map<String,Object> param)
{ |
23 |
//編輯使用者 |
24 |
userDao.updateUser(user); |
25 |
//刪除原有的使用者角色關聯 |
26 |
userDao.deleteUserRole(param); |
27 |
String
s = null ; |
28 |
s.getBytes(); |
29 |
//新增使用者角色關聯 |
30 |
insertUserRole(param); |
31 |
} |
32 |
這裡用來測試事務回滾 |
33 |
... |
34 |
} |
測試過幾次updateUser的呼叫,事務就是不起作用的,後臺log資訊如下:
1 |
DEBUG
2011 - 09 - 04
16 : 19 : 46 , 672
org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl:
JDBC Connection
[org.apache.commons.dbcp.PoolableConnection @67aece ] will not be
managed by Spring |
2 |
3 |
DEBUG
2011 - 09 - 04
16 : 19 : 46 , 672
org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl:
SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession @19006c9 ]
was not registered for
synchronization because synchronization is
not active |
4 |
5 |
DEBUG
2011 - 09 - 04
16 : 19 : 46 , 687
org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl:
Committing JDBC Connection
[org.apache.commons.dbcp.PoolableConnection @67aece ] |
6 |
7 |
DEBUG
2011 - 09 - 04
16 : 19 : 46 , 687
org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl:
Closing no transactional SqlSession
[org.apache.ibatis.session.defaults.DefaultSqlSession @19006c9 ] |
8 |
9 |
DEBUG
2011 - 09 - 04
16 : 19 : 46 , 687
org.springframework.jdbc.datasource.DataSourceUtils:
Returning JDBC Connection to DataSource |
解決方案:
檢視原始碼 列印?01 |
1.root-context.xml |
02 |
<!--
不掃描帶有@Controller註解的類。因為這些類已經隨容器啟動時,在servlet-context中掃描過一遍了
--> |
03 |
< context:component-scan base-package = "com.kimho" > |
04 |
< context:exclude-filter type = "annotation"
expression = "org.springframework.stereotype.Controller" /> |
05 |
</ context:component-scan > |
06 |
07 |
2、servlet-context.xml: |
08 |
<!--
掃描業務元件,讓spring不掃描帶有@Service註解的類(留在root-context.xml中掃描@Service註解的類),防止事務失效
--> |
09 |
< context:component-scan base-package = "com.kimho" > |
10 |
< context:exclude-filter type = "annotation"
expression = "org.springframework.stereotype.Service" /> |
11 |
</ context:component-scan > |
我做了相應的修改,把isap-servlet裡的bean掃描拆成了兩部分,分別放入applicationContext.xml和isap-servlet.xml檔案中。
applicationContext.xml中加入:
檢視原始碼 列印?1 |
<!--
掃描帶有@Service註解的類。 --> |
2 |
< context:component-scan
base-package = "com.cosbulk.isap" > |
3 |
< context:include-filter
type = "annotation"
expression = "org.springframework.stereotype.Service"
/> |
4 |
</ context:component-scan > |
5 |
6 |
< context:component-scan
base-package = "com.cosbulk.smis" > |
7 |
< context:include-filter
type = "annotation"
expression = "org.springframework.stereotype.Service"
/> |
8 |
</ context:component-scan > |
isap-servlet.xml檔案中,掃描bean部分換成:
1 |
<!--排除掃描的包,
對web包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 --> |
2 |
< context:component-scan
base-package = "com.cosbulk.isap" > |
3 |
< context:exclude-filter
type = "annotation"
expression = "org.springframework.stereotype.Service" /> |
4 |
</ context:component-scan > |
5 |
6 |
< context:component-scan
base-package = "com.cosbulk.smis" > |
7 |
< context:exclude-filter
type = "annotation"
expression = "org.springframework.stereotype.Service" /> |
8 |
</ context:component-scan > |
重啟伺服器測試,發現效果和之前一樣,事務還是沒起作用。開始以為是和包掃描的位置有關,於是把掃描的位置放到檔案末尾,發現還是沒有起作用。最後偶然一個想法clean了下tomcat,然後重啟,居然事務有效了。
總結: 修改完配置檔案後,clean下工程,重啟載入新的配置檔案。OK,問題解決。
2、如果按照你的步驟設定為ID為null的話,那麼就需要捕獲mybatis丟擲的異常,然後在catch語句中丟擲一個Exception,這個時候Spring容器的事務管理就會起作用,會回滾事務。
3、如果用mysql資料庫,資料庫表你如果是自動建表,那麼就需要把建表的Engine設定為InnoDB格式,自動建表的格式為:MyISAM,這中格式的是不支援事務管理的。
總結下: Spring在使用ContextLoadListener載入applicationContext.xml或其他名稱的xml檔案時,能進行資料來源和相關事務註解的檢查,啟動事務特性。若在isap-servlet.xml檔案中載入是,僅作為普通bean定義載入。所以一個良好的習慣就是,分層配置相關的bean。applicationContext.xml中配置資料庫相關的bean(dao、service等), isap-servlet中配置mvc相關的bean(controller等)。
轉自:http://blog.csdn.net/walkerjong/article/details/7839002
相關推薦
Spring+Mybatis整合事務不起作用之解決方案彙總
前言: 公司最近一個專案用到Spring和Mybatis,發現用起來挺方便,比以前的那個struts+hibernate舒服多了。廢話少說,直接擺問題,碰到的問題是,mybatis不在事務中執行,後臺日誌報 “Closing no transactional SqlSes
Spring+Mybatis整合事務不起作用之…
前言: 公司最近一個專案用到Spring和Mybatis,發現用起來挺方便,比以前的那個struts+hibernate舒服多了。廢話少說,直接擺問題,碰到的問題是,mybatis不在事務中執行,後臺日誌報 “Closing no transactional SqlSession [[email
spring-hibernate整合 事務不起作用
當spring和hibernate 整合後,事務不起作用 解決辦法:通過Spring的SessionFactory的getCurrentSession的方法建立Session 一、首先說一下hibernate中建立用來連線資料庫的Session,有兩種方式。 1.通過Sp
spring,springmvc,hibernate整合事務不起作用
剛學習完這三大框架,跟著別人的部落格整合一下,就測試一下事務是否起作用,用的1/0來測試,用的@Transactional註解在service層,發現事務始終不起作用,也在網上查了一些資料,發現spring,springmvc配置檔案中掃描包衝突了,因為我的是在web.xm
Spring Boot使用事務不起作用
今天使用spring boot做關於事務的demo時發現在service層使用@Transactional註解執行之後遇到錯誤並不能回滾。@Service public class HelloContr
Spring 事務不起作用的幾種情況
service() true bean 每次 語句 imp 作用 stc current 1:必須是runtime異常,方法不要有try catch語句。 2:service 中 @Service() public class AServiceImpl1 imple
jdk動態代理引起的spring事務不起作用
最近做專案遇到了一個很奇怪的問題,大致的業務場景是這樣的:我們首先設定兩個事務,事務parent和事務child,在Controller裡邊同時呼叫這兩個方法,示例程式碼如下: 1、場景A: 這裡其實是分別執行了兩個事物,執行的結果是兩個方法都可以插入資料!如下: 2、場景B: 修改上述程式碼如下:
在使用spring mvc時,我使用了@Service這樣的註解, 發現使用註解@Transactional宣告的事務不起作用
問題出現的場景: 在使用spring mvc時,我使用了@Service這樣的註解, 發現使用註解@Transactional宣告的事務不起作用。 我的配置如下: <mvc:annotation-driven /> <context:component-s
spring @Transactional 方法內事務不起作用的解決辦法
class ManagerImpl implements Manager { @Override public void Method(){updateAndDelete()} public void updateBankCardBalance(){dao.update(
Spring事務不起作用原因
首先宣告: Checked異常繼承java.lang.Exception類。Unchecked異常繼承自java.lang.RuntimeException類。 而,Spring的事務實現採用基於AOP的攔截器來實現,如果沒有在事務配置的時候註明回滾的checked exception, 那麼只有在發生了un
springMVC mybatis mysql 配置 事務不起作用的原因
springMVC和spring一起使用的時候,配置檔案會先後生成。大家現在多數喜歡用spring的註解方式進行配置,在配置檔案中會有包的掃描過程,然而spring的掃描生成的service是具有事務處理能力的service,springMVC掃描出來的service是不具有事務處理能力的service。sp
Spring事務不起作用問題彙總
1、首先使用如下程式碼 確認bean 是代理物件嗎? 必須是Spring定義(通過XML或註解定義都可以)的Bean才接受事務。 直接new出來的物件新增事務是不起作用的。 可以通過以下方式判斷是否是代理物件: AopUtils.isAopProxy(Object o
springmvc mybatis 註解的方式 事務不起作用解決方案
1、使用springmvc導致事務不起作用時,首先檢視是否元件重複掃描問題導致 由於採用的是SpringMVC、 MyBatis,故統一採用了標註來宣告Service、Controller。伺服器啟動時的載入配置檔案的順序為web.xml---root-con
spring 事務不起作用 問題彙總
今早接手了別人的專案,測試了一下事務,發覺不起作用,檢查了N久程式碼,才發覺是springMVC 配置檔案裡context:component-scan重複掃碼了service,導致事務不起作用(建議MVC scan只掃描action或Controller),
spring事務、當業務中並沒有異常丟擲時執行一半的事務無法正常出發導致資料一致性遭到破壞、事務不起作用
spring 事務:當所攔截的方法有指定錯誤丟擲的時候觸發事務的執行。 有些情況下正常開發的業務並沒有異常的發生、但是業務要求並沒有滿足的時候並不會有異常的產生。這個時候需要我們自己手動丟擲異常。觸發事務、保證資料的一致性。 手動丟擲異常有兩種方式: 一:手動丟擲異常:
Spring Ehcache中@Cacheable不起作用。
cts pri col time 查找文件 後綴名 數值 spring div 確認各項配置沒有問題。 一開始關於EhCache的配置放在了SpringMVC.xml中,移動到applicationContext.xml中,解決。 以下為EhCache的配置: <!-
spring boot2 配置 FastJsonHttpMessageConverter 不起作用
專案使用自定義 FastJsonHttpMessageConverter 進行API資料響應JSON轉換器 在原來springboot1.X 版本中是可以生效,配置如下: /** * 替換使用 FastJson 解析返回結果 */ @Overrid
@Transactional事務不起作用原因補充
今天,使用@Transactional,但是事務卻不起作用,配置確定配置成功,在網上查詢資料,在每個每個原因都試了之後仍然無效,最終發現,原來他們都漏了一個原因,而我正好是這個原因。 因為spring
Spring註解式事物不起作用的原因分析
1、首先檢查配置檔案是否正確:<!-- 事物管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionM
事務不起作用
參考:http://blog.csdn.net/qq_32588349/article/details/52097943 http://blog.csdn.net/u010310183/article/details/51769407 重點:使用context:includ