File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to store the database connection as an attribute? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to store the database connection as an attribute?" Watch "How to store the database connection as an attribute?" New topic
Author

How to store the database connection as an attribute?

muksh sharma
Ranch Hand

Joined: Apr 29, 2009
Posts: 37
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?


[Java FAQs] [SCWCD Links] [SCBCD Links] [SCWCD Wall of Fame] [SCBCD Wall of Fame]
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
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.


Thanks,
Shailesh
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 28, 2008
Posts: 5575

Have a look at this

Hope this helps
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
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
Marshal

Joined: Mar 22, 2005
Posts: 41633
    
  55
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.


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Ulf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store the database connection as an attribute?