-->
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: Is it possible To connect same dataBase to connect twise.
PostPosted: Mon Apr 11, 2011 7:35 am 
Newbie

Joined: Wed Mar 23, 2011 2:47 am
Posts: 8
HI to all,

Thanks for reading.

I am new to hibernate. i am in situation to connect to the student database.

studentdatabase sche is same and the contents will me changed from one year to another year. i need to access student first year data and second year data.

is it possible ,please give reply


Top
 Profile  
 
 Post subject: Re: Is it possible To connect same dataBase to connect twise.
PostPosted: Mon Apr 11, 2011 8:04 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
i have worked with multiple databases using two hibernate.cfg.xml and writing two seperate methods to get the session factory object for the different database.

See Try this

public class HibernateUtil {

private static Log log = LogFactory.getLog(HibernateUtil.class);
private static HashMap<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>();

public static final ThreadLocal sessionMapsThreadLocal = new ThreadLocal();

public static Session currentSession(String key) throws HibernateException {

HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();

if (sessionMaps == null) {
sessionMaps = new HashMap();
sessionMapsThreadLocal.set(sessionMaps);
}

// Open a new Session, if this Thread has none yet
Session s = (Session) sessionMaps.get(key);
if (s == null) {
s = ((SessionFactory) sessionFactoryMap.get(key)).openSession();
sessionMaps.put(key, s);
}

return s;
}

public static Session currentSession() throws HibernateException {
return currentSession("");
}

public static void closeSessions() throws HibernateException {
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
sessionMapsThreadLocal.set(null);
if (sessionMaps != null) {
for (Session session : sessionMaps.values()) {
if (session.isOpen())
session.close();
};
}
}

public static void closeSession() {
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
sessionMapsThreadLocal.set(null);
if (sessionMaps != null) {
Session session = sessionMaps.get("");
if (session != null && session.isOpen())
session.close();
}
}

public static void buildSessionFactories(HashMap<String, String> configs) {
try {
// Create the SessionFactory
for (String key : configs.keySet()) {
URL url = HibernateUtil.class.getResource(configs.get(key));
SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory();
sessionFactoryMap.put(key, sessionFactory);
}

} catch (Exception ex) {
ex.printStackTrace(System.out);
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);

} // end of the try - catch block
}

public static void buildSessionFactory(String key, String path) {
try {
// Create the SessionFactory
URL url = HibernateUtil.class.getResource(path);
SessionFactory sessionFactory = new Configuration().configure(url).buildSessionFactory();
sessionFactoryMap.put(key, sessionFactory);

} catch (Throwable ex) {

log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);

} // end of the try - catch block
}

} // end of the class

_________________
Thanks
Niki


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.