Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat: Java Static Variable Scope, Application or session wide?

Are java static variables shared across all sessions using the same webapp or does each session gets its own version of the static variables?

To put it another way, do Tomcat created a new set of classes for each session or just one set for the whole web app?

like image 491
NicXen Avatar asked Jul 09 '13 17:07

NicXen


2 Answers

Tomcat creates one ClassLoader for each web application, i.e. war-File or context. So every Class is loaded once for the web application. Therefore static variables are shared across multiple sessions and requests.

References:

  • Apache Tomcat 7 Classloader Howto
like image 163
nif Avatar answered Oct 18 '22 22:10

nif


Static variables are shared across the sessions. Be careful of using static variables.

like image 37
Heejin Avatar answered Oct 18 '22 21:10

Heejin