Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction Management in Spring with SERVICE and DAO layers?

 
Rao Potla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Mark
 
Rao Potla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic