• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

JDBC connection best practice

Hi,

My project is a desktop application that use a RDBMS. I use only one JDBC connection though out the app rumtime. But i think it's not the best solution.

 Please tell me what is the best practice ways to use with JDBC connection.

Thanks you.
0
pdidominic
Asked:
pdidominic
  • 3
  • 3
  • 3
  • +2
4 Solutions
 
ksivananthCommented:
if you application is single monolithic app, single conn would serve the purpose. if yours is multi threaded, go for conn pooling!
0
 
ksivananthCommented:
one such conn pool implementation is DBCP and widely used, http://commons.apache.org/dbcp/
0
 
objectsCommented:
if you only need a single connection, then just use a single connection. don't overcomplicate things unnecesarily

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CEHJCommented:
The number of connections is closely connected to threading issues and isn't simply an issue of 'only needing one connection'. If you only ever used one connection, that would suggest it's an instance variable. This means you would never be able to implement multiple threading as your application couldn't be thread-safe without synchronization

To make it thread-safe without pooling you would have to use local variables and open and close connections each time (expensive) or you're into coding for synchronization, and you might as well use a thread pool

That's why k's answer is the correct one - and the one i've already given you elsewhere ;-)
0
 
iboCommented:
if your app has a potential to grow bigger in the future and involves many database operations, dont use straight jdbc. use Spring's JdbcTemplate or  ibatis. take a peek at http://static.springframework.org/spring/docs/2.0.x/reference/jdbc.html. otherwise, keep it as it is.
0
 
objectsCommented:
Suggestions that everyone should always use a connection pool are a bit strange, and I hope they aren't confusing you.
What problems are you having with your current solution?
0
 
CEHJCommented:
>>What problems are you having with your current solution?

The question is not about problems, it's about best practice. Best practice includes making sure your app is future-proof and scalable, to which matters ibo added a useful contribution. If your code is quick and fairly dirty, there's absolutely nothing wrong with keeping one connection open and reusing it
0
 
pdidominicAuthor Commented:
I just want to know the others solutions to deal with JDBC connect, their pros,cons and when to use it.
0
 
pdidominicAuthor Commented:
@CEHJ, ksivananth,objects: Thank you, I'll show my favor in point ;-)

PS: it should be great if EE has emoicon :-)
0
 
objectsCommented:
In say a full blown web application you wouldn't even deal with connections at all, they would be managed for you.

But how you manage depends on the application, if your application is simple you don't need a full blown hibernate backend, just as you don't need to add a connection pool to every application you write just case you can (or don't know better). If you find your application is in need of a connection pool to manage a number of dynamic connections to the database then certainly use one, but not if there is no need.

0
 
pdidominicAuthor Commented:
Thank you all!
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now