多数据源的配置

1、根据用户的选择,使用不同的数据源。

2、解决思路锁定:将sessionFactory属性dataSource设置成不同的数据源,以达到切换数据源的目的。

3、问题产生:因为整个项目用的几乎都是单例模式,当多个用户并发访问数据库的时候,会产生资源争夺的问题。即项目启动时候,所有的bean都被装载到内存,并且每个bean都只有一个对象。正因为只有一个对象,所有的对象属性就如同静态变量(静态变量跟单例很相似,常用静态来实现单例)。整个项目系统的dataSource只有一个,如果很多用户不断的去改变dataSource的值,那必然会出现资源的掠夺问题,造成系统隐患。

4、多资源共享解决思路:同一资源被抢夺的时候,通常有两种做法,a、以时间换空间 b、以空间换时间。

5、线程同步机制就是典型的“以时间换空间”,采用排队稍等的方法,一个个等待,直到前面一个用完,后面的才跟上,多人共用一个变量,用synchronized锁定排队。   

6、“ThreadLocal”就是典型的“以空间换时间”,她可以为每一个人提供一份变量,因此可以同时访问并互不干扰。

7、言归正传:sessionFactory的属性dataSource设置成不用的数据源,首先不能在配置文件中写死,我们必须为她单独写一个类,让她来引用这个类,在这个类中再来判断我们到底要选择哪个数据源。

8、我们把这类取个名字:MultiDataSource.,java,在这个类中要有DataSource的属性,还有方便我们取得配置文件中的数据源,我们还需要ApplicationContext这个属性,有这两个属性就够了。下面是我自己写的MultiDataSource类的源代码:


public class MultiDataSource implementsDataSource {

         private DataSource dateSource=null;

 

         publicvoid setDateSource(DataSource dateSource) {

                   this.dateSource =dateSource;

         }

         public DataSource getDateSource() {

                   returnthis. dateSource;

         }


//其他实现方法自己写,主要是上面几个;

}

9、在单例模式下,我们的系统中只有一个DataSource对象,我们每次调用MultiDataSource都可能是不同的,所以我们把DataSource放在实例变量中,最直接的方法是在getDataSource中告诉她,增加参数让她知道我们需要哪个数据源。

public DataSource getDateSource(StringdataSourceName) {

                   try{

                            if(dataSourceName==null || dataSourceName.equals("")){

                                     returnthis.dateSource;

                            }

                            return(DataSource)this.applicationContext.getBean(dataSourceName);          
                      }catch(NoSuchBeanDefinitionException ex){

                            returnthis.dateSource;

                   }

}


11、       注意的是:DataSourceName就是配置文件中对应的数据源Id

 

ApplicationContext.xml中数据源的代码:

<bean id="DateSource1"

                   class="com.mchange.v2.c3p0.ComboPooledDataSource">

                   <property name="driverClass"

                            value="oracle.jdbc.driver.OracleDriver">

                   </property>

                   <property name="jdbcUrl"

                            value="jdbc:oracle:thin:@192.168.1.113:1521:orcl">

                   </property>

                   <property name="user"value="hycs"></property>

                   <property name="password"value="hycs"></property>

                   <property name="maxPoolSize"value="40"></property>

                   <property name="minPoolSize"value="1"></property>

                   <property name="initialPoolSize"value="1"></property>

                   <property name="maxIdleTime"value="20"></property>

         </bean>

 

         <bean id="DateSource2"

                   class="com.mchange.v2.c3p0.ComboPooledDataSource">

                   <property name="driverClass"

                            value="oracle.jdbc.driver.OracleDriver">

                   </property>

                   <property name="jdbcUrl"

                            value="jdbc:oracle:thin:@192.168.1.12:1521:orcl">

                   </property>

                   <property name="user"value="hycs"></property>

                   <property name="password"value="hycs"></property>

                   <property name="maxPoolSize"value="40"></property>

                   <property name="minPoolSize"value="1"></property>

                   <property name="initialPoolSize"value="1"></property>

                   <property name="maxIdleTime"value="20"></property>

12、          </bean>

 

13、           再说一次sessionfactory在*.xml中的配置

 

<bean id="multiDataSource"class="com.hy.datasource.MultiDataSource">

                   <property name="dateSource"ref="DateSource1"/>

         </bean>

         <bean id="sessionFactory"

                   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                   <property name="dataSource">

                            <ref bean="multiDataSource"/>

           </property>

。

。

。其他自己补上

。

。

</bean>


14、           sessionfactory的属性DataSource已经不是直接ref 配置文件中的数据源了。

