Hi,
Here is the scenario:
There is application running and it uses Hibernate for persistency. The application is connected to Database 1. I’m trying to add a new flow to that application. The front end will be connected to a different Database (i.e. Database 2). The existing code (for flow 1) is inaccessible and I cannot modify the existing cfg file.
I have created a new Hibernate configuration file called (hibernate2.cfg.xml) and a “IniSessionFactory” class given below. I also generated hbm and ORM files for my application.
The problem is that my “initSessionFactory” method is never accessed. That is, I guess, the application creates a session to the other Database (Database 1) and then tries to access the tables I specified in my “cfg” file through that session and obviously, the result set is always “0”.
Anyone knows how to handle this issue? Your help is greatly appreciated.
Thank you!
************************************************
public class InitSessionFactory {
private static final Log log = LogFactory.getLog(InitSessionFactory.class);
/**
* Default constructor.
*/
private InitSessionFactory() {
}
private static String CONFIG_FILE_LOCATION = "/hibernate2.cfg.xml";
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static org.hibernate.SessionFactory sessionFactory;
/**
* initialises the configuration if not yet done and returns the current
* instance
*
* @return
*/
public static SessionFactory getInstance() {
if (sessionFactory == null)
initSessionFactory();
return sessionFactory;
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public Session openSession() {
return sessionFactory.getCurrentSession();
}
/**
* The behaviour of this method depends on the session context you have
* configured. This factory is intended to be used with a hibernate.cfg.xml
* including the following property <property
* name="current_session_context_class">thread</property> This would return
* the current open session or if this does not exist, will create a new
* session
*
* @return
*/
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
/**
* initializes the sessionfactory in a safe way even if more than one thread
* tries to build a sessionFactory
*/
private static synchronized void initSessionFactory() {
/*
* [laliluna] check again for null because sessionFactory may have been
* initialized between the last check and now
*
*/
Logger log = Logger.getLogger(InitSessionFactory.class);
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
String sessionFactoryJndiName = cfg
.getProperty(Environment.SESSION_FACTORY_NAME);
if (sessionFactoryJndiName != null) {
cfg.buildSessionFactory();
log.debug("get a jndi session factory");
sessionFactory = (SessionFactory) (new InitialContext())
.lookup(sessionFactoryJndiName);
} else{
log.debug("classic factory");
sessionFactory = cfg.buildSessionFactory();
}
} catch (Exception e) {
log.error(" Error Creating HibernateSessionFactory: ");
throw new HibernateException(
"Could not initialize the Hibernate configuration");
}
}
}
public static void close(){
if (sessionFactory != null)
sessionFactory.close();
sessionFactory = null;
}
static {
try {
log.debug("In HibernateUtil try-clause");
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
log.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Thanks!
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html