File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Single Database connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Single Database connection" Watch "Single Database connection" New topic

Single Database connection

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
I would like to create a single Database connection point that I can use for 4 classes in my Java Web Application.

Here is my ConnectionManager Class:

Now how would I access this in each one of my classes?

For example here is one:

Another Class looks like this:

Please advise.
[ October 17, 2007: Message edited by: Dan Parsons ]
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

You don't need to pass connection from one class to another,
Instead whenever you need a connection in any of your class just call
ConnectionManager.getConnection() and it will return the active connection.

IF classes are going to be used by more than one threds, make the threadlocal connection inside ConnectionManager.

[Servlet tutorial] [Servlet 3.0 Cook Book]
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
You don't need to pass connection from one class to another

If your application need a transaction among the classes, you need to pass the connection through and set autocommit to false.
I agree. Here's the link:
subject: Single Database connection
jQuery in Action, 3rd edition