This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Struts actions and declarative transactions

Aug 16th, 2004, 06:54 AM

Hi,

The question is: What is the simplest way to define declarative transaction demarcation for all my Struts actions?

If I try to explain further:
Our project currently uses Struts and Castor. We want to move to Spring and Hibernate (with Struts still present for the moment).
The architecture we came up with is like this (giving example class names):

I have written unit tests and it works beautifully. However, all the methods in the managers are the methods present in the DAOs... Which means that the managers are not real managers at the moment, but a facade to call the DAO methods instead.

Now I want to adapt my Struts actions to use this setup. Unfortunately, at the moment in our Struts actions, what we do is this:
1. Get a transaction context, and begin transaction
2. Get necessary parameters from request object
3. Do lots of business logic
4. At the end, commit or rollback the transaction.

With the new Struts+Hibernate setup, I understand that I have to move lots of the code in my Struts actions to the Managers... As an example, we have a "CreateOfferAction" Struts action. I already refactored it to call the methods from OfferManager, which in turn calls methods of OfferDAO. However, since I defined declarative transactions for the OfferManager, each call from the Struts action takes its own transaction. But of course what I want is, that in a single Struts action, all calls to the OfferManager methods to share a single transaction.

So, let me repeat the question again: I have lots of Struts actions, which obviously have their own "execute" methods. How can I define in the applicationContext.xml file so that all "execute" methods of all Struts actions to have "PROPAGATION_REQUIRED" declarative transaction?

Of course, ideally, I'll refactor the actions to call the managers only, sending request parameters, and accepting parameters to be put into the response object. All transactons will take place in the manager methods. But at the moment, I don't have that time, so I'd like to learn if there is any simple way to make all "execute" methods of Struts actions to have their own transactions, using Spring AOP support...

With the new Struts+Hibernate setup, I understand that I have to move lots of the code in my Struts actions to the Managers...

Yes .

[...] have a "CreateOfferAction" Struts action. I already refactored it to call the methods from OfferManager, which in turn calls methods of OfferDAO. However, since I defined declarative transactions for the OfferManager, each call from the Struts action takes its own transaction. But of course what I want is, that in a single Struts action, all calls to the OfferManager methods to share a single transaction.

Consider rewriting Your Service layer in such a way, so You would be able to declare transaction on one "service level". Just delegate your "buisness logic" from actions into service layer.

So, let me repeat the question again: I have lots of Struts actions, which obviously have their own "execute" methods. How can I define in the applicationContext.xml file so that all "execute" methods of all Struts actions to have "PROPAGATION_REQUIRED" declarative

[...]

Of course, ideally, I'll refactor the actions to call the managers only, sending request parameters, and accepting parameters to be put into the response object. All transactons will take place in the manager methods. But at the moment, I don't have that time, so I'd like to learn if there is any simple way to make all "execute" methods of Struts actions to have their own transactions, using Spring AOP support...

In my applicationContext.xml file, I defined declarative transactions for my facade, such that if a method name begins with "update_", that method is executed in a transaction, for other methods, a transaction is not forced:

Unfortunately, the facade is tied to the servlet API right now, but I know that this should be abstracted as well, to let other kinds of clients (like Swing) use the facade... With HashMaps as inputs and outputts maybe...

Comment

Set up declarative transactions and in struts action make multiple calls to service layer. It seems that each call to the service layer results in a commit. I thought the transaction was tied to the local thread so it would only commit once per request. What am I missing here?

Comment

Set up declarative transactions and in struts action make multiple calls to service layer. It seems that each call to the service layer results in a commit. I thought the transaction was tied to the local thread so it would only commit once per request. What am I missing here?

No, the transaction is wrapped around the service method call. For this reason I like to make a single service call per action (which returns a composite view object).