• 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

best way to handle database connection

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a simple question about the db-connection. What is the best way to handle the connection in my jsp? Now I have my own class with a static method for returning the connection.
like:
public static Connection getConnection()

What happens if there are more than one call/statements at one time? Is that a "good" way to handle it? What may be better?

Thx a lot
 
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One statement object per Connection object. Make sure they both get closed in a finally block when you are finished with them.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you use connection pool provided by your container with the help of DataSource?
 
Kay Tracid
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thx i will follow your hint

no, I dont/cant use a datasource.
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Duncan:
One statement object per Connection object.



It is quite common to have multiple Statement/PreparedStatement (s) from a single Connection.

Is there a specific reason for not having more than one Statement from one Connection?
[ May 27, 2005: Message edited by: Sonny Gill ]
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better to have a connection pool and get the new connection .. if not, then
please do not have getConnection as a static method..instead, get a new con and close the same once db operation is performed ..

Ravi..
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting confused between a static method, which is just a class method, and a static Connection variable. A static method is fine, I believe, but the Connection variable should always be local.

Incidentally, it's a good idea that the DataSource variable should be static.
reply
    Bookmark Topic Watch Topic
  • New Topic