15、           她ref的是我们在自己编写的类,在一个封装类里面单独判断,这在外部是完全感觉不到我们数据源的改变。

16、    为了在类中得到ApplicationContext,我们必须让MultiDataSource实现org.springframework.context.ApplicationContextAware接口,并实现其方法:

private ApplicationContext applicationContext=null;

public void setApplicationContext(ApplicationContextapplicationContext) {

                   this.applicationContext = applicationContext;

         }


17、       这样就可以通过this.applicationContext.getBean(dataSourceName)得到dataSource;

18、           写到这里差不多已经完毕了,但是页面上的数据源名字怎么通过request传到MultiDataSource类里呢,难道我们还要通过每个业务层和数据层一层层的传递数据源的名字吗?这样写也太多了吧。

在这里我们可以通过线程的方式跳过业务层和数据层直接把值传递到MultiDataSource类里

public class SpObserver{

         @SuppressWarnings("unchecked")

         private static ThreadLocal<String> local = newThreadLocal();

         @SuppressWarnings("unchecked")

         public staticvoid putSp(String sp) {

                   local.set(sp);

         }

         public static String getSp() {

                   returnlocal.get();

         }

 }


 

20、 做一个filter,每一次请求的时候就调用SpObserver.putSp(dataSources);

 

request中的dataSourceName传给SpObserver对象并改变MultiDataSource类中getDataSource方法。

public DataSourcegetDateSource() {

                   String sp=SpObserver.getSp();

                   returnthis.getDateSource(sp);

}

22、           小小说明:每一次请求如果都发送数据源的名字未免也太麻烦了,可以在发送请求的时候把她保存在session中,如果下次没发请求的话,自己去session中取值,如果再发请求的时候就改变session中保存的值。

23、           总的来说就增加MultiDataSourceSpObserver两个类,其他的就是filter和配置文件的事了。下面我把详细的代码附上去;

MultiDataSource类的完整代码:

package com.hy.datasource;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

importorg.springframework.beans.factory.NoSuchBeanDefinitionException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

 

publicclass MultiDataSource implementsDataSource,ApplicationContextAware{

         private DataSource dateSource=null;

         private ApplicationContext applicationContext=null;

 

         public Connection getConnection() throwsSQLException {

                   // TODOAuto-generated method stub

                   return getDateSource().getConnection();

         }

 

         public Connection getConnection(String arg0, String arg1)

                            throwsSQLException {

                   // TODOAuto-generated method stub

                   returnnull;

         }

 

         public PrintWriter getLogWriter() throwsSQLException {

                   // TODOAuto-generated method stub

                   returnnull;

         }

 

         publicint getLoginTimeout() throwsSQLException {

                   // TODOAuto-generated method stub

                   return 0;

         }

 

         publicvoid setLogWriter(PrintWriter arg0) throwsSQLException {

                   // TODOAuto-generated method stub

 

         }

 

         publicvoid setLoginTimeout(int arg0) throwsSQLException {

                   // TODOAuto-generated method stub

 

         }

 

         public ApplicationContext getApplicationContext() {

                   returnapplicationContext;

         }

 

         publicvoid setApplicationContext(ApplicationContextapplicationContext) {

                   this.applicationContext = applicationContext;

         }

 

         public DataSource getDateSource(String dataSourceName) {

                   System.out.println("线程进来2");

                   try{

                            if(dataSourceName==null ||dataSourceName.equals("")){

                                     System.out.println("线程进来3");

                                     returnthis.dateSource;

                            }

                            System.out.println("线程进来4");

                            return(DataSource)this.applicationContext.getBean(dataSourceName);//根据dataSourceName加载配置文件中的数据源对象

                   }catch(NoSuchBeanDefinitionException ex){

                            System.out.println("线程进来5");

                            returnthis.dateSource;

                   }

         }

 

         publicvoid setDateSource(DataSource dateSource) {

                   System.out.println("dataSource方法");

                   this.dateSource = dateSource;

         }

         /**

          *项目启动时,默认使用defaultDataSource

          *用户选择时,根据选择数据源

          *ThreadLocal在单例下生成多个线程变量副本,解决多用户并发访问

          */

         public DataSource getDateSource() {

                   System.out.println("线程进来1");

                   String sp=SpObserver.getSp();

                   returnthis.getDateSource(sp);

         }

 

}


         SpObserver的完整代码:

 package com.hy.datasource;

public class SpObserver{

     @SuppressWarnings("unchecked")

     private static ThreadLocal<String> local = new ThreadLocal();

 

