aspose file tools*
The moose likes Spring and the fly likes Transaction Management in Spring with SERVICE and DAO layers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Transaction Management in Spring with SERVICE and DAO layers?" Watch "Transaction Management in Spring with SERVICE and DAO layers?" New topic
Author

Transaction Management in Spring with SERVICE and DAO layers?

Rao Potla
Greenhorn

Joined: Jan 14, 2012
Posts: 11
How to apply different PROPAGATION transaction attributes for methods in SERVICE and DAO layers using Classic Spring AOP(i.e using TransactionProxyFactoryBean)?

Below Example is using @Transactional annotation. I would like know How to apply the same using TransactionProxyFactoryBean(Spring 1.x) (or) Transaction Advices (Spring 2.x i.e <tx:advice> and <aop:config>)

//Service Interface


//Service Implementation





//DAO Interface


//DAO Implementation


//beans.xml


// example beans.xml with TransactionProxyFactoryBean .
Here I could able to configure "transactionAttributes" for only Service(i.e Cashier) methods . How to apply "transactionAttributes" differently for DAO(i.e BookShop) methods like we did using @Transactional Annotation ? and the same with <tx:advice> ,<aop:config> ?


// example beans.xml using Trasaction Advices(Spring 2.x way)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6



look at the attributes of tx:method. It includes all the attributes of the @Transactional annotation.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rao Potla
Greenhorn

Joined: Jan 14, 2012
Posts: 11
Mark Spritzler wrote:

look at the attributes of tx:method. It includes all the attributes of the @Transactional annotation.

Mark




sorry Mark, I have placed "cashier" instead of "chechout" in <tx:method>


moreover in <tx:method> "checkout" is the name of the method in Cashier interface(i.e Service layer) & for that "checkout" method we are applying default transaction attributes. Now I would like to apply like <tx:method name="purchase" propagation="REQUIRES_NEW"/> <-- "purchase" is BookShop (DAO layer) interface method.

can I configure like the below? please help me in this.




even I tried the above one, but it is not working correctly.

Initial data in the tables

SQL> select * from BOOK;

ISBN BOOK_NAME PRICE
---------- ------------------------- ----------
0001 The First Book 30
0002 The Second Book 50

SQL> SELECT * FROM BOOK_STOCK;

ISBN STOCK
---------- ----------
0001 10
0002 10

SQL> SELECT * FROM ACCOUNT;

USERNAME BALANCE
-------------------------------------------------- ----------
user1 40

After the below operation execution



The data in the tables should be like

SQL> select * from BOOK;

ISBN BOOK_NAME PRICE
---------- ------------------------- ----------
0001 The First Book 30
0002 The Second Book 50

SQL> SELECT * FROM BOOK_STOCK;

ISBN STOCK
---------- ----------
0001 9
0002 10

SQL> SELECT * FROM ACCOUNT;

USERNAME BALANCE
-------------------------------------------------- ----------
user1 10


because, there should be one successful Transaction of First time purchase("0001","user1") method execution. But when run the above code with that configuration (<tx:advice>) in beans.xml I am not getting the first purchase()'s transaction successfully to get the data in DataBase as show above.


Please help me in this?

Thanks
RaoPotla

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Just what I said, the tx:method tag has attributes for propagation.

If you haven't used tx:method before, then check it out in the SpringFramework.org Spring Framework Documentation under the transactions section.

here shows the settings
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-txadvice-settings

and here is an example
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-first-example

and here is another example
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-diff-tx

Mark
Rao Potla
Greenhorn

Joined: Jan 14, 2012
Posts: 11


Thank you very much Mark, the following URL is very helpful for me & this is what I am looking for.

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-diff-tx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction Management in Spring with SERVICE and DAO layers?