problem with using two datasources with hibernate?

2019-08-18 03:10发布

问题:

greeting all I tried to use two different datasources with hibernate (two different sessionFactories and two different transactionManagers ) but I got the application failed to start

it stops in the step of

Creating new JDBC DriverManager Connection to [jdbc:mysql://some-ip-here/mydb]

here's the configuration:

<?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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <context:component-scan base-package="com.myapp" />
    <context:annotation-config />
    <tx:annotation-driven transaction-manager="transactionManager" />


    <!-- ================PostgreSQL/Hibernate================ -->

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.myapp.domain.myDomain1</value>
            </list>

        </property>

        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
            </value>
        </property>


    </bean>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.postgresql.Driver" />

        <property name="url">
        <value>${db.url}</value> 
        </property>

        <property name="username" > 
        <value>${db.username}</value>
        </property>

        <property name="password">  
        <value>${db.password}</value>
        </property>

    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- ================MySQL/Hibernate================ -->

    <bean id="mysqlSessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="mysqlDataSource" />
        <property name="annotatedClasses">
            <list>
            <value>com.myapp.domain.myDomain2</value>       
            </list>
        </property>

        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
        </property>
    </bean>


    <bean id="mysqlDataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="url">
        <value>${db.mysql.url}</value> 
        </property>

        <property name="username" > 
        <value>${db.mysql.username}</value>
        </property>

        <property name="password">  
        <value>${db.mysql.password}</value>
        </property>

    </bean>

    <bean id="mysqltransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="mysqlSessionFactory" />
    </bean>

        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />    

        <!-- ================End of MySQL/Hibernate================ -->


</beans>

回答1:

problem fixed, it was with the remote server firewall.