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