1

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") );
                closeDb();
            }
            openDb();       
            for ( int i = 0 ; i < service.length ; i++ )
            {
                service[i].setConnection(db);
            }
        }

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

2 Answers 2

0

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?

1
  • yes, that's exactly what I was thinking.
    – user49282
    Commented Mar 7, 2012 at 17:38
0

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.
       }
       closeDb();
   }
   openDb();
   // 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.

1
  • Our application is single-threaded, and it will never change. I forgot to mention that in my question.
    – user49282
    Commented Mar 9, 2012 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.