Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

best way to handle database connection  RSS feed

 
Kay Tracid
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • 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
 
Scott Duncan
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • 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.
 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thx i will follow your hint

no, I dont/cant use a datasource.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • 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 ]
 
R K Parulekar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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..
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!