I am using Hibernate and Spring on a small personal project. Well, still a newbie in this area, so would like to raise some basic questions re transactions.
-
It seems that I have to declare transactional manager and annotate the DAO class as
@Transactional(propagation = Propagation.REQUIRED, readOnly = false). Otherwise i get some strange exceptions or the entities are not saved in the database. Is there actually a must to use transactions? Can’t i save data in the database without using them (i thought that MySQL ISAM tables don’t support transactions), how would you use them then? -
What is the best place to put the @Transactional attribute on? Currently its declared on my generic HibernateDAO superclass, possibly the deepest level it can be. I guess that’s not the best place for that. But if moving it up, I would wind up having it in the Spring MVC Controller, which is arguably also inappropriate place for that. So far there is no other service layer, because i do nothing except for saving and serving words and definitions from the database.
Many thanks
Yes it is. One of the fundamental features of relational databases are transactions. However simple
@Transactionalwith default parameters is enough. You can however declare transactions in XML using AOP, surrounding whole set of classes, e.g. all having*DAOname.Service layer, see: What is the right way to use spring MVC with Hibernate in DAO, sevice layer architecture.