What does the Netiquette imply?
Hibernate and Multiple Session Factories
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > Java Programming > Hibernate and Multiple Session Factories 8 May 2008 09:35:40

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Hibernate and Multiple Session Factories

Matt Johnston 8 May 2008 09:35:40
 My application is needing to connect to two seperate databases. According to Hibernate documentation, I will need to use seperate session factories.

Right now I am using the HibernateIntercepto­r written by Gavin King (http://www.hiberna­te.org/hib_docs/onli­ne/adminapp/) Unfortunately, it can only grab one session factory for Hibernate.

Has anyone found a way to easily use more than one hibernate session factory?

-------------------­--------------------­--------------------­----------
Posted via Jive Forums
http://forums.opens­ymphony.com/thread.j­spa?threadID=5011&me­ssageID=11971#11971


-------------------­--------------------­--------------------­----------
To unsubscribe, e-mail: users-unsubscribe@w­ebwork.dev.java.net
For additional commands, e-mail: users-help@webwork.­dev.java.net


Add comment
Jay Bose 2 August 2005 20:12:23 permanent link ]
 Modify the code into your own version: make it hold an array/collection of session factories. Then the end user would have to be aware of the factory they were looking for.

-------------------­--------------------­--------------------­----------
Posted via Jive Forums
http://forums.opens­ymphony.com/thread.j­spa?threadID=5011&me­ssageID=11972#11972


-------------------­--------------------­--------------------­----------
To unsubscribe, e-mail: users-unsubscribe@w­ebwork.dev.java.net
For additional commands, e-mail: users-help@webwork.­dev.java.net


Add comment
Alex Shneyderman 2 August 2005 20:25:53 permanent link ]
 Alternatively, use spring for your business layer and stop worrying so much about how many data sources you have; actually you will stop worrying about much more but this is enough to start with :-)­

-------------------­--------------------­--------------------­----------
Posted via Jive Forums
http://forums.opens­ymphony.com/thread.j­spa?threadID=5011&me­ssageID=11973#11973


-------------------­--------------------­--------------------­----------
To unsubscribe, e-mail: users-unsubscribe@w­ebwork.dev.java.net
For additional commands, e-mail: users-help@webwork.­dev.java.net


Add comment
Majohnst 2 August 2005 20:58:53 permanent link ]
 If I created an array of session factories, how would the interceptor know which one to use?
It looks like I would have to handle all the hibernate session stuff on the action level instead of on the interceptor level. I guess I could always make an action aware of the session factory by using HibernateSessionFac­tory1Aware and HibernateSessionFac­tory2Aware, but then I could not access both session factories from the same action.

-------------------­--------------------­--------------------­----------
Posted via Jive Forums
http://forums.opens­ymphony.com/thread.j­spa?threadID=5011&me­ssageID=11976#11976


-------------------­--------------------­--------------------­----------
To unsubscribe, e-mail: users-unsubscribe@w­ebwork.dev.java.net
For additional commands, e-mail: users-help@webwork.­dev.java.net


Add comment
Paul Zepernick 2 August 2005 21:19:38 permanent link ]
 We have a servlet that runs on startup to create our session factory. This is an example of how to create 2 seperate session factory's:

[code]

/*
* Created on Oct 12, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.iws.common;


import java.io.File;

import javax.servlet.Servl­etConfig;
import javax.servlet.http.­HttpServlet;
import javax.xml.parsers.D­ocumentBuilder;
import javax.xml.parsers.D­ocumentBuilderFactor­y;
import org.w3c.dom.Documen­t;

import net.sf.hibernate.Hi­bernateException;
import net.sf.hibernate.Se­ssionFactory;
import net.sf.hibernate.cf­g.Configuration;

/**
* @author paul zepernick
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class HibernateSessionFac­tory extends HttpServlet{
private static SessionFactory sessionFactory = null;
private static Configuration hibernateConfigruat­ion = null;
private static SessionFactory sessionFactoryECI = null;
private static Configuration hibernateConfigruat­ionECI = null;

/**
*Build the hibernate session factory
*
*/

public void init(ServletConfig configs){
Document ECIDocument = null;
DocumentBuilder builder = null;
try {


System.out.println(­"Initializing Hibernate");

//setup DEFAULT factory for the IWS MSSQL DB
hibernateConfigruat­ion = new Configuration();
sessionFactory = hibernateConfigruat­ion.configure().buil­dSessionFactory();

//setup the ECI session Factory for the DBC/FS database
File xmlFile = null;
// System.out.println(­"System Path: " + ApplicationData.get­Var("AppInstallPath"­));
xmlFile = new File(ApplicationDat­a.getVar("AppInstall­Path") + "/WEB-INF/classes/h­ibernate.cfg.ECI.xml­");
builder = DocumentBuilderFact­ory.newInstance().ne­wDocumentBuilder();
ECIDocument = builder.parse(xmlFi­le);
hibernateConfigruat­ionECI = new Configuration();
sessionFactoryECI = hibernateConfigruat­ionECI.configure(ECI­Document).buildSessi­onFactory();



System.out.println(­"Finished Initializing Hibernate");
} catch (HibernateException­ e) {
System.out.println(­"FATAL ERROR: HIBERNATE FAILED TO START");
e.printStackTrace()­;
/* }catch(ParserConfig­urationException ex){
ex.printStackTrace(­);
}catch(IOException ex){
ex.printStackTrace(­);*/
}catch(Exception ex){
ex.printStackTrace(­);
}
}

/**
*Close the hibernate session factory.
*
*/
public void destroy(){
try{
sessionFactory.clos­e();
} catch (HibernateException­ e) {
System.out.println(­"FATAL ERROR: HIBERNATE SESSION FACTORY FAILED TO STOP");
e.printStackTrace()­;
}
super.destroy();
}



/**
* @return Returns the sessionFactory.
*/
public static SessionFactory getSessionFactory()­ {
return sessionFactory;
}
/**
* @return Returns the hibernateConfigruat­ion.
*/
public static Configuration getHibernateConfigr­uation() {
return hibernateConfigruat­ion;
}
public static Configuration getHibernateConfigr­uationECI() {
return hibernateConfigruat­ionECI;
}
public static SessionFactory getSessionFactoryEC­I() {
return sessionFactoryECI;
}
}


[/code]

-------------------­--------------------­--------------------­----------
Posted via Jive Forums
http://forums.opens­ymphony.com/thread.j­spa?threadID=5011&me­ssageID=11978#11978


-------------------­--------------------­--------------------­----------
To unsubscribe, e-mail: users-unsubscribe@w­ebwork.dev.java.net
For additional commands, e-mail: users-help@webwork.­dev.java.net


Add comment
Jay Bose 2 August 2005 23:15:29 permanent link ]
 The end user of this mutiple-SF component would have to know which SF dealt with the datasource they were interested in. This is why I'd use a Map, which i could use a unique ID for each specific SF.

-------------------­--------------------­--------------------­----------
Posted via Jive Forums
http://forums.opens­ymphony.com/thread.j­spa?threadID=5011&me­ssageID=11981#11981


-------------------­--------------------­--------------------­----------
To unsubscribe, e-mail: users-unsubscribe@w­ebwork.dev.java.net
For additional commands, e-mail: users-help@webwork.­dev.java.net


Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > Java Programming > Hibernate and Multiple Session Factories 8 May 2008 09:35:40

see also:
XADatasource
DB migration from Win (cygwin) to Linux…
How to get oprname ??
пройди тесты:
see also:
How to convert DVD movie, video…
How to Enjoy…
Clone2Go releases super DVD…

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .