春季mybatis交易得到落实 [英] spring mybatis transaction getting committed

查看:61
本文介绍了春季mybatis交易得到落实的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Mybatis Spring交易管理 我的问题是,即使引发异常,事务也会被提交. 相对于此而言,任何帮助都深表感谢. 以下是代码段

I am trying to use mybatis spring transaction management My problem is that the transactions are getting committed even if an exception is thrown. Relatively new to this, anykind of help is much appreciated. Following are the code snippets

spring xml配置

spring xml configuration

 <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=Propagation.REQUIRED)
public void insertNotes(String noteTypeId,String 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();





}

不确定我要去哪里...

Not sure where I am going wrong...

当前呼叫来自如下所示的控制器

The current call is from the controller as given below

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

推荐答案

我遇到了同样的问题.我对春天也比较陌生.但是据我说,这取决于您如何调用insertNotes()方法.如果您是从另一个本地方法调用的,那么它将无法正常工作,因为spring无法得知它已被调用并开始事务.

I had the same issue. I am also relatively new to spring. But according to me it depends on how you are calling your insertNotes() method. If you are calling it from another local method then it will not work, because spring has no way of know that it is called and to start the transaction.

如果使用包含insertNotes()方法的类的自动装配对象从另一个类的方法中调用它,则它应该可以工作.

If you are calling it from a method of another class by using autowired object of the class which contains insertNotes() method, then it should work.

例如

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

这篇关于春季mybatis交易得到落实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