Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

How to use 2 sessionFactory in one project

$
0
0
Hi
I need 2 connect to two database and read/write data to both.
For this issue i creates a jdbc.properties like this :
Code:

##################################################################

jdbc1.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc1.databaseurl=jdbc:jtds:sqlserver://oracle01/Sale;
jdbc1.username=sa
jdbc1.password=111111

jdbc1.dialect=org.hibernate.dialect.SQLServer2008Dialect
jdbc1.show_sql=true
jdbc1.hbm2ddl=validate

##################################################################

jdbc2.driverClassName=net.sourceforge.jtds.jdbc.Driver
jdbc2.databaseurl=jdbc:jtds:sqlserver://app01/DSDB_TEH;
jdbc2.username=VNRW
jdbc2.password=VN123

jdbc2.dialect=org.hibernate.dialect.SQLServer2008Dialect
jdbc2.show_sql=true
jdbc2.hbm2ddl=validate

##################################################################

In spring.xml I created bean like this :
Code:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--=======================================================================================================-->
    <tx:annotation-driven/>

    <context:component-scan base-package="org.sunich1"/>
    <context:component-scan base-package="org.sunich2"/>
    <!--=======================================================================================================-->

    <!--=======================================================================================================-->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="WEB-INF/jdbc.properties"/>
    <!--=======================================================================================================-->
    <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc1.driverClassName}"/>
        <property name="url" value="${jdbc1.databaseurl}"/>
        <property name="username" value="${jdbc1.username}"/>
        <property name="password" value="${jdbc1.password}"/>
    </bean>

    <bean id="sessionFactory1" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource1"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc1.dialect}</prop>
                <prop key="hibernate.show_sql">${jdbc1.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${jdbc1.hbm2ddl}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="org.sunich1.model"/>
    </bean>

    <bean id="transactionManager1" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory1"/>
    </bean>
    <!--=======================================================================================================-->
    <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc2.driverClassName}"/>
        <property name="url" value="${jdbc2.databaseurl}"/>
        <property name="username" value="${jdbc2.username}"/>
        <property name="password" value="${jdbc2.password}"/>
    </bean>

    <bean id="sessionFactory2" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource1"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc2.dialect}</prop>
                <prop key="hibernate.show_sql">${jdbc2.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${jdbc2.hbm2ddl}</prop>
            </props>
        </property>
        <property name="packagesToScan" value="org.sunich2.model"/>
    </bean>

    <bean id="transactionManager2" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory2"/>
    </bean>
    <!--=======================================================================================================-->
</beans>

if I want to implement DAO classes which uses database 1 , the code is like :
Code:

package org.sunich1.dao.impl;


import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.sunich1.dao.BankDAO;
import org.sunich1.model.Bank;

import java.util.List;

@Repository
public class BankDAOImpl implements BankDAO {

    @Autowired
    private SessionFactory sessionFactory1;

    @Override
    public void insertBank(Bank bank) {
        sessionFactory1.getCurrentSession().save(bank);
    }

    @Override
    public void updateBank(Bank bank) {
        sessionFactory1.getCurrentSession().update(bank);
    }

    @Override
    public Bank getBankById(long bankId) {
        return (Bank) sessionFactory1.getCurrentSession().get(Bank.class, bankId);
    }

    @Override
    public void deleteBank(long bankId) {
        Bank bank = new Bank();
        bank = getBankById(bankId);
        sessionFactory1.getCurrentSession().delete(bank);
    }

    @Override
    public Bank getBank(String bankname) {
        Query query = sessionFactory1.getCurrentSession().createQuery("from Bank where name = :bankname");
        query.setParameter("bankname", bankname);
        return (Bank) query.list().get(0);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Bank> getBanks() {
        Criteria criteria = sessionFactory1.getCurrentSession().createCriteria(Bank.class);
        return criteria.list();
    }

}

and if I want to Implement BO classes which uses database 1 , the code is like :
Code:

package org.sunich1.bo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.sunich1.bo.BankBO;
import org.sunich1.dao.BankDAO;
import org.sunich1.model.Bank;

import java.util.List;

@Service
public class BankBOImpl implements BankBO {

        @Autowired
        private BankDAO bankDAO;
       
        @Override
        @Transactional(value = "transactionManager1")
        public void insertBank(Bank bank) {
                bankDAO.insertBank(bank);
        }

    @Override
    @Transactional(value = "transactionManager1")
    public void updateBank(Bank bank) {
        bankDAO.updateBank(bank);
    }

    @Override
    @Transactional(value = "transactionManager1")
        public Bank getBankById(long bankId) {
                return bankDAO.getBankById(bankId);
        }

    @Override
    @Transactional(value = "transactionManager1")
    public void deleteBank(long bankId) {
        bankDAO.deleteBank(bankId);
    }

    @Override
    @Transactional(value = "transactionManager1")
        public Bank getBank(String bankname) {
                return bankDAO.getBank(bankname);
        }

        @Override
    @Transactional(value = "transactionManager1")
        public List<Bank> getBanks() {
                return bankDAO.getBanks();
        }

}

for using databse 2 ,
I changed the transactionManager1 to transactionManager2 in BO classes and I changed sessionFactory1 to sessionFactory2 in DAO classes.

but I can't use this and it gives me error. what is the best solution ?
Regards

Viewing all articles
Browse latest Browse all 297

Trending Articles