0

I'm using Spring 4.0.5, Mysql 5.6.19 and BoneCP 0.8.0

My issue is that, in one transaction defined in the application, MySql is commiting each insert or update, so it does not making a transaction.

First of all, I have read some questions like mine, but setting this in the Spring datasource does not work for me:

<property name="defaultAutoCommit" value="false" />

My classes are these:

DBWriter.java

    private DataSourceTransactionManager txManager; // Injected in Spring Beans XML
private IMyDAO writerDAO;

public void saveBeans(DataContainer targetData) throws Throwable{
    try {
        JdbcTemplate templateTransaction = new JdbcTemplate(txManager.getDataSource());

        MyTx newTx = new MyTx(targetData, templateTransaction, writerDAO);

        TransactionTemplate txTemplate = new TransactionTemplate(txManager);
        txTemplate.execute(newTx);
    } catch (Throwable e) {
        logger.error("Error saving into DB", e);
        throw e;
    }
}

MyTx.java

public class MyTx extends TransactionCallbackWithoutResult {
private IMyDAO writerDAO;
private DataContainer finalData;
private JdbcTemplate txTemplate;

public MyTx(DataContainer newData, JdbcTemplate newTxTemplate, IMyDAO writerDAO){
    this.finalData = newData;
    this.txTemplate = newTxTemplate;
    this.writerDAO = writerDAO;
}

@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
    writerDAO.saveTargetBean(newData, txTemplate);
}

}

MyDAO.java

    private void saveTargetBean(...) {
    jdbcTemplate.update("INSERT...", ...); // First
    jdbcTemplate.update("INSERT...", ...); // Second
    jdbcTemplate.update("INSERT...", ...); // Third
}

My issue is, when debugging step by step or forcing failures, First, Second, Thirs is committing immediately to database, without transactional behaviour.

Is there anything wrong in my code or in my approach? Should I put the INSERTs sentences directly in doInTransactionWithoutResult method? Should I do this in another way, so the three inserts were done in a transactional way?

Any help would be very very appreciated. Regards

yaki_nuka
  • 724
  • 4
  • 26
  • 44
  • 1
    You are using MySQL make sure you are using tables that support transactions. MyISAM tables don't support transactions, make sure they are InnoDB tables. Why aren't you using declarative transactions but are using manual transactions? Also `JdbcTemplate` and `TransactionTemplate` are thread safe, create them in your context, inject and reuse them. Especially `JdbcTemplate` is quite a large and time consuming object construction. – M. Deinum Jun 22 '14 at 12:28
  • @m-deinum, thank you for your answer. I'll try declarative transactions. I'm using InnoDB tables (verified). – yaki_nuka Jun 22 '14 at 15:43

1 Answers1

0

As Martin suggest, declarative transaction with @Transactional are better for this, and works for me. So, thank you

These two links were very helpful for me in this topic:

Propagation types

@Transactional example

yaki_nuka
  • 724
  • 4
  • 26
  • 44