JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Application Framework » Spring  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 請教 Transaction of Spring 3.0.5 + MyBatis 的使用
scharwenka





發文: 4
積分: 0
於 2011-07-06 23:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請教 Transaction of Spring 3.0.5 + MyBatis 的使用.
欲使用類似 Spring 3.0.5 + Hibernate 使用Transactoin的手法來如法泡製, 但不斷失敗.
Google上看了好幾種, 都介紹不全, 關鍵點應該是在applicationContext.xml 中使用org.springframework.transaction.interceptor.TransactionProxyFactoryBean 時的錯誤, 錯誤訊息如下:
Error creating bean with name 'personSession2' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>

哪位先進知道此問題, 在煩請告知, thanks.


reply to postreply to post
作者 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 [Re:scharwenka]
mmncsmm





發文: 12
積分: 0
於 2011-07-07 14:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
試試看
org.springframework.jdbc.datasource.DataSourceTransactionManager

如果不行請把applicationContext.xml 貼出來看看


reply to postreply to post
my blog : http://mmncsmm.blogspot.com
作者 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 [Re:scharwenka]
daith





發文: 13
積分: 0
於 2011-10-02 07:55 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思,發生類似的問題,借這篇文章問.

採用Spring 3.0.5 + myBatis,並想要使用Spring 提供的Transaction機制,使用了兩個設定方式,但無rollback.

設定方式1. 採用@Transactional.

application-myBatis.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!-- Transaction -->
  <bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
 
  <!-- Mybatis's sqlSessionFactory config -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="mapperLocations">
      <list>
        <value>classpath:com/test/mapper/sqlMapper/*Mapper.xml</value>
      </list>
    </property>
  </bean>

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="annotationClass" value="org.springframework.stereotype.Repository" />
    <property name="basePackage" value="com.test.mapper" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  </bean>

        <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.test.mapper.basic.UserMapper" />
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  </bean>

     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="newtonDataSource" />
       <property name="mapperLocations" value="classpath:mappers/*.xml" />        
       <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

     <tx:annotation-driven transaction-manager="transactionManager" >


ServiceImp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Repository
@Transactional 
public class UserServiceImp implements UserService {
 
  @Autowired
  private UserMapper userMapper;
 
  @Autowired
  private UserFamilyInfoMapper userFamilyMapper;
 
       @Transactional 
  public void insertEntity(User entity) throws Exception {
 
    userMapper.insertEntity(entity);
    for (UserFamilyInfo info : entity.getUserFamilyInfo()) {
      info.setUserID(entity.getUid());
      userFamilyMapper.insertEntity(info);
    }
  }
....
}


方法2. 採用JTA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- ======================================= -->
  <!-- Service Methods Transaction Settings -->
  <!-- ======================================= -->
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="query*" read-only="true" />
      <tx:method name="get*" read-only="true" />
      <tx:method name="find*" read-only="true" />
      <tx:method name="load*" read-only="true" />
      <tx:method name="save*" rollback-for="java.lang.Exception" />
      <tx:method name="insert*" rollback-for="java.lang.Exception" />
      <tx:method name="modify*" rollback-for="java.lang.Exception" />
      <tx:method name="update*" rollback-for="java.lang.Exception" />
      <tx:method name="delete*" rollback-for="java.lang.Exception" />
      <tx:method name="change*" rollback-for="java.lang.Exception" />
      <tx:method name="remove*" rollback-for="java.lang.Exception" />
    </tx:attributes>
  </tx:advice>
 
  <aop:config>
    <aop:pointcut id="serviceOperation" expression="execution(* com.test.services..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
  </aop:config>


主要是for時insert 失敗,應該是整個method需要rollback.
但是卻主檔會inset成功.


reply to postreply to post
作者 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 [Re:daith]
pico2k

地球真危險



發文: 177
積分: 0
於 2011-10-20 14:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
改用TransactionProxyFactoryBean...

reply to postreply to post
作者 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 [Re:daith]
jini

SoftLeader Taiwan

版主

發文: 1266
積分: 23
於 2012-02-24 20:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問 方法1 與 方法2 都會造成主檔 insert 成功嗎 ?

我猜想是不是你的 jdbc connection auto-commit 設為 true ?!


reply to postreply to post
» JWorld@TW »  Java Application Framework » Spring

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8