-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Accessing 2 databases through 2 cfg files
PostPosted: Mon Jan 05, 2009 3:12 pm 
Newbie

Joined: Mon Jan 05, 2009 2:47 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 12:34 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
I would check if the getInstance method is getting called. If it is, because the sessionFactory is null, initSessionFactory should get called.

btw, I would also rename the (initSessionFactory) method or the class to avoid confusion.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.