Main class passes dbConn obj to all its services, I need to change the dbConn for one of its services. - suggestion for design pattern

Posted by tech_learner on Programmers See other posts from Programmers or by tech_learner
Published on 2012-03-07T16:56:06Z Indexed on 2012/03/30 11:41 UTC
Read the original article Hit count: 187

Filed under:
|

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?

© Programmers or respective owner

Related posts about design-patterns

Related posts about database