*
The moose likes Struts and the fly likes Spring and declarative transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Spring and declarative transactions" Watch "Spring and declarative transactions" New topic
Author

Spring and declarative transactions

Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Hi Matt,

EJB offers declarative transaction support for EJBs. I heard that Spring offers such a support for pojos. Is this true?

thanks.
Matt Raible
author
Ranch Hand

Joined: Jan 11, 2001
Posts: 114
Yes, Spring can wrap methods that exist in POJOs with transactions. Under the covers, it uses its AOP framework to intercept calls to the methods and wrap them in transactions according to rules you define. You can do this in XML or with source-level metadata (via Commons Attributes or JDK 5 annotations). Here's an XML example:



I've actually just started the first update chapter for Spring Live. It's on Transactions and the outline is on my blog.


Matt<br /> <br />Author: <a href="http://springlive.com" target="_blank" rel="nofollow">Spring Live</a> and <a href="http://www.apress.com/book/bookDisplay.html?bID=256" target="_blank" rel="nofollow">Pro JSP</a><br />Weblogs: <a href="http://raibledesigns.com" target="_blank" rel="nofollow">Raible Designs</a> <a href="http://jroller.com/page/raible" target="_blank" rel="nofollow">Spring Live Blog</a>
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Matt, are these interceptors implemented using Dynamic Proxies / somethign else? for that matter how is Spring AOP implemented. Does it fall under the category of Dynamic AOP like aspectwerkz / Do we have to write our aspects the way it is in AspectJ.
Matt Raible
author
Ranch Hand

Joined: Jan 11, 2001
Posts: 114
Originally posted by karthik Guru:
Matt, are these interceptors implemented using Dynamic Proxies / somethign else? for that matter how is Spring AOP implemented. Does it fall under the category of Dynamic AOP like aspectwerkz / Do we have to write our aspects the way it is in AspectJ.


Spring uses dynamic proxies when proxying interfaces and dynamic byte-code generation (using CGLIB0 when proxying against classes. It intercepts methods by generating subclasses dynamically. These subclasses override parent methods and have hooks to invoke interceptor implementations.

One limitation is that dynamic subclasses cannot override and proxy final methods.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
karthik if you like to go AOP-way (I would encourage you ) you should take a look at the nice solutions to integrate AspectJ (the most powerfull AOP solution for the moment) and AspectWerkz. These 2 solutions are very powerfull in what regards AOP and will probably serve you better than the simple solution (but in any case not bad solution ) of Spring.

./pope


blog - InfoQ.com
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
I hope that the following days I will have enough time to see how can somebody integrate JBossAOP with Spring (I will probably publish this after finishing the unit testing env research ).

./pope
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
I have read at some point that there may be a problem with Spring transactions . Was it fixed meanwhile?

./pope
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Ali Pope:
take a look at the nice solutions to integrate AspectJ (the most powerfull AOP solution for the moment) and AspectWerkz. These 2 solutions are very powerfull in what regards AOP and will probably serve you better than the simple solution (but in any case not bad solution ) of Spring.

./pope


In
this post matt pointed out the stuff that is missing in Spring.
Do you know of any other things missing in Spring that is present in AspectJ for eg. Does AspectWerkzz also use CGLIB for dynamic AOP?
The reason i'm asking this is I dont want to be learning a new framework if I can do most things with Spring. Why do you think that Spring AOP is a sub standard AOP solution?
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Matt Raible:

Spring uses dynamic proxies when proxying interfaces and dynamic byte-code generation (using CGLIB when proxying against classes. It intercepts methods by generating subclasses dynamically. These subclasses override parent methods and have hooks to invoke interceptor implementations.
One limitation is that dynamic subclasses cannot override and proxy final methods.


Thanks Matt that helps. Looks like CGLIB is everywhere these days?
Does'nt Hibernate also use CGLIB? On a side note do any of the application servers employ CGLIB for code generation for EJBs as well?
Matt Raible
author
Ranch Hand

Joined: Jan 11, 2001
Posts: 114
Originally posted by karthik Guru:
Looks like CGLIB is everywhere these days?
Does'nt Hibernate also use CGLIB? On a side note do any of the application servers employ CGLIB for code generation for EJBs as well?


Yes, Hibernate uses CGLIB extensively and has proven that it works well in many production environments. I have seen NoClassDefFoundErrors on Sun's App Server, but it seems to work quite well on most.

I don't think there are any proprietary app servers employ CGLIB to do code generation. JBoss uses Javassist for it's byte-code manipulation.
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
karthik: afaik AspectJ uses indeed CGLLIB too, but I saw some movement to ASM. AspectWerkz uses a combination of ASM and javassist.

./pope
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Matt I think that Hibernate uses in fact a patched version of CGLIB .

./pope
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Ali Pope:
karthik: afaik AspectJ uses indeed CGLLIB too,
./pope


You need to correct me if i'm wrong. I thought aspectj compiler manipulates the .class file. Does'nt CGLIB generate class files dynamically after the class is loaded in the JVM?
Does the original .class file (which is a target of a cross cutting concern) get changed when using CGLIB?
and what is ASM

thanks
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Ali Pope:
karthik: afaik AspectJ uses indeed CGLLIB too, but I saw some movement to ASM.


I should correct the above by saying that AspectJ uses BCEL (which is based on CGLIB) .

./pope
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by karthik Guru:
You need to correct me if i'm wrong. I thought aspectj compiler manipulates the .class file. Does'nt CGLIB generate class files dynamically after the class is loaded in the JVM?
Does the original .class file (which is a target of a cross cutting concern) get changed when using CGLIB?


ajc/iajc (AspectJ) compiler is written in java and so it manipulates also the code in memory . CGLIB is a bytecode manipulation solution, so you can use it to change a class.


and what is ASM


ASM is another very quick and with a very small footprint bytecode manipulation solution.

./pope
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Ali Pope:


ajc/iajc (AspectJ) compiler is written in java and so it manipulates also the code in memory ../pope


Guess you prefer to answer over multiple posts

Anyways to repeat my question...

ajc -->.class file --> modified .class file --> then loaded into JVM

What happens with CGLIB?

.class file --> while loading into JVM , is the .class file modified ?

How is this technique different from say what AspectWerkz uses?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
No i don't like this... but too many phone calls. So I execute a commit immediately .

Oke let's see:
ajc uses the javac normal compiler to create the class files and than injects into the class its custom information. So the sequence would look:
ajc -> .class + modified .class. Ajc is an offline compiler/weaver: meaning this must be done before starting the application. In the last months I think that Martin Lippert has created a load time weaver for AspectJ (I will explain in another post what is this ).
So BCEL (implicetely CGLIB) is used during the compilation/weaving step to create the final classes.
On the other hand AspectWerkz is mainly based on load time weaver: this means that the classes are changed at load time (using custom classloaders). I must underline that AspectWerkz offers also the possibility to weave offline.

./pope

Hope this helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring and declarative transactions
 
Similar Threads
Spring vs Other frameworks
Hibernate and transaction management
Spring/Struts/EJB?
Transactional DB access without EJB
Transaction Support