• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing variables

 
lokesh reddy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have initialized a database connection in one jsp and i would like to use the same connection object in the subsequent jsps, with out initializing another connection, such that i can optimize my server resources.
I want to know how to do this.
Bye.
Loke.
 
Ravee Bhat
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about a Bean?
ravee.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lokesh reddy:
I have initialized a database connection in one jsp and i would like to use the same connection object in the subsequent jsps, with out initializing another connection, such that i can optimize my server resources.

This is quite commonly done and called a "connection pool". You can find one in this JavaWorld article about object pooling.
- Peter
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravee Bhat:
How about a Bean?

One thing to watch out for is that servlets and JSPs are multi-threaded, unless you implement SingleThreadModel (avoid if you can).
Store a connection in a bean and there will be a time when two threads will be fighting over that connection. So you'll have to make sure that connection is properly checked out and checked back in again, etc, and before you know it you're coding a connection pool.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic