I'm using a custom interceptor which creates a new db connection, and sets this connection onto the current action before executing the action. After that, the interceptor closes the connection.
I'm looking for a convenient way to share this db connection with other classes / static methods (such as Models) that are used by the action. E.g so I can call static method like User.get( id )
or User.getEmail( id )
without having to pass the db connection to each method separately.
I could set the db connection onto the ActionContext
from the interceptor, by doing:
ServletActionContext.getActionContext().put("db", db );
And then I could access this db connection from a static method, such as:
public class User implements Model
{
public static String getEmail(int id)
{
Connection db =
(Connection) ServletActionContext.getActionContext().get("db");
//...
}
}
My question is, would a new ActionContext
be generated for every given request, so I can be sure that a new db connection will be used each time? E.g if I have 500 people visiting mysite.com/fooAction
, could I be sure that each of those 500 requests is generating a unique ActionContext, and each call to User.getEmail()
would access only the db connection which is unique to the given request?
Thanks.
My question is, would a new ActionContext be generated for every given request, so I can be sure that a new db connection will be used each time?
Since ActionContext uses ThreadLocal it is thread safe. Struts 2 creates an ActionContext for each request, and each request has its own thread. So yes, if you create a new connection and store it in the ActionContext every thread will have its own connection. But I don't recommend you to store the connection in the ActionContext because this couple you to Struts 2 which is not a good thing, also your services shouldn't be calling web specific classes because it also couple them.
The ActionContext is the context in which an Action is executed. Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc.
The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. See the ActionContext.ActionContextThreadLocal class for more information. The benefit of this is you don't need to worry about a user specific action context, you just get it:
ActionContext context = ActionContext.getContext(); Finally, because of the thread local usage you don't need to worry about making your actions thread safe.
ActionContext excerpt:
public class ActionContext implements Serializable {
static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
....
}