• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting JDBC Connection object in the HttpSession?  RSS feed

 
Venkat Babu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to use the jdbc Connection object in several places in my web application.

Option 1: Create a new Connection object every time I read/write to table and close Connection object when read/write is done.

Option 2: Create a new Connection object and store it in HttpSession scope and use it from the session any time I need the Connection object.

Please let me know which of the above option is best or if you have any other suggestions. Please advise.

Thanks,
Venkat
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 3: None of the above.

Placing a Connection on the session is a bad idea, we have discussed it a few times and I recommend searching this forum for other discussions.

Please look at plugging in a Connection Pool, it is a much much better practice.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66156
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 3: Use a connection pool. Containers like Tomcat provide one for you.

P.S. Option 2 is a terrible horrible idea.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66156
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thunder stolen by a nose!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phew. It's rare I get one up on Bear, but I'm bookmarking this one!
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do some thing like this,
Create a thread local connection, let your filter create the connection(or get one from the pool) and attach it with current request thread, Create a JdbcUtil which will return a connection attached with current thread upon calling getConnection().
Filter will close (may be return to pool) the connection. This way you will have one connection per request.

[ January 06, 2008: Message edited by: sudhir nim ]
[ January 06, 2008: Message edited by: sudhir nim ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66156
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating connections in the control layer is not a best practice. The connections should be handled in the model layer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!