Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There is this main class

and there are several services ( which uses db connection to retrieve data )

These services are initialized in the main class db properties are obtained from the property file and then

dbconnection is opened by calling a method dbOpen() written in the main class and the resultant connection object is set to the service objects by iterating through the list of services and by calling setConnection method on the service

note: that the services are instantiated in the main class and the main class is not a superclass for services.

I also need to mention that there is this recycle db connection scenario only main class is aware of.

/** connects to DB, optionally recycling existing connection), 
     * throws RuntimeException if unable to connect */ 
    private void connectDb(boolean recycle) {
        try {
            if (recycle) {
                log.status( log.getSB().append("Recycling DB Connection") );
            for ( int i = 0 ; i < service.length ; i++ )

One of the service needs to use a different database, what is the best design pattern to use?

share|improve this question
up vote 0 down vote accepted

What if the service classes had some kind of attribute indicating the type of connection they needed, and the main class examined that attribute and passed the requested connection to the service?

share|improve this answer
yes, that's exactly what I was thinking. – user49282 Mar 7 '12 at 17:38

I would recommend that you create service objects after you have established connection to the database and provide your db connection object as a parameter to a service constructor.
And when you call connectDb(true) in order to re-cycle connection, you do something as the following:

private void connectDb(boolean recycle) {
   if (recycle) {
       // move this loop to closeDb() method:
       for (Service i : services) {
          i.closeConnection() // guarantee that all transactions are committed, etc.
   // move the following loop to openDb() method 
   // or even better create ServiceFactory class that obtain appropriate connection from the main class before creating a service.
   for ( int i = 0 ; i < services.length ; i++ ) {
      // here you create your service classes as you have created them before:
      services[i] = new Service(db); 
      // the old service instances will be destroyed by system garbage collector...

This approach is cleaner and will work well (with minor adaptations) if one day your application become multithreading and the services will run in parallel.

share|improve this answer
Our application is single-threaded, and it will never change. I forgot to mention that in my question. – user49282 Mar 9 '12 at 15:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.