`

Spring+Hibernate多数据源的配置

    博客分类:
  • J2EE
阅读更多

俗话说:好记性不如烂笔头。况且,我记性还不好。于是,我开始记博文了!

 

不敢说给遇到类似问题的人以什么借鉴,但求我再遇到类似的问题时候,有处可查。

 

Spring+Hibernate多数据源的配置问题:

 

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

//数据源1
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
	 
	  	<property name="jdbcUrl" value="jdbc:oracle:thin:@192.0.0.100:1555:orcl" />
		<property name="user" value="xxx" />
		<property name="password" value="xxx" />
		
	     <property name="initialPoolSize" value="5" />
		<property name="minPoolSize" value="5" />
		<property name="maxPoolSize" value="10" />
		<property name="idleConnectionTestPeriod" value="600" />
		<property name="maxIdleTime" value="600" />
		<property name="maxStatements" value="0" />
		<property name="maxStatementsPerConnection" value="0" />
		<property name="acquireIncrement" value="5" />
		<property name="acquireRetryAttempts" value="200" />
		<property name="acquireRetryDelay" value="180000" />
	</bean>
	
	
<!--连接LNSJ数据库,读取“健康知识”	-->
//数据源2
	<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
	 	
	    <property name="jdbcUrl" value="jdbc:oracle:thin:@119.0.0.0:1555:orcl" />
		<property name="user" value="xxx" />
		<property name="password" value="xxx" />
		 		 
	    <property name="initialPoolSize" value="5" />
		<property name="minPoolSize" value="5" />
		<property name="maxPoolSize" value="10" />
		<property name="idleConnectionTestPeriod" value="600" />
		<property name="maxIdleTime" value="600" />
		<property name="maxStatements" value="0" />
		<property name="maxStatementsPerConnection" value="0" />
		<property name="acquireIncrement" value="5" />
		<property name="acquireRetryAttempts" value="200" />
		<property name="acquireRetryDelay" value="180000" />
	</bean>
</beans>

 

 

-----------------app-hibernate.xml:-----------------------------------------------------------------------

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置sessionFactory1 -->
	<bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
		id="sessionFactory">
		
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="mappingResources">
			<list>
				<value>hbm/TabMsColumn.hbm.xml</value>
				<value>hbm/TabMsAd.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>

				<prop key="hibernate.query.substitutions">true 1, false 0</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.jdbc.batch_size">50</prop>
				<prop key="hibernate.jdbc.fetch_size">100</prop>
				<prop key="hibernate.max_fetch_depth">2</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
			</props>
		</property>
	</bean>

	<!--LNSJ库“健康知识” SessionFactory -->
<!-- 配置sessionFactory1 -->
	<bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
		id="sessionFactory2">
		<property name="dataSource">
			<ref bean="dataSource2" />
		</property>
		<property name="mappingResources">
			<list>
				<value>hbm/TabMsKnowledge.hbm.xml</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>

				<prop key="hibernate.query.substitutions">true 1, false 0</prop>
				<prop key="hibernate.cache.use_query_cache">false</prop>
				<prop key="hibernate.jdbc.batch_size">50</prop>
				<prop key="hibernate.jdbc.fetch_size">100</prop>
				<prop key="hibernate.max_fetch_depth">2</prop>
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
			</props>
		</property>
	</bean>


<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="transactionBase"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		lazy-init="true" abstract="true">
		<!-- 配置事务管理器 -->
		<property name="transactionManager" ref="transactionManager" />
		<!-- 配置事务属性 -->
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager2"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory2" />
	</bean>
	
	<bean id="transactionBase2"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		lazy-init="true" abstract="true">
		<!-- 配置事务管理器 -->
		<property name="transactionManager" ref="transactionManager2" />
		<!-- 配置事务属性 -->
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
</beans>

 

 

-----------------app-dao.xml:-----------------------------------------------------------------------

 

<beans  .......> 
.......
<bean id="imeiDao" class="com.zmks.dao.impl.ImeiDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
<bean id="knowledgeDao" class="com.zmks.dao.impl.IKnowledgeDaoImpl">
		<property name="sessionFactory" ref="sessionFactory2" />
	</bean>
........
<beans />

 

 

-----------------app-dao.xml:-----------------------------------------------------------------------

 

<beans  .......> 
.......
<bean id="knowledgeService" class="com.zmks.service.impl.KnowledgeServiceImpl">
        <constructor-arg index="0" ref="knowledgeDao" />
	</bean>
........
<beans />

 

 

我的J2EE还是很弱,多学习多学习!

 

1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics