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.xmljava

<!-- 數據庫鏈接池配置 -->
	<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下執行JDBCweb

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;
	}
相關文章
相關標籤/搜索