• 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

Returning DB connection

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

I am using below method to create ad database connection where con is declared as class variable : Connection con;


Now i call this method whenever i need a db connection :

like :



And then use the con object to run queries & then close the con like:



I do this many a times in my application whenever i need to connect to db.

I need to know if if this is the right way to use the connection object.
Am I leading my application to
too many connections exception.

Thanks
Robin
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It actually depends on how you intend to use the connection.

As a simple scenario, let us say you execute some queries (select/update) but only one at a time. Creating connection each time is not the right way and you can use the same connection.

You want to execute many queries simultaneously, then you can still reuse the connection provided you are doing only select queries.

Finally if the requirement is like multiple queries simultaneously, both selects and updates - yes this requires for separate connections. In this situation also you would be better off by using some kind of pooling.
[ October 17, 2008: Message edited by: Phani Raju ]
 
robin singal
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raju

I am using the last scenario you mentioned. I'll look at the pooling topic and try to come up with a better logic.

Thanks
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds a good idea; maybe better to have a close() method in your own class so you call close() rather than con.close(); this enables you to put your null checking and exception handling all in one place.

Sounds like a candidate for transfer to JDBC where this sort of topic is usually discussed.
reply
    Bookmark Topic Watch Topic
  • New Topic