Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Hibernate using multiple datasource/database

I'm working on a web application that uses Spring MVC 3 and Hibernate

I want to use 2 datasource MySql and Oracle databases for my web application,

I've been read many tutorial and problem solving for "spring-hibernate multiple datasource/database" for example :

directjump2java.blogspot.com

stackoverflow

forum spring

and etc.

but when every single time I run it, the config just read my first database config (MySql) and show this error Table 'db_prod.ksei_lookup_holiday' doesn't exist db.prod is my first database(MySql) and KSEI_LOOKUP_HOLIDAY is my second database (Oracle),

this is my spring.xml

<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:annotation-driven transaction-manager="transactionManagerSOAAPP"/>
<context:annotation-config />


<context:component-scan base-package="prod.support" />

<!-- Database MySql, Desktop -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/db_prod" />
    <property name="username" value="root" />
    <property name="password" value="shikamaru" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="prod.support.model.splatter" />
</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactory">
</bean>

<!-- Database Oracle, Schema : SOAAPP -->

<bean id="dataSourceSOAAPP" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE" />
    <property name="username" value="splatter" />
    <property name="password" value="shikamaru" />
</bean>

<bean id="sessionFactorySOAAPP"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"></property>
    <property name="dataSource" ref="dataSource"></property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
    <property name="packagesToScan" value="prod.support.model.soaapp" />
</bean>

<bean id="transactionManagerSOAAPP"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"
    p:sessionFactory-ref="sessionFactorySOAAPP">
</bean>

this is my DAO Implementation for my first database (MySql)

@Repository
@Qualifier(value="sessionFactory")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

@Autowired
private UserDaoImpl(SessionFactory sessionFactory){
    setSessionFactory(sessionFactory);
}

this is my DAO Implementation for my second database (Oracle)

@Repository
@Qualifier(value="sessionFactorySOAAPP")
public class UpdateKSEIDaoImpl extends HibernateDaoSupport implements UpdateKSEIDao{

@Autowired
private UpdateKSEIDaoImpl(SessionFactory sessionFactorySOAAPP){
    setSessionFactory(sessionFactorySOAAPP);
}

any help will be pleasure :)

like image 855
splatter_fadli Avatar asked Jun 04 '14 09:06

splatter_fadli


1 Answers

The problem is that you have used

<property name="dataSource" ref="dataSource"></property> in sessionFactorySOAAPP.

You should have used <property name="dataSource" ref="dataSourceSOAAPP"></property>

like image 70
geoand Avatar answered Oct 06 '22 14:10

geoand