Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Mahesh

Thread: Hibernate with multiple databases

  1. #1
    dsr0781 is offline Member
    Join Date
    Jun 2007
    Posts
    2
    Rep Power
    0

    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

  2. #2
    JavaForums's Avatar
    JavaForums is offline Administrator
    Join Date
    Dec 2006
    Posts
    854
    Rep Power
    10

    Default

    Check the thread below. It may solve your problem.

    http://www.java-forums.org/database/...-database.html

  3. #3
    dsr0781 is offline Member
    Join Date
    Jun 2007
    Posts
    2
    Rep Power
    0

    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

  4. #4
    chengcsw is offline Member
    Join Date
    Jul 2007
    Posts
    1
    Rep Power
    0

    Default try this

    Java 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

  5. #5
    jackchen is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    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();
    }
    }

  6. #6
    Mahesh is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    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
    BarathVenki92 likes this.

Similar Threads

  1. Hibernate with multiple database
    By Marty in forum JDBC
    Replies: 3
    Last Post: 12-23-2008, 12:57 PM
  2. how to use XML databases
    By paty in forum XML
    Replies: 3
    Last Post: 08-16-2008, 12:11 AM
  3. Recommendation about databases
    By tommy in forum JDBC
    Replies: 2
    Last Post: 07-28-2007, 05:04 AM
  4. multiple databases
    By varunthecool in forum JDBC
    Replies: 2
    Last Post: 07-09-2007, 08:06 AM
  5. Replies: 2
    Last Post: 06-30-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •