Vinoth Kumar C M
Vinoth Kumar C M

Reputation: 10598

Can we create a new instance for struts 1.x action classes each time?

The action classes of struts 1.x is inherently not thread safe since the struts caches the action classes and uses it for other requests too.

Is there any way we can configure struts 1.x action classes in such a way that a new instance of the action class is created each time ?

If it is possible to do so, are there any downsides to doing it ?

Upvotes: 1

Views: 819

Answers (2)

Dave Newton
Dave Newton

Reputation: 160251

Ultimately, of course the answer is yes: you can get the complete source for Struts 1 and twist it into something it was not intended to be.

Should you? Almost certainly not. If you decide to undertake this, start with RequestProcessor.processActionCreate.

I'm with Brad, but would be a bit stronger: what you're going to try to do is a bad idea. Use session context, application context, thread locals, synchronization, etc. as the framework was intended to be used.

By moving outside the framework's intent, you open yourself up to an amount of technical risk that should concern you. Nothing in Struts 1 was tested with an action-per-request, because that's not how the framework was built. It may work. It may fail spectacularly. It may look like it's working. For awhile. Until it isn't, and you're screwed.

IMO it'd be more effective to use the framework as designed. What problem are you trying to solve? There's likely a better solution that wouldn't require at least a full release cycle to vet the modified framework's functionality.

Upvotes: 3

Brad
Brad

Reputation: 15879

Don't do it. As you've stated they're not thread safe.

If you need to hold state for the user, store the data in a database and a lookup key value in the user's HTTP_SESSION. If (and only if) you need better performance you can introduce a caching strategy.

If you're trying to store some data just for each single request, and don't want to or can't use the HTTP_SESSION, you could look at using ThreadLocal

Upvotes: 0

Related Questions