SSH構成マルチデータソース(JDBC接続)
0.使用したJDBCパッケージを取り込む
1.クラスData SourceMapの作成
2.クラスCustomerContextHolderの作成
3.接続プールの構成
4.データベース接続プールの設定
5.接続プールの割当て
6.sessionFactoryの構成
7.spring注入
8.javaコード呼び出しクラスで宣言
9.呼び出し
10.完了
1.クラスData SourceMapの作成
public class DataSourceMap {
public static final String ds1= "ds1";
public static final String ds2= "ds2";
}
2.クラスCustomerContextHolderの作成
import java.util.Map;
import com.FrameWork.db.interf.IDAO;
public class CustomerContextHolder {
private Map<String,IDAO> map =null;
public Map<String, IDAO> getMap() {
return map;
}
public void setMap(Map<String, IDAO> map) {
this.map = map;
}
}
3.接続プールの構成
<!-- -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@192.168.1.188:1521:orcl" />
<property name="username" value="ddd" />
<property name="password" value="xxxx" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="1000" />
<property name="defaultAutoCommit" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="60" />
</bean>
<!-- sql server 2000 car -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<property name="url" value="jdbc:microsoft:sqlserver://192.168.1.109:1433;DatabaseName=car"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
</bean>
<!-- sql server 2000 gps -->
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
<property name="url" value="jdbc:microsoft:sqlserver://192.168.1.109:1433;DatabaseName=gps"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
</bean>
4.データベース接続プールの設定
<!-- Generic IDao - can be used when doing standard CRUD-->
<bean id="idao" class="com.FrameWork.db.impl.DAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="idao1" class="com.FrameWork.db.impl.DAOImpl">
<property name="dataSource" ref="dataSource1" />
</bean>
<bean id="idao2" class="com.FrameWork.db.impl.DAOImpl">
<property name="dataSource" ref="dataSource2" />
</bean>
5.接続プールの割当て
<!-- Construct Map -->
<bean id="nameMap" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="ds1" value-ref="idao1"/>
<entry key="ds2" value-ref="idao2"/>
</map>
</constructor-arg>
</bean>
<bean id="customerContextHolder" class="com.FrameWork.dynamicDataSource.CustomerContextHolder">
<property name="map">
<ref bean="nameMap"></ref>
</property>
</bean>
6.sessionFactoryの構成
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"> <!-- -->
<ref bean="dataSource" />
</property>
<property name="lobHandler" ref="lobHandler" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<!--
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
-->
</props>
</property>
<!--Spring Blob --><!--
<property name="lobHandler" ref="oracleLobHandler" />
--><!--hbm.xml -->
<property name="mappingResources">
<list>
<!-- -->
<value>
com/czkj/event/wzgl/mapping/TWzDepot.hbm.xml
</value>
<value>
com/czkj/event/wzgl/mapping/TWzCkgly.hbm.xml
</value>
....
</list>
</property>
</bean>
7.spring注入
<!-- -->
<bean id="synCarImp" class="com.czkj.service.syn.SynCarImp">
<property name="dynDataSource" ref="customerContextHolder"></property>
<property name="idao" ref="idao"></property>
<property name="ihibernatedao" ref="ihibernatedao"></property>
<property name="sctx" ref="SystemContext"></property>
</bean>
8.javaコード呼び出しクラスで宣言
//
private CustomerContextHolder dynDataSource = null;
public void setDynDataSource(CustomerContextHolder dynDataSource) {
this.dynDataSource = dynDataSource;
}
9.呼び出し
public void test(){
List list = dynDataSource.getMap().get(DataSourceMap.ds2).queryforlist("select * from FuncMain ");
System.out.println(list);
}
10.完了