I think transactions belong on the Service layer. It's the one that knows about units of work and use cases. It's the right answer if you have several DAOs injected into a Service that need to work together in a single transaction.
I agree with that. Sometimes it does not matter, but sometimes you can benefit from that e.g. Bernate session is spanned for the while transaction, so all loaded objects are in 1st-level cache and you don't need to reattach the objects to session again, plus lazily loaded properties function without fuzz. – dma_k Sep 26 '10 at 18:47 Can a global transaction consist of more than one of these @Transactional(propagation = Propagation.
REQUIRED)? Or @Transactional is always a boundary for a transaction? I'm not sure I got it from the documentation, but it seems a bit that you can create even transactions that consist of @Transactional methods and everything that runs inside – Sloin Nov 4 '10 at 23:56 Yes, I think the outermost @Transactional is the one the becomes the boundary for the transaction if Propagation.
REQUIRED is turned on. – duffymo Nov 5 '10 at 0:10.
The normal case would be to annotate on a service layer level, but this really depends on your requirements. Annotating on a service layer will result in longer transactions than annotating on DAO level. Depending on the transaction isolation level that can youse problems, as concurrent transactions won't see each other's changes in eg. REPEATABLE READ.
Annotating on the DAOs will keep the transactions as short as possible, with the drawback that the functionality your service layer is exposing won't be done in a single (rollbackable) transaction. It does not make sense to annotate both layers if the propagation mode is set to default.
The correct answer for traditional Spring architectures is to place transactional semantics on the service classes, for the reasons that others have already described. An emerging trend in Spring is toward domain-driven design. Spring Roo exemplifies the trend nicely.
The idea is to make the domain object POJOs a lot richer than they are on typical Spring architectures (usually they are anemic), and in particular to put transaction and persistence semantics on the domain objects themselves. In cases where all that's needed is simple CRUD operations, the web controllers operate directly on the domain object POJOs (they're functioning as entities in this context), and there's no service tier. In cases where there's some kind of coordination needed between domain objects, you can have a service bean handle that, with @Transaction as per tradition.
You can set the transaction propagation on the domain objects to something like REQUIRED so that the domain objects use any existing transactions, such as transactions that were started at the service bean. Technically this technique makes use of AspectJ and . Roo uses AspectJ inter-type definitions to separate the entity semantics (transactions and persistence) from the domain object stuff (basically fields and business methods).
Transactional Annotations should be placed around all operations that are inseparable. For example, your call is "change password". That consists of two operations change the password audit the change email the client that the password has changed So in the above, if the audit fails, then should the password change also fail?
If so, then the transaction should be around 1 and 2 (so at the service layer). If the email fails (probably should have some kind of fail safe on this so it won't fail) then should it roll baack the change password and the audit? These are the kind of questions you need to be asking when deciding where to put the @Transactional.
It is better to have it in the service layer! This is clearly explained on one of the article that I came across yesterday! Here is the link that you can check out!
In general I agree with the others stating that transactions are usually started on the service level (depending on the granularity that you require of course). However, in the mean time I also started adding @Transactional(propagation = Propagation. MANDATORY) to my DAO layer (and other layers that are not allowed to start transactions but require existing ones) because it is much easier to detect errors where you have forgotten to start a transaction in the caller (e.g. The service).
If your DAO is annotated with mandatory propagation you will get an exception stating that there is no active transaction when the method is invoked. I also have an integration test where I check all beans (bean post processor) for this annotation and fail if there is a @Transactional annotation with propagation other than Mandatory in a bean that does not belong to the services layer. This way I make sure we do not start transactions on the wrong layer.
Also, Spring recommends only using the annotation on concrete classes and not interfaces. static.springsource.org/spring/docs/2.0.....
Does'nt it make sense to annotate both the service layer and the dao layer - if one wants to make sure that dao method is always called (propogated)from a service layer with propogation "mandatory" in dao. This would provide some restriction for dao methods from being called from UI layer (or controllers). Also - when unit testing Dao layer in perticular - having dao annotated will also ensure it is tested for transactional functionality.
Usually, one should put a transaction at the service layer. But as stated before, the atomicity of an operation is what tells us where an annotation is necessary. Thus, if you use frameworks like where a single "save/update/delete/...modification" operation on an object has the potential to modify several rows in several tables (because of the cascade through the object graph), of course there should also be transaction management on this specific DAO method.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.