     @SuppressWarnings("unchecked")

     public static void putSp(String sp) {

              System.out.println("set方法");

              local.set(sp);

     }

 

     public static String getSp() {

              System.out.println("get方法");

              returnlocal.get();

     }

}


 

24、MyFilter的完整代码:

public class MyFilter implements Filter {

 

         public void destroy() {

                   // TODOAuto-generated method stub

 

         }

 

         public void doFilter(ServletRequest request, ServletResponseresponse,

                            FilterChain chain) throwsIOException, ServletException {

                   HttpServletRequest req=(HttpServletRequest)request;

                   String dataSources=req.getParameter("dataSource");

                   String dataSource=(String)req.getSession().getAttribute("dataSource");

                   if(StringUtils.hasText(dataSources)){

                            SpObserver.putSp(dataSources);

                   }elseif(StringUtils.hasText(dataSource)){

                            SpObserver.putSp(dataSource);

                   }

                   chain.doFilter(request, response);

         }

 

         publicvoid init(FilterConfig arg0) throws ServletException{

                   // TODOAuto-generated method stub

 

         }

 

}


26、       配置文件的部分配置:web.xml的过滤器配置

<filter>

         <filter-name>dsFilter</filter-name>

                   <filter-class>com.hy.datasource.MyFilter</filter-class>

         </filter>

<filter-mapping>

    <filter-name>dsFilter</filter-name>   

      <url-pattern>/*</url-pattern>   

</filter-mapping>


27.ApplicationContext.xml

<bean id="DateSource1"

                   class="com.mchange.v2.c3p0.ComboPooledDataSource">

                   <property name="driverClass"

                            value="oracle.jdbc.driver.OracleDriver">

                   </property>

                   <property name="jdbcUrl"

                            value="jdbc:oracle:thin:@192.168.1.113:1521:orcl">

                   </property>

                   <property name="user"value="hycs"></property>

                   <property name="password"value="hycs"></property>

                   <property name="maxPoolSize"value="40"></property>

                   <property name="minPoolSize"value="1"></property>

                   <property name="initialPoolSize"value="1"></property>

                   <property name="maxIdleTime"value="20"></property>

         </bean>

 

         <bean id="DateSource2"

                   class="com.mchange.v2.c3p0.ComboPooledDataSource">

                   <property name="driverClass"

                            value="oracle.jdbc.driver.OracleDriver">

                   </property>

                   <property name="jdbcUrl"

                            value="jdbc:oracle:thin:@192.168.1.12:1521:orcl">

                   </property>

                   <property name="user"value="hycs"></property>

                   <property name="password"value="hycs"></property>

                   <property name="maxPoolSize"value="40"></property>

                   <property name="minPoolSize"value="1"></property>

                   <property name="initialPoolSize"value="1"></property>

                   <property name="maxIdleTime"value="20"></property>

         </bean>

         <bean id="multiDataSource"class="com.hy.datasource.MultiDataSource">

                   <property name="dateSource"ref="DateSource1"/>

         </bean>

         <bean id="sessionFactory"

                   class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

                   <property name="dataSource">

                            <ref bean="multiDataSource"/>

                   </property>

                   <property name="hibernateProperties">

                            <props>

                                     <prop key="hibernate.dialect">

                                               org.hibernate.dialect.Oracle9Dialect

                                     </prop>

                            </props>

                   </property>

                   <property name="mappingResources">

                            <list>

                                     <value>com/hy/bean/Job.hbm.xml</value>

                                     <value>com/hy/bean/News.hbm.xml</value>

                                     <value>com/hy/bean/ProductInfo.hbm.xml</value>

                                     <value>com/hy/bean/Sort.hbm.xml</value>

                                     <value>com/hy/bean/Userinfo.hbm.xml</value>

                                     <value>com/hy/bean/Message.hbm.xml</value>

                            </list>

                   </property>

         </bean>

总结:

1、上面的方案是针对不同数据库但表的结构完全一样而设计的,用的是同一个sessionfactory。

2、如果是针对用户使用不同数据库且不同的表结构

类似的可以根据上面的设计搞定不同的sessionfactory,也可以写一个MultiSessionFactory的类来实现(提示:如果多个数据库是不同时使用才需要这样做,如果多个数据库是同时使用,就不必写了,直接在配置文件中几个数据库就配置几个sessionfactory就行了)。

3、还有一种情况是不同数据库有些表结构一样有些不一样:如果一样的就用同个sessionfactory,不同个dataSource,不一样的就在页面传入sessionfactoryName和dataSourceName,让他们分别对号入座就行了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值