What is the best way to duplicate / extend functions of a static class?

In the application I'm working in, there is one class that supports connecting to the database. All members of this class are static to provide a singleton pattern, so the actual connection logic is executed in a static initialization block:

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        sessionFactory = new Configuration().configure().buildSessionFactory();
    }

    static void openSession() {
        //open session with sessionFactory
    }

    public static Session currentSession() {
        //return the currently open session
    }

    static void closeSession() {
        //close the currently open session
    }

}

However, the application should now open a database connection to the second database. Now that this class is structured, the only way to save the second connection by saving the above template is to create a second class (something like SecondHibernateUtil) and change one configuration line in the initializer block. It looks like a really wasteful amount of copies / paste.

- , , , , ?

+5
9

.

.

, , .

+2

, .

+2

, .

, .

.

.

. ( )

  • , . . .
  • .
  • (HibernateUtility2) .

    // Create a new utility class using a map instead.
    class HibernateUtility { 
    
        private Map<String, SessionFactory> factories;
        private HibernateUtility instance;
    
        public  static HibernateUtility getInstance() { 
            if( instance == null ) { 
            instance = new HibernateUtility();
            }
            return instance;
        }
    
        private HibernateUtility() {}
    
        private void check( String whichConnection ) { 
            if( !factories.containsKey( whichConnection ) ) {
                // start the connection
                factories.put( whichConnection, new Configu..
                //... etc etc ().configure().buildSessionFactory() );
            }
        }
    
        void openSession( String whichConnection ) {
            check( whichConnection );
            //open session with sessionFactory
            factories.get( whichConnection ).open(); //etcetc
        }
    
        Session currentSession( whichConnection ) {
            check( whichConnection );
            //return the currently open session
            factories.get( whichConnection ) .....
        }
    
        void closeSession( String whichConnection ) {
            check( whichConnection );
            //close the currently open session
            factories.get( whichConnection );
        }       
    }
    
    //------------------------------------------
    // Make your existing class call that class
    public class HibernateUtil {
    
        static void openSession() {
            //open session with sessionFactory
            HibernateUtility.getInstance( "one" ).openSession();
        }
    
        public static Session currentSession() {
            //return the currently open session
            HibernateUtility.getInstance( "one" ).currentSession();
        }
    
        static void closeSession() {
            //close the currently open session
            HibernateUtility.getInstance( "one" ).closeSession();
        }
    }
    
    //------------------------------------------            
    // Make the new connection use that class too.
    public class HibernateUtil {
    
        static void openSession() {
            //open session with sessionFactory
            HibernateUtility.getInstance( "two" ).openSession();
        }
    
        public static Session currentSession() {
            //return the currently open session
            HibernateUtility.getInstance( "two" ).currentSession();
        }
    
        static void closeSession() {
            //close the currently open session
            HibernateUtility.getInstance( "two" ).closeSession();
        }
    }
    

, .

. .

, HibernateUtility, , openSession closeSession t .

, .

currentSession, , , ( "" "" )

, .

. - SO, , , , :(

+1

-, - , , , . 2 ...

( ), .

-, (, , , , , !)

, factory ( - spring), - factory, , - - .

, factory. , spring - , .

+1

, , .

, , , , , ?

, - , spring SessionFactory , Hibernate "Hello World", , .

0

Spring. Hibernate, Spring . Spring - , -.

Spring JDBC, Hibernate, iBatis , Spring. Spring, , . .

, , , XA , ?

0

; .

, . , , , , (SessionFactory) . , SessionFactory, ( , ). HibernateUtil , , . HibernateUtil.

- HibernateUtil2, HibernateUtil. , - , .

, , .

0

2- , 2- SessionFactory , "currentSession", String / .

0

, SessionFactory , hibernate.cfg.xml! SessionFactory .

0
source

All Articles