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() {
}
public static Session currentSession() {
}
static void closeSession() {
}
}
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.
- , , , , ?