How can I config to turn off autocommit in Spring + JDBC?

前端 未结 3 1808
礼貌的吻别
礼貌的吻别 2020-12-14 03:41

I am using Spring with JDBC and found that it is autocommit.

How can I config to turn it off in spring-servlet.xml?

This is my current configuration:

3条回答
  •  别那么骄傲
    2020-12-14 04:35

    You can't, simply run your code within a transaction, Spring will automatically disable auto-commit for you. The easiest (at least to set-up) way to run a piece of code in a transaction in Spring is to use TransactionTemplate:

    TransactionTemplate template = new TransactionTemplate(txManager);
    
    template.execute(new TransactionCallback() {
      public Object doInTransaction(TransactionStatus transactionStatus) {
        //ALL YOUR CODE ARE BELONG TO... SINGLE TRANSACTION
      }
    }
    
        

    提交回复
    热议问题