Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-18-2007, 09:12 AM
Member
 
Join Date: Jun 2007
Posts: 2
dsr0781 is on a distinguished road
Hibernate with multiple databases
hello every one

I am very new to hibernate.I want to work with multiple databases.How it is possible?
I have read in the docs that i have to create multiple session factories .But i dont know how to do that?
Can anybody give me examples?

Thanks in Advance
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-18-2007, 09:23 AM
Administrator
 
Join Date: Dec 2006
Posts: 577
JavaForums has disabled reputation
Check the thread below. It may solve your problem.

Hibernate with multiple database
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-18-2007, 11:41 AM
Member
 
Join Date: Jun 2007
Posts: 2
dsr0781 is on a distinguished road
hi
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. Is it the wright way? Or is there any better way of doing this?


Thanks
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-12-2007, 08:29 AM
Member
 
Join Date: Jul 2007
Posts: 1
chengcsw is on a distinguished road
try this
Code:
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

Last edited by JavaBean : 07-12-2007 at 08:43 AM. Reason: Code placed inside [code] tag
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-22-2008, 11:06 AM
Member
 
Join Date: May 2008
Posts: 1
jackchen is on a distinguished road
there is a method called currentSession(String key) inside the class which need s method closeSession(String key). I try to add it.I don't know if it is right.please feel free to propose your suggestions.
-----codes as below

public static void closeSession(String key) {
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
if (sessionMaps != null) {
Session session = sessionMaps.get(key);
if (session != null && session.isOpen())
session.close();
}
}
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-23-2008, 02:06 PM
Member
 
Join Date: Jun 2008
Posts: 1
Mahesh is on a distinguished road
Accessing two schema from spring and hibernate
Hi,

I am new to hibernate and Spring
My application use spring , struts and hibernate mapping. Its cuurently connecting to a single database

The requirement is to create a new schema and to fetch the data from both the schemas ….. I need to take data from both the schemas…. Both the schemas are having the same set of tables (Table names are same across the schemas as the new one is the archival schema )

In my dataAccessContext-jta.xml , datasource is defined as follows….

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc racle:thin:@someserver</value>
</property>
<property name="username">
<value>usrname</value>
</property>
<property name="password">
<value>passwd/value>
</property>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg>
<ref bean="dataSource"/>
</constructor-arg>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean"> <property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>trial.hbm.xml</value> </list> </property> <property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.O racle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.region_prefix">hibernate.cach e</prop>
<prop key="hibernate.cache.provider_class"> net.sf.hibernate.cache.EhCacheProvider </prop> </props></property></bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate.Hibernate TransactionManager"> <property name="sessionFactory">
<ref local="sessionFactory"/>
</property> </bean>

My question is whether its possible to connect to two different schema s using hibernate … If so how can we go about it… Here we are usnig hibernate transaction manager .. Please explain in detail as I dont have any idea of how to go about it... Please help me in this…

Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use XML databases paty XML 3 08-16-2008 01:11 AM
Recommendation about databases tommy Database 2 07-28-2007 06:04 AM
multiple databases varunthecool Database 2 07-09-2007 09:06 AM
org.hibernate.ejb.Version <clinit> INFO: Hibernate EntityManager 3.2.0.CR1 Heather Database 2 06-30-2007 04:01 PM
Hibernate with multiple database Marty Database 2 05-07-2007 05:56 PM


All times are GMT +3. The time now is 09:08 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org