2012-04-04 94 views
0

我想使用mybatis spring事務管理 我的問題是即使引發異常事務也會被提交。 這個比較新,非常感謝任何幫助。 以下是代碼片段spring mybatis事​​務提交

彈簧xml配置

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 

     <property name="location"> 

      <value>classpath:Config.properties</value> 

     </property> 

    </bean> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="${db.driver}"/> 
    <property name="url" value="${db.url}"/> 
    <property name="username" value="${db.user}"/> 
    <property name="password" value="${db.pass}"/> 
    <property name="defaultAutoCommit" value="false" /> 
    </bean> 



    <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 


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


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <property name="configLocation" value="classpath:Configuration.xml" /> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSessionTemplate"> 
     <constructor-arg ref="sqlSessionFactory"/> 
    </bean> 

Service類

@Transactional(的rollbackFor = Exception.class,傳播= Propagation.REQUIRED) 公共無效insertNotes(字符串noteTypeId,字符串confidentialValue,String summaryValue,String notes,String notesId,String noteTypeValue, String claimNumber,String notepadId,String mode) {

 NotepadExample notepadExample= new NotepadExample(); 

     //to be moved into dao class marked with transaction boundaries 
     Notepad notepad = new Notepad(); 
     notepad.setAddDate(new Date()); 
     notepad.setAddUser("DummyUser"); 
     if("true".equalsIgnoreCase(confidentialValue)) 
      confidentialValue="Y"; 
     else 
      confidentialValue="N"; 
     notepad.setConfidentiality(confidentialValue); 
     Long coverageId=getCoverageId(claimNumber); 
     notepad.setCoverageId(coverageId); 
     notepad.setDescription(summaryValue); 

     notepad.setEditUser("DmyEditUsr"); 
     //notepad.setNotepadId(new Long(4)); //auto sequencing 
     System.out.println(notes); 
     notepad.setNotes(notes); 
     notepad.setNoteType(noteTypeValue); //Do we really need this? 
     notepad.setNoteTypeId(Long.parseLong(notesId)); 
     if("update".equalsIgnoreCase(mode)) 
     { 
      notepad.setNotepadId(new Long(notepadId)); 
      notepad.setEditDate(new Date()); 
      notepadMapper.updateByPrimaryKeyWithBLOBs(notepad); 

     } 
     else 
      notepadMapper.insertSelective(notepad); 

       throw new java.lang.UnsupportedOperationException(); 





} 

不知道我要去的地方錯了...

當前呼叫是從控制器下面

@Controller 
public class NotesController { 

    private static final Logger logger = LoggerFactory 
      .getLogger(NotesController.class); 

    @Autowired 
    private Utils utility; 

    @Autowired 
    NotepadService notepadService; 


public @ResponseBody List<? extends Object> insertNotes(HttpServletRequest request, 
     HttpServletResponse response,@RequestParam("noteTypeValue") String noteTypeId, 
    @RequestParam("confidentialValue")String confidentialValue, 
@RequestParam("summaryValue")String summaryValue, 
@RequestParam("notes")String notes , 
@RequestParam("notesId")String notesId, 
@RequestParam("noteTypeValue")String noteTypeValue, 
@RequestParam("claimNumber")String claimNumber, 
@RequestParam("notepadId")String notepadId, 
@RequestParam("mode")String mode) { 




    try { 
     notepadService.insertNotes(noteTypeId, confidentialValue, summaryValue, notes, notesId, noteTypeValue, claimNumber, notepadId, mode); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null;  
} 

回答

1

給出我有同樣的問題。我對春天也比較陌生。但根據我這取決於你如何調用你的insertNotes()方法。如果你從另一個本地方法調用它,那麼它將不起作用,因爲spring無法知道它被調用並啓動事務。

如果您使用包含insertNotes()方法的類的自動裝配對象從另一個類的方法調用它,那麼它應該可以工作。

例如

class ABC 
{ 
@Autowired 
NotesClass notes; 

    public void testMethod() { 
     notes.insertNotes(); 
    } 
} 


class NotesClass 
{ 
    @Transactional(rollbackFor=Exception.class, propagation=Propagation.REQUIRED) 
    public void insertNotes(String noteTypeId, 
          String confidentialValue, 
          String summaryValue,String notes , 
          String notesId,String noteTypeValue, 
          String claimNumber, 
          String notepadId, 
          String mode) { 
       //Your code 
    } 
} 
+0

嗨之前,你必須讓所有的參數最後,目前的辦法,我叫它是 – user1163585 2012-04-05 06:02:12

+0

已更新與控制器調用服務的代碼。以我猜測的相同方式做。 – user1163585 2012-04-05 06:25:48

0

您可以嘗試使用交易模板。從方法和下面的代碼中刪除@Tranasactional註釋到xml文件。

<bean id="trTemplate" class="org.springframework.transaction.support.TransactionTemplate"> 
<property name="timeout" value="30"/> 
<property name="transactionManager" ref="transactionManager"/> 
</bean> 

創建Trasactiontemplate的對象,從控制器調用insertNotes這樣

@Autowired 
private TransactionTemplate transactionTemplate; 

transactionTemplate.execute(new TransactionCallbackWithoutResult() { 

     @Override 
     protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) { 


      try { 
       insertNotes(); 
      } catch (Exception e) { 
       transactionStatus.setRollbackOnly(); 
       logger.error("Exception ocurred when calling insertNotes", e); 

       throw new RuntimeException(e); 
      } 
     } 
    }); 

注:調用insertNotes方法