I have a Service class like below:
@Service("MyService")
public class MyService {
@Autowired
MyDao dao;
public void process() {
getFromDao();
// getMoreFromDao();
// process();
// if all good, then
doStuff();
}
public void getFromDao() {
// do some stuff
dao.getData();
}
@Transactional(transactionManager="simpleDatasourceTxMgr", propagation=Propagation.REQUIRED)
public void doStuff() {
dao.saveData(1);
dao.saveData(2);
dao.saveData(3);
}
}
The DAO called is:
@Repository
public class MyDao {
@Autowired
@Qualifier("myjdbcTemplate")
NamedParameterJdbcTemplate jdbcTemplate;
public void saveData(obj a) {
jdbcTemplate.execute("Query", ...);
}
}
I want my doStuff()
method in the service class to run within a transaction and rollback everything if there is an exception in the saveData()
method. But this is not running in transaction.
If I add @Transaction
to a DAO method looks like it runs in separate transaction. Is this correct?
Update: I have added a process()
method to my Service and I call getFromDao()
and doStuff()
from process()
. process()
is called from the controller. So looks like if I make the service class @Transactional
, then everything executes within a transaction. But I don't want getFromDao()
to execute in transaction.
We use just JDBC and no Hibernate.