• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting JDBC Connection object in the HttpSession?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Phew. It's rare I get one up on Bear, but I'm bookmarking this one!
 
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic