Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-18-2007, 08:12 AM
Member
 
Join Date: Jun 2007
Posts: 2
Rep Power: 0
dsr0781 is on a distinguished road
Smile 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
  #2 (permalink)  
Old 06-18-2007, 08:23 AM
Administrator
 
Join Date: Dec 2006
Posts: 650
Rep Power: 10
JavaForums has disabled reputation
Default
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, 10:41 AM
Member
 
Join Date: Jun 2007
Posts: 2
Rep Power: 0
dsr0781 is on a distinguished road
Question 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, 07:29 AM
Member
 
Join Date: Jul 2007
Posts: 1
Rep Power: 0
chengcsw is on a distinguished road
Default 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 07:43 AM. Reason: Code placed inside [code] tag
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-22-2008, 10:06 AM
Member
 
Join Date: May 2008
Posts: 1
Rep Power: 0
jackchen is on a distinguished road
Default
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, 01:06 PM
Member
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
Mahesh is on a distinguished road
Default 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
Reply

Bookmarks

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

BB 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
Hibernate with multiple database Marty Database 3 12-23-2008 12:57 PM
how to use XML databases paty XML 3 08-16-2008 12:11 AM
Recommendation about databases tommy Database 2 07-28-2007 05:04 AM
multiple databases varunthecool Database 2 07-09-2007 08:06 AM
org.hibernate.ejb.Version <clinit> INFO: Hibernate EntityManager 3.2.0.CR1 Heather Database 2 06-30-2007 03:01 PM


All times are GMT +2. The time now is 09:21 PM.



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