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

» JWorld@TW » Java Application Framework » Spring  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 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
話題樹型展開
人氣 標題 作者 字數 發文時間
2686 請教 Transaction of Spring 3.0.5 + MyBatis 的使用 scharwenka 516 2011-07-06 23:49
2645 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 mmncsmm 107 2011-07-07 14:40
2295 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 daith 3372 2011-10-02 07:55
2243 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 pico2k 32 2011-10-20 14:06
2056 Re:請教 Transaction of Spring 3.0.5 + MyBatis 的使用 jini 83 2012-02-24 20:54
» JWorld@TW »  Java Application Framework » Spring

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8