Connection to the database and session
Friends I am developing a JSP based application ,and I am storing Connection (to the database) info in a session variable in my main menu , and then I retrieve the connection session in all the pages that I navigate to.
But a friend of mine told me it is not a good programming practice.He adviced me to use the connection pooling to make effecient use of the connection.
So using session for storing connection to the database is a bad idea ?
I thought it is bad idea to get connected to the database everytime a jsp is thrown ...hence I was using session variable.
Please give ur thought about this.
Thanks.
Well.
You are using a kind of connection pooling already, since you make a connection and reuse it along every jsp page in a session but reusability is at the cut&paste level here.
And what would you do if you needed to share a connection between sessions (scope:application)
Or how would you resolve the need to connect to 2 databases in one JSP page ? Duplicate the code from the "main menu" ?
If you have let's say 5 applications written as you described and the connection data (ip address, SID, host) changes as someone moves the database to another machine (for example :-))) you will have to manually correct all 5 applications. So it's not much of flexibility in this kind of code.
Connection pooling as I understood it (I might be wrong :-))) is placing the code in a bean or servlet and the parameters in a txt (or better xml) file so when something changes you don't rewrite applications, just rewrite one txt (xml) file and you are done.
If you use a connection pool as a bean you have the bean scopes you can choose from and so on.....
Visit
http://www.codestudio.com/index.shtml
for an example of a very well written connection pool. Maybe there you will find other issues to help you decide.
Hope this helps!
Bye