2012-04-04 7 views
0
begangen

Ich versuche Mybatis Frühling Transaktionsmanagement Mein Problem zu verwenden, ist, dass die Geschäfte begangen zu werden, auch wenn eine Ausnahme ausgelöst wird. Relativ neu dazu wird jede Art von Hilfe sehr geschätzt. Im Folgenden sind die Code-SchnipselFrühjahr Mybatis Transaktion immer

Frühjahr xml Konfiguration

<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-Klasse

@Transactional (rollbackFor = Exception.class, Fortpflanzung = Propagation.REQUIRED) public void insertNotes (String noteTypeId, String securityValue, String summaryValue, String Hinweise, String notesId, String noteTypeValue, Zeichenfolge 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(); 





} 

nicht sicher, wo ich falsch bin dabei ...

Der aktuelle Anruf wird von der Steuerung wie unten angegeben

@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;  
} 

Antwort

1

ich hatte das gleiche Problem. Ich bin auch relativ neu im Frühling. Aber nach mir hängt es davon ab, wie Sie Ihre insertNotes() Methode aufrufen. Wenn Sie es von einer anderen lokalen Methode aufrufen, wird es nicht funktionieren, weil Spring nicht wissen kann, dass es aufgerufen wird und um die Transaktion zu starten.

Wenn Sie es von einer Methode einer anderen Klasse fordern von autowired Objekt der Klasse verwendet, die insertNotes() -Methode enthält, dann sollte es funktionieren.

Zum Beispiel

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

Hallo, zur Zeit die Art und Weise ruft ich es nenne ist – user1163585

+0

Haben sie den Code mit Controller Aufruf service.Doing es die gleiche Art, wie ich denke, aktualisiert. – user1163585

0

können Sie versuchen, Transaktionsvorlage. Entfernen Sie die @Tranasactional-Annotation von der Methode und folgen Sie dem Code in die XML-Datei.

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

erstellen Objekt von Trasactiontemplate und rufen insertNotes von der Steuerung wie diese

@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); 
      } 
     } 
    }); 

Hinweis: Sie müssen alle Parameter endgültig machen, bevor insertNotes Methode