helpermethod
helpermethod

Reputation: 62304

Servlets - Sharing Initialcontext among several Servlets

I have my application logic divided into 3 servlets, two of them connecting to a database.

Both of them get database connections by:

Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/AutoMedScan");

There problem here is that with each request, an new Context gets created. I could store the context in a static final variable but then I have one in each servlet (and so, code duplication).

What's the best way to handle this problem?

Upvotes: 1

Views: 248

Answers (1)

duffymo
duffymo

Reputation: 309028

I don't think it's that big a problem.

But if you must do something about it, perhaps a ServletContextListener that creates the context and stores it at application scope is the answer.

Upvotes: 2

Related Questions