相关疑难解决方法(0)

从具有相同持久性单元的多个Db读取?

我需要一些帮助来使用相同的Persistence单元为多个db配置多个连接.

它们都具有相同的架构.因此,我想使用相同的持久性单元/ DAO等,并且不想设置10个EntityManagers,10 Persistence xml等.有没有办法做到这一点?这是我当前的配置:

  <persistence-unit name="PersistenceUnit-c1" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.show_sql" value="${hibernate-show-sql}"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SybaseDialect" />
            <property name="hibernate.c3p0.min_size" value="${hibernate-c3p0-min-size}" />
            <property name="hibernate.c3p0.max_size" value="${hibernate-c3p0-max-size}" />
            <property name="hibernate.c3p0.timeout" value="${hibernate-c3p0-timeout}" />
            <property name="hibernate.c3p0.max_statements" value="${hibernate-c3p0-max-statements}" />
            <property name="hibernate.c3p0.idle_test_period" value="${hibernate-c3p0-idle-test-periods}" />     
        </properties>
Run Code Online (Sandbox Code Playgroud)
        <class>com.domain.TktOrder</class>
        <exclude-unlisted-classes/>
    </persistence-unit>
Run Code Online (Sandbox Code Playgroud)

我也使用Spring/hibernate来设置我的上下文:

    <bean id="EntityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:persistenceUnitName="PersistenceUnit-c1" 
    p:dataSource-ref="DataSource">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
            p:showSql="${hibernate-show-sql}" 
            p:generateDdl="false" 
            p:databasePlatform="org.hibernate.dialect.SybaseDialect" />
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

<bean id="DataSource" 
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" 
    p:driverClass="net.sourceforge.jtds.jdbc.Driver"
    p:jdbcUrl="jdbc:jtds:sybase://url.net:port;DatabaseName=db_1"
    p:user="user" 
    p:password="password"
    />
Run Code Online (Sandbox Code Playgroud)

最后我使用:

@PersistenceContext(unitName="PersistenceUnit-c1")
public void setEntityManager(EntityManager …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa

14
推荐指数
1
解决办法
2万
查看次数

使用AbstractRoutingDataSource动态更改数据库架构/目录

根据这篇文章,您可以使用Spring Framework中的AbstractRoutingDataSource动态更改应用程序使用的数据源.

但是,使用的数据源是由配置定义的,而不是以编程方式定义的.有没有办法配置要在运行时使用的数据源?

该解决方案的可扩展性如何,即数据源数量有何限制?

谢谢!

java database spring datasource runtime

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×2

spring ×2

database ×1

datasource ×1

hibernate ×1

jpa ×1

runtime ×1