Hibernate JNDI 数据源动态切换及在Hibernate下执行JDBC

1. Hibernate JNDI 数据源动态切换
1.1 配置tomcat context.xml数据
	<!-- jdbc/Datasource_DAP -->
	<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" 
					maxActive="100" maxIdle="30" maxWait="-1" 
					name="jdbc/Datasource_DAP" 
					type="javax.sql.DataSource" 
					url="jdbc:oracle:thin:@10.2.98.80:1521/test"
					testOnBorrow="true"  
    				testWhileIdle="true"  
    				validationQuery="select 1 from dual" 
					username="dap"
					password="*******"/> 
	<!-- jdbc/Datasource_CSA -->
	<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" 
					maxActive="100" maxIdle="30" maxWait="-1" 
					name="jdbc/Datasource_CSA" 
					type="javax.sql.DataSource" 
					url="jdbc:oracle:thin:@10.2.98.80:1521/test"
					testOnBorrow="true"  
    				testWhileIdle="true"  
    				validationQuery="select 1 from dual" 
					username="csa"
					password="******"/> 
1.2 web.xml 配置
	<resource-ref>         
     <res-ref-name>jdbc/Datasource_DAP</res-ref-name>       
      <res-type>javax.sql.DataSource</res-type>       
      <res-auth>Container</res-auth>       
    </resource-ref>
    
    <resource-ref>         
     <res-ref-name>jdbc/Datasource_CSA</res-ref-name>       
      <res-type>javax.sql.DataSource</res-type>       
      <res-auth>Container</res-auth>       
    </resource-ref>
1.3 修改配置连接池的配置文件

如果是把Hibernate交给Spring管理,则修改Spring的配置文件,本项目修改context-base.xml

<!-- 数据库连接池配置 -->
	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/Datasource_DAP</value>
		</property>
	</bean>
	
	<!-- 数据库连接池配置 -->
	<bean id="dataSourceCsa" class="org.springframework.jndi.JndiObjectFactoryBean">
		<property name="jndiName">
			<value>java:comp/env/jdbc/Datasource_CSA</value>
		</property>
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingResources">
			<list>
				<value>hibernate/privilege/TPrivilegeMenu.hbm.xml</value>
				<value>---------</value>
</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <!--org.hibernate.dialect.SQLServerDialect -->
				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory
				</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.jdbc.batch_size">50</prop>
				<!-- <prop key="hibernate.jdbc.fetch_size">100</prop> --><!--由于oracle驱动的bug导致memory leak -->
				<prop key="hibernate.generate_statistics">true</prop>
				<prop key="hibernate.query.substitutions">true=1 false=0</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="lobHandler">
			<ref bean="lobHandler" />
		</property>
	</bean>
	
	<bean id="sessionFactoryCsa"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSourceCsa" />
		<property name="mappingResources">
			<list>
				<value>hibernate/privilege/TPrivilegeMenu.hbm.xml</value>
								<value>---------</value>
								</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <!--org.hibernate.dialect.SQLServerDialect -->
				<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory
				</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.jdbc.batch_size">50</prop>
				<!-- <prop key="hibernate.jdbc.fetch_size">100</prop> --><!--由于oracle驱动的bug导致memory leak -->
				<prop key="hibernate.generate_statistics">true</prop>
				<prop key="hibernate.query.substitutions">true=1 false=0</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="lobHandler">
			<ref bean="lobHandler" />
		</property>
	</bean>
1.4 封装Hibernate BaseDao处修改,或Dao修改

增加一个SessionFactory,显示调用,调用代码参考2.Hibernate下执行JDBC

public class BaseDao implements IBaseDao
 {
    @Autowired
    private SessionFactory sessionFactory;
	@Autowired
	private SessionFactory sessionFactoryCsa;

2.Hibernate下执行JDBC
	private boolean executeByJdbcCsa(String sql){
		boolean result = true;
		ConnectionProvider cp =((SessionFactoryImplementor)sessionFactoryCsa).getConnectionProvider();
		Connection conn =null;
		Statement statement = null;
		try{
			//Transaction tx=this.getSessionCsa().beginTransaction(); //注意用的是hibernate事务处理边界
			//开启session和事务
			conn =cp.getConnection();
			statement = conn.createStatement();
			result =  statement.execute(sql);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
		//关闭session
		}//end try
		return result;
	}

转载于:https://my.oschina.net/Alexyeh/blog/2962285

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值