我开发了报告工具的应用程序
我必须连接多个远程端数据库及其凭据并获取数据库值
我怎么能这样做?
这是我的hibernate配置文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.jdbc.batch_size">0</property>
</session-factory>
</hibernate-configuration>
答案 0 :(得分:1)
您应该为每个DBS配置单独的会话工厂和数据源配置,您可以通过数据源名称引用正确的会话工厂和数据源配置。
这意味着如果您有3个远程数据库,那么理想情况下您应该有3个sessionFacories和数据源。
示例:
hibernate-first.cfg.xml - 与您一样
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.jdbc.batch_size">0</property>
<!-- Here commes the entities mapped to tables in the FIRST database -->
<mapping class="com.yourpackage.EntityOftheFirstDatabase" />
</session-factory>
hibernate-second.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.jdbc.batch_size">0</property>
<!-- Here commes the entities mapped to tables in the SECOND database -->
<mapping class="com.yourpackage.EntityOftheSecondDatabase" />
</session-factory>
现在让我们在春天配置会话工厂:
<!-- Hibernate SessionFactory for the FIRST database -->
<bean id="sessionFactoryForTheFirstDatabase" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceForTheFirstDatabase" />
<property name="configLocation" value="classpath:hibernate-first.cfg.xml" />
</bean>
<!-- Hibernate SessionFactory for the SECOND database -->
<bean id="sessionFactoryForTheSecondDatabase" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceForTheSecondDatabase" />
<property name="configLocation" value="classpath:hibernate-second.cfg.xml" />
</bean>
<强>数据源强>
<bean id="dataSourceForTheFirstDatabase" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="your specific" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
第二个数据源只是指向第二个数据库。如果要配置Tx管理,则需要两个事务管理器用于数据库:
<bean id="firstDbServiceProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<description>Transaction boundaries are mapped to service layer</description>
<property name="proxyTargetClass" value="false" />
<property name="beanNames">
<list>
<value>ServicesMangingoperationsAboveYourFirstDatabase1</value>
<value>ServicesMangingoperationsAboveYourFirstDatabase2</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>FirstDbTxInterceptor</value>
</list>
</property>
</bean>
如果你想在多个数据库中拥有单一交易,也可以,不管你是否需要它都不知道!希望这对你来说足够清楚。我知道有更多奇特的方法来定义TX管理器和弹簧的东西,主要是如果你使用最新的,我会建议你尝试我所描述的,然后你可以调整它 - 谷歌是你的朋友:)
答案 1 :(得分:1)
为了连接多个数据库,写一个会话工厂并不好。所以有多少数据库,如果你想连接写多个配置文件是最好的方法。例如假设,我想连接 oracle,mysql 数据库。所以我可以做的是我将为oracle编写一个配置文件,即 oracle-configuration.cfg.xml ,对于mysql,我将创建 mysql-configuration.cfg.xml < / p>
现在您的问题是要连接远程数据库,您需要为远程地址提供凭据。
例如,我的远程数据库地址是 192.168.0.14 。现在我想连接oracle数据库 现在连接属性如
<强>甲骨文configuration.cfg.xml 强>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.username">ranga</property> <property name="connection.password">ranga</property> <property name="connection.url">jdbc:oracle:thin:@192.168.0.14:1521:XE</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="com/ranga/mapping/Persom.hbm.xml"/> </session-factory> </hibernate-configuration>