aspose file tools*
The moose likes Spring and the fly likes Spring transaction using AOP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring transaction using AOP" Watch "Spring transaction using AOP" New topic
Forums: Spring Aspect Oriented Programming
Author

Spring transaction using AOP

Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
Hi friends,
I have a question regarding spring transaction using AOP.
I can able to declare an transaction advice in spring.xml and able to apply the advice to respective methods using pointcut.




Here transactionAdvice(txAdvice) is applied to all methods starting with book present in org.simple.service.TicketBookingService.



But,

I would like to use annotation.So what i did is

This is also working fine.

Now what I feel is,here @Transactional is repeated in all methods which is starting with book.So i would like to put in some common area.
So I did the following,

1.
in spring.xml,by mentioning this spring will scan for aspect components.

2.
I created a class called TransactionLoggingAspect.





My question is,
what is the annotation equivalent of the above "txAdvice"?
In TransactionLoggingAspect.java,Where I can put the annotation equivalent?

In spring tutorial,
http://static.springsource.org/spring/docs/3.0.x/reference/transaction.html
they have mentioned for XML part,which is working fine.
But I am searching for annotation part.But not found.

Please help me in this issue.

Regards,
Anand
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

To use @Transactional Annotations. Your config has to have <tx:annotation-driven/> And you need to have a transactionManager bean. That is all. Now you can put @Transactional on any service method and it will be transactional.

No need for any aop or aspect classes. Everything that you did before in xml is taken care of. That is why using annotations for Transactional methods is so much easier than using xml for it.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
Mark Spritzler wrote:To use @Transactional Annotations. Your config has to have <tx:annotation-driven/> And you need to have a transactionManager bean. That is all. Now you can put @Transactional on any service method and it will be transactional.

No need for any aop or aspect classes. Everything that you did before in xml is taken care of. That is why using annotations for Transactional methods is so much easier than using xml for it.

Mark


Hi Mark Spritzler,
Thanks for your comment. I understood your comment and totally agree with that.
My question here is,
I keep on put @Transactional for any service methods that i needed.I feel lazy to put @Transactional also.
Instead I would like to put in some common area @Transactional and declare that methods starting with book*(bookBusTicket,bookPlaneTicket,bookTrainTicket) present in org.simple.service.TicketBookingService class.
This I can put in xml as


But I would like in programmatic way.(@Transactional in common area and declare methods starting with book*(bookBusTicket,bookPlaneTicket,bookTrainTicket) present in org.simple.service.TicketBookingService class)
Is there any programmatic way?(May be annotation or something)
(My question may be wrong.correct me if i am wrong)

Regards,
Anand
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well, you can put @Transactional on the class level and then all methods from your interfaces that that class implements are now all transactional.

That is a way to put the annotation in once and get many methods covered.

So one trick it to make sure you put all those methods that start with into a single interface that that class implements. Or just put @Transactional on that interface.

Mark
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
Mark Spritzler wrote:Well, you can put @Transactional on the class level and then all methods from your interfaces that that class implements are now all transactional.

That is a way to put the annotation in once and get many methods covered.

So one trick it to make sure you put all those methods that start with into a single interface that that class implements. Or just put @Transactional on that interface.

Mark


Oh.
You are telling to put @Transaction in the class level or interface level

1.
class level - flavor 1


2.
interface level - flavor 2


These are the two flavors you are coming to say. Am I right?

Regards,
Anand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring transaction using AOP