Mikey S.
Mikey S.

Reputation: 3331

Servlet Filters - Context Initialization

When using a "global" filter, one which is located in the root lib directory of an apache tomcat webserver and applies to all web application contexts: I was wondering if the web application contexts are initialized synchronously - one at a time, or can there be a case where the Filter.init() function is called by two different contexts in the same time.

I'm asking this since I want to initialize a database connection configuration object based on a specific FilterConfig.initParameter the first time init() is called by any of the application contexts, and I'm wondering if I should put the above in a synchronized block or not...

Upvotes: 2

Views: 605

Answers (1)

palacsint
palacsint

Reputation: 28885

I think it's the internals of Tomcat, it could change any time so I'd do it in a synchronized block. It's not a big deal or performance issue because it runs only once per application context initialization.

From Java Servlet Specification 6.2.1 Filter Lifecycle:

Only one instance per <filter> declaration in the deployment descriptor is instantiated per JVM of the container.

You could reach the same result with a custom Valve:

Upvotes: 1

Related Questions