本文介绍了Struts 2中的ActionContext是当前请求唯一的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个自定义拦截器,该拦截器会创建一个新的数据库连接,并在执行操作之前将该连接设置为当前操作.之后,拦截器将关闭连接.

我正在寻找与该操作使用的其他类/静态方法(例如Models)共享此db连接的便捷方法.例如,这样我可以调用User.get( id )User.getEmail( id )之类的静态方法,而不必将db连接分别传递给每个方法.

我可以通过以下操作将db连接设置为从拦截器到ActionContext:

ServletActionContext.getActionContext().put("db", db );

然后我可以从静态方法访问此数据库连接,例如:

public class User implements Model
{ 
   public static String getEmail(int id)
   {
      Connection db = 
         (Connection) ServletActionContext.getActionContext().get("db");
      //...
   }
}

我的问题是,是否会为每个给定的请求生成一个新的ActionContext,所以我可以确定每次都会使用一个新的数据库连接?例如,如果我有500个人访问mysite.com/fooAction,是否可以确定这500个请求中的每个请求都生成一个唯一的ActionContext,并且对User.getEmail()的每个调用将仅访问给定请求所独有的db连接? >

谢谢.

解决方案

由于ActionContext使用ThreadLocal,所以它是线程安全的. Struts 2为每个请求创建一个ActionContext,每个请求都有自己的线程.因此,是的,如果您创建一个新的连接并将其存储在ActionContext中,则每个线程都将拥有自己的连接.但是我不建议您将连接存储在ActionContext中,因为这会使您与Struts 2耦合,这不是一件好事,而且您的服务也不应调用特定于Web的类,因为它也会将它们耦合.

摘自Struts 2 Javadoc :

ActionContext 摘录:

public class ActionContext implements Serializable {

    static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
    ....
}

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.

解决方案

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.

From Struts 2 Javadoc:

ActionContext excerpt:

public class ActionContext implements Serializable {

    static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
    ....
}

这篇关于Struts 2中的ActionContext是当前请求唯一的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 11:06