2012-04-04 39 views
0

mybatisスプリングトランザクション管理を使用しようとしています 私の問題は、例外がスローされてもトランザクションがコミットされていることです。 これに比較的新しいですが、何かの助けがありがたいです。 次のコードは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> 

サービスクラス

@Transactional(rollbackFor = Exception.class、伝播= Propagation.REQUIRED) 公共ボイドinsertNotes(文字列noteTypeId、文字列のスニペットでありますString noteType、String noteTypeValue、 文字列claimNumber、文字列notepadId、文字列モード) {

 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()メソッドを含むクラスのautowiredオブジェクトを使用して別のクラスのメソッドから呼び出す場合、それはうまくいくはずです。たとえば

については

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

こんにちは、私はそれを呼び出しています、現在の方法をinsertNotes呼び出す前に、すべてのパラメータが最終確認する必要があります – user1163585

+0

コードをservice.Doingへのコントローラ呼び出しで更新したのと同じように私は推測します。 – user1163585

0

あなたは、トランザクションのテンプレートを使用して試すことができます。 @Tranasactionalアノテーションをメソッドから削除し、次のコードをxmlファイルに追加します。

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

Trasactiontemplateのオブジェクトを作成し、この

@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を呼び出す:あなたは方法

関連する問題