0

我想为两个不同的数据库定义一个路由数据源:mysql 和 oracle

我已经定义了以下 config.xml

<bean id="dataSourceTarget" class="com.test.common.RoutingDataSource">
    <property name="targetDataSources">
        <map key-type="com.test.common.DataSourceType">
            <entry key="MY_SQL" value-ref="mySqlDataSource"/>
            <entry key="ORACLE" value-ref="oracleDataSource"/>
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="mySqlDataSource"/>
</bean>

<bean id="mySqlDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="${jdbc.driver.classname}" />
    <property name="jdbcUrl" value="${mysql.url}" />
    <property name="user" value="${mysql.username}" />
    <property name="password" value="${mysql.password}" />
</bean>

<bean id="oracleDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
    <property name="jdbcUrl" value="${oracle.url}" />
    <property name="user" value="${oracle.user}" />
    <property name="password" value="${oracle.passw}" />
</bean>

现在我可以定义会话工厂和事务管理器

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.use_sql_comments">true</prop>
        </props>
    </property>
</bean>

但这是行不通的。我得到了

Caused by: java.lang.UnsupportedOperationException: The user must supply a JDBC connection

此外,我必须为会话定义方言。但是如何根据它正在处理的数据源来定义它?

4

0 回答 0