• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store the database connection as an attribute?

 
muksh sharma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I need to sore the Database connection as an attribute of the Context Listener. So that my whole application can access it.


Can anybody tell me how I can achieve this task?

 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this by load on startup servlet.
But are you want to store only one connection in context and all client will use it there will be synchronisation issues definetaly.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shailesh Narkhede wrote: there will be synchronisation issues definetaly.


How? As far as i know Connection is Common for all user.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this

Hope this helps
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what is happen when two client want to use connection,
both of them get it from context, then calling database simultaneously.
At this time it will work ??

If yes then what is need of connection pooling concept.
there we are getting connection from pool after using it will return to connection pool.

Please somebody clarify this.

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best thing about that example is that it uses a ServletContextListener instead of a load-on-startup servlet; the latter should not be used any more these days - it's an obsolete concept.

But the example completely disregards the multithreaded nature of web apps, making it a bad example indeed. If two users access the web app simultaneously, and both end up using the single DB connection, there WILL be problems.

The solution is to either a) synchronize access to the connection (thereby destroying parallel access - a bad solution), or b) to use a DB connection pool. DB pools are so easy to use that there's really no reason not to use one.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic