【问题标题】:Spring. Multiple data source with one persistence unit春天。具有一个持久性单元的多个数据源
【发布时间】:2017-04-05 19:28:06
【问题描述】:

spring 是否可以使用同一个持久化单元和多个数据源?

例如。我有两个数据库。他们在不同的服务器上。一种仅用于写入操作(插入、更新、删除等),另一种仅用于读取操作。它们都使用相同的结构。那么我可以为它们都使用一个持久性单元吗?

我发现这个ReplicationDriver 解决了我的问题。我只定义了我的 ma​​sterslave 服务器,在代码方面我只定义了 @Transaction(readOnly=true)。因此,当事务是 readOnly=true 时,经理根据 connector/j 使用我的 slave 和其他情况使用我的 ma​​ster文档。现在使用这个tomcat时无法连接到我的数据库。我的意思是它只是等待什么都没有发生。 这是我的 apache tomcat context.xml

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <Resource 
        auth="Container" 
        driverClassName="com.mysql.jdbc.ReplicationDriver" 
        maxActive="250" 
        maxIdle="100"
        maxWait="30"
        validationQuery="select 1"
        name="mysql/test" 
        type="javax.sql.DataSource" 
        url="jdbc:mysql:replication://address=(host=master)(user=root)(password=mroot),address=(host=slave)(user=root)(password=sroot)/test?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF8&amp;failOverReadOnly=false&amp;maxReconnects=10" 
    />
</Context>

这是我的春天context.xml

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <tx:annotation-driven/>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>

    <jee:jndi-lookup id="dataSource" jndi-name="mysql/test" expected-type="javax.sql.DataSource" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="test_pu" />
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean> 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>

我怎么了?为什么tomcat无法连接到db?顺便使用 eclipslink provider v2.4.0

更新

我不知道为什么,但是当我使用 jdbc:mysql://address 这种格式时,我的程序无法连接到数据库并给出以下异常

Caused by: java.lang.NullPointerException
    at com.mysql.jdbc.NonRegisteringDriver.parseHostPortPair(NonRegisteringDriver.java:204)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2235)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284).

但是当使用jdbc:mysql://host1,host2格式连接成功。如果我们使用第二个版本,我应该为每个连接使用相同的用户名和密码,但第一个版本我们可以定义不同的用户名和密码。根据 MySQL 连接器 J 文档

以下是 JDBC URL 连接到 MySQL 服务器,对于 IPv6 连接是必需的,但也可以是 与 IPv4 一起使用(方括号 ([ ]) 中的项目是可选的):

jdbc:mysql://address=(key1=value)[(key2=value)]...[,address=(key3=value)[(key4=value)]...]...[ /[数据库]]” [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

这意味着这也适用于 IP4 但不起作用。有什么我想念的配置吗?

【问题讨论】:

标签: java spring mysql-connector persistence-unit connector-j


【解决方案1】:

据我所知,这是不可能的 详情请参考以下链接:Exaplained why not

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2016-05-17
    • 2012-01-14
    • 1970-01-01
    相关资源
    最近更新 更多