I am learning Spring-MVC in my first project here.
After reading the documentation on transactions I noted that all of the examples put transactions around the service objects/methods, not the DAO (data access objects/methods).
I wondered why. Without knowing better I would think to add transactions around most of my DAO methods that access the database (my thinking: database=transactions). I don’t yet have many service methods that span multiple DAO’s (but I guess that could be a reason for marking services as transactional).
The question:
I just want to know what others do in this situation. Do you naturally put transactions around the lowest level item that you can (e.g. around DAOs always, and around services only when they span multiple DAO’s in a way which requires transactions)?
Or do you just focus on transactions around services as a general principal? Thus sticking to one layer because that’s more all-encompassing in the long run?
For my money I try the put the transaction at the coarsest point possible in the app, this tends to be a service/manager like object that coordinates calls to one or more finer gained dao calls.
In theory you could put transactions everywhere from services to daos, assuming they are defined to join an existing transaction if required. In practice I’ve found this less than useful as it is unnecessary and will just annoy you when attempting to debug the code.