简体   繁体   English

如何在Tomcat的多个请求中不保留Static变量?

[英]How to not keep Static variable across multiple request with Tomcat?

According to this post Tomcat: Java Static Variable Scope, Application or session wide? 根据这篇文章Tomcat:Java静态变量范围,应用程序还是会话范围? Tomcat retains static variable across request, how can it be configured to not keep static variable per request? Tomcat在整个请求中保留静态变量,如何配置为不为每个请求保留静态变量?

No it can't. 不,它不能。 It is not practical. 这是不实际的。

The Java Language Specification (JLS) mandates that there is one and only instance of a classes static variables in the entire execution context (ie the JVM). Java语言规范(JLS) 要求在整个执行上下文(即JVM)中只有一个类静态变量的实例。

The only possible "get out" is that it is technically possible to have multiple copies of the same class, each in different classloaders. 唯一可能的 “退出”是,从技术上讲,可以在同一个类中拥有多个副本,每个副本都位于不同的类加载器中。 However, creating a new classloader and loading a new copy of a class for each request would be horribly inefficient, and totally impractical. 但是,为每个请求创建一个新的类加载器并加载一个新的类副本将非常低效,而且完全不切实际。

The solution is simple: 解决方案很简单:

Don't use statics in your request processing classes. 不要在请求处理类中使用静态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM