File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts Action and Business layer - Thread safe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Action and Business layer - Thread safe?" Watch "Struts Action and Business layer - Thread safe?" New topic
Author

Struts Action and Business layer - Thread safe?

Venkataramanan Amirthalingam
Greenhorn

Joined: Nov 27, 2008
Posts: 20
Hi, I understand that Struts Action classes are not thread safe and hence we should not create instance variables. How about reference to business classes? If i need to invoke business classes from action class, should i instantiate the business class inside the method or can i have one instance created and reuse it?

E.g Pseudo code

==approach 1==
TestAction
{
execute()
{
AddBusinessImpl addBusiness = new AddBusinessImpl();
addBusiness.add(10,20);
}
}

==approach 2==
TestAction
{
private AddBusinessImpl addBusiness = null;
execute()
{
if(addBusiness == null)
{
addBusiness = new AddBusinessImpl();
}
addBusiness.add(10,20);
}
}

I believe both approach will work and which is the best and thread safe? Also in second approach, if two requests hit the action class EXACTLY at the same time, will both requests instantiate the AddBusinessImpl class (assuming IF condition for NULL check returned TRUE for both requests).

Kindly guide the corret approach and the thread safe implementation.

Thanks,
venkat
[ November 28, 2008: Message edited by: Bear Bibeault ]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You don't say which version of Struts you use. S1 actions are not thread-safe. S2 actions, since they're instantiated per-request, typically are.

As to whether or not *your* business or service objects are thread-safe I'm not exactly sure how we'd know.
Venkataramanan Amirthalingam
Greenhorn

Joined: Nov 27, 2008
Posts: 20
Hi David Newton,

The question is very simple. Assuming my business class is thread safe, how should i instantiate it? Should i do it inside the method(as local variable) or can i create an instance and reuse it? If i create an instance, what would happen if 2 or more threads invoke the action execute method EXACTLY AT THE SAME TIME? Will both try to check for NULL and create the instance? Kindly refer my pseudo code. Kindly resolve this.

Regards,
venkat
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If your business class is thread-safe then it doesn't make much difference which way you do it. You still didn't say which version of Struts you're using; it makes a difference.

If it's Struts 2, then it doesn't make any difference whatsoever, because each action will have its own instance of AddBusinessImpl.

If it's Struts 1, approach one will create a new instance of AddBusinessImpl in each request thread: even though there's only one instance of the action each method call is in its own thread.

Approach two uses a shared AddBusinessImpl; if it's truly thread-safe the worse-case scenario is that both requests create a new AddBusinessImpl and one of them replaces the first. If the action's execute() method is invoked at "EXACTLY THE SAME TIME" then you've introduced a race condition since you're not synchronizing the instantiation. Since the business class is thread-safe, it may not matter, but I'd argue it's a bad habit.

Of course, if it's Struts 1, and AddBusinessImpl actually *is* thread-safe, there's zero reason to create the instance in the execute() method--you could just use:



On a stylistic note, if AddBusinessImpl is really an implementation, the addBusiness member should really reference its interface, allowing for easier injection of test implementations. That would also affect the code inside the execute method: the point of having interfaces and implementations is so the main-line code doesn't care what the implementation is; it would simply access addBusiness, the implementation of which is determined elsewhere.
[ November 28, 2008: Message edited by: David Newton ]
Venkataramanan Amirthalingam
Greenhorn

Joined: Nov 27, 2008
Posts: 20
Hi David,

I am really sorry for not mentioning the version of struts. We are using Struts 1.2 and thanks for the response.

In case if i use interfaces, where should i set/refer the instance of actual implementation?

(1) If i reuse the instance (by checking NULL ), then again the race condition will occur.

(2) If i do it the following way,
---
private AddBusiness addBusiness = new AddBusinessImpl();
---, then the actual implementation class is explicitly programmed/mentioned in the source file.

If i have to inject this, then probably i should go for Spring kind of frameworks. Am i right?

Kindly let me know your thoughts/inputs.

Regards,
venkat
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes, you could use Spring or an equivalent. Short of going for a full solution, though, you could still directly instantiate the desired implementation, but leave the option open for the future, and use the implementation setter available for testing purposes.

I already explained the pros and cons of the race condition you have--it doesn't matter (much) if the business class is really thread-safe, although such situations raise an eyebrow.
Venkataramanan Amirthalingam
Greenhorn

Joined: Nov 27, 2008
Posts: 20
Hi David Newton,

Thanks for the reply and if i use STRUTS 2, should i use SPRING IOC / dependency injection? Since you said in STRUTS2, one instance of action will be created for each requests, business classes also will get created that many instances. Hope i am right here. So, will SPRING DI is needed here?

Regards,
venkat
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Well, I'd hesitate to say you *should* use Spring, since there are other options--it's not only a technical decision. *I* use Spring, because I also use several other of its features (AOP being the big one, and a lot of functionality builds off of that).

There's no reason you can't use Spring with Struts 1, though, and it's not necessary to use Spring at all. The original question doesn't have anything to do with my comment about making the member variable declaration an interface rather than a concrete implementation.
Venkataramanan Amirthalingam
Greenhorn

Joined: Nov 27, 2008
Posts: 20
Hi David Newton,

Thanks Well, i am new to Spring and in our application, we use Struts1.2 and Spring framework. We just use Delegate Action Proxy and Dependency Injection by bean definitions in XML file.


In case if i have to move to STRUTS 2 version and if we use Spring just for Dependency injection, then i believe i dont need SPRING here. Kindly confirm.!!!

And if something wrong here, kindly correct me.


Regards,
venkat
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In case if i have to move to STRUTS 2 version and if we use Spring just for Dependency injection, then i believe i dont need SPRING here. Kindly confirm.!!!

Um, if you use Spring for DI then yeah, you'll need Spring.

We seem to be going in circles. If you're currently using Spring, why would you *stop* using Spring if you switch to Struts 2? If you're *already* using Spring, then you can already inject the implementation, and the bulk of this discussion is redundant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts Action and Business layer - Thread safe?