I have a Spring Boot project with a test case that calls a public method deleteEntity(Long id) on a service. The method is annotated with @Transactional and it should delete an entity with the given id, after which it throws a RuntimeException to induce a rollback.
If I implement deleteEntity(Long id) by using JdbcTemplate, the rollback occurs (logs confirm this) but it is not reflected in my database. If I implement it by using a CrudRepository, it works fine.
This is the code for the service that is called in the test case (JdbcTemplate code is commented out) :
@Service
public class TransactionalAPI {
@Autowired
private JdbcTemplate template;
@Autowired
private QueueEntryDAO dao;
@Transactional
public void deleteEntity(Long entryID) {
dao.deleteById(entryID);
throw new RuntimeException("Fake exception for testing purposes.");
// int rows = template.update(String.format("DELETE FROM MYSCHEMA.BATCH_QUEUE WHERE ID = %d", entryID));
// System.out.printf("Deleted %d rows.", rows);
// throw new RuntimeException("Fake exception for testing purposes.");
}
}
My database is an IBM DB2 database. Creating a minimal working example may be useful except that it's in-house code and I don't have administrator access to the database. That's why I wrote this test case as a replacement (the real problem I'm trying to solve is that I'm trying to clean up the Spring Batch tables and I'm having issues making that code transactional).
I looked at internals of SimpleJPARepository to try and spot the differences compared to JdbcTemplate and it's telling the entity manager to remove the entity, but the code is quite involved so it's hard to compare. Any idea what could be happening here?