My problem is simple, I want to rollback all the db changes that were caused by invocation of my service method. I have a problem with correct annotating. I’m not really sure where to put @Transactional and where should I set Propagation to REQUIRES_NEW or MANDATORY. I will paste a code that I wrote, which apart from the fact that SQLException is thrown does not rollback db changes.
The code:
@Service
public class SwapInsertService {
@Autowired
MyDao myDao;
@Transactional(propagation = Propagation.REQURIES_NEW, rollbackFor = SQLException.class)
public void insertToManyTables(MyData data) throws SQLException {
insertToSpecificTables(data);
myDao.insertTwo(data);
}
@Transactional(propagation = Propagation.MANDATORY)
public void insertToSpecificTables(MyData data) throws SQLException {
myDao.insertOne(data);
}
}
@Repository
public class MyDaoImpl implements MyDao {
@Transactional(propagation = Propagation.MANDATORY)
public void insertOne(MyData data) throws SQLException {
// INSERT
}
@Transactional(propagation = Propagation.MANDATORY)
public void insertTwo(MyData data) throws SQLException {
// throws SQLException
}
}
Let’s assume insertTwo throws SQLException . I would like to rollback all the previous inserts. What am I doing wrong?
My understanding:
Propagation.REQURIES_NEW creates new transaction.
Propagation.MANDATORY supports current transaction.
insertTwo throws error, insertToManyTables rollbacks everything and voilà. Unfortunately it is not that easy.
EDIT: It’s important to mention that i’m using CGLIB for proxies.
Check whether AUTO_COMMIT is set to false in data source configuration in spring specific xml