<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    自由飛翔

    我在仰望,java之上

    統(tǒng)計

    留言簿(2)

    我關注的blog

    閱讀排行榜

    評論排行榜

    轉載:在項目中使用多個數(shù)據(jù)源-多sessionFactory方案

    適用范圍:適合SSH架構訪問多個數(shù)據(jù)庫,數(shù)據(jù)庫的類型和表結構不必相同,且沒有跨庫事務的情況(跨庫事務最好用分布式事務處理)。

    文章來源:http://apps.hi.baidu.com/share/detail/15756344

    實現(xiàn)方式:我們可以在spring的配置文件中配置多個sessionFactory,如:
    <bean id="aDataSource"
       class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <property name="driverClassName">
        <value>${adriver}</value>
       </property>
       <property name="url">
        <value>${aurl}</value>
       </property>
       <property name="username">
        <value>${ausername}</value>
       </property>
       <property name="password">
        <value>${apassword}</value>
       </property>
    </bean>
    <bean id="bDataSource"
       class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <property name="driverClassName">
        <value>${bdriver}</value>
       </property>
       <property name="url">
        <value>${burl}</value>
       </property>
       <property name="username">
        <value>${busername}</value>
       </property>
       <property name="password">
        <value>${bpassword}</value>
       </property>
    </bean>
    <bean id="cDataSource"
       class="org.apache.commons.dbcp.BasicDataSource"
       destroy-method="close">
       <property name="driverClassName">
        <value>${cdriver}</value>
       </property>
       <property name="url">
        <value>${curl}</value>
       </property>
       <property name="username">
        <value>${cusername}</value>
       </property>
       <property name="password">
        <value>${cpassword}</value>
       </property>
    </bean>

     

    <!-- Hibernate SessionFactorys -->
    <bean id="aSessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource">
        <ref local="aDataSource" />
       </property>
       <property name="mappingResources">
        <list>
         <value>
          .hbm.xml文件
         </value>
        </list>
       </property>
       <property name="hibernateProperties">
        <props>
         <prop key="hibernate.dialect">
          ${ahibernate.dialect}
         </prop>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="format_sql">true</prop>
        </props>
       </property>
    </bean>

    <bean id="bSessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource">
        <ref local="bDataSource" />
       </property>
       <property name="mappingResources">
        <list>
         <value>
          .hbm.xml文件
         </value>
        </list>
       </property>
       <property name="hibernateProperties">
        <props>
         <prop key="hibernate.dialect">
          ${bhibernate.dialect}
         </prop>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="format_sql">true</prop>
        </props>
       </property>
    </bean>

    <bean id="cSessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource">
        <ref local="cDataSource" />
       </property>
       <property name="mappingResources">
        <list>
         <value>
           .hbm.xml文件
         </value>
        </list>
       </property>
       <property name="hibernateProperties">
        <props>
         <prop key="hibernate.dialect">
          ${chibernate.dialect}
         </prop>
         <prop key="hibernate.show_sql">true</prop>
         <prop key="format_sql">true</prop>
        </props>
       </property>
    </bean>

    <bean id="sessionFactory" class="com.cintel.dcp.datasource.MultiSessionFactory">
       <property name="sessionFactory"><ref local="aSessionFactory"/></property>
    </bean>
    注意:最后一個com.cintel.dcp.datasource.MultiSessionFactory要自己實現(xiàn),它實現(xiàn)了SessionFactory接口和ApplicationContext接口,如下:
    package com.cintel.dcp.datasource;

    import java.io.Serializable;
    import java.sql.Connection;
    import java.util.Map;
    import java.util.Set;

    import javax.naming.NamingException;
    import javax.naming.Reference;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.hibernate.HibernateException;
    import org.hibernate.Interceptor;
    import org.hibernate.SessionFactory;
    import org.hibernate.StatelessSession;
    import org.hibernate.classic.Session;
    import org.hibernate.engine.FilterDefinition;
    import org.hibernate.metadata.ClassMetadata;
    import org.hibernate.metadata.CollectionMetadata;
    import org.hibernate.stat.Statistics;
    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;

    public class MultiSessionFactory implements SessionFactory, ApplicationContextAware {
    private static final long serialVersionUID = 2064557324203496378L;
    private static final Log log = LogFactory.getLog(MultiSessionFactory.class);
    private ApplicationContext applicationContext = null;
    private SessionFactory sessionFactory = null;

    public ApplicationContext getApplicationContext() {
       return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
       this.applicationContext = applicationContext;
    }

    public SessionFactory getSessionFactory(String sessionFactoryName) {
       log.debug("sessionFactoryName:"+sessionFactoryName);
       try{
        if(sessionFactoryName==null||sessionFactoryName.equals("")){
         return sessionFactory;
        }
        return (SessionFactory)this.getApplicationContext().getBean(sessionFactoryName);
       }catch(NoSuchBeanDefinitionException ex){
        throw new RuntimeException("There is not the sessionFactory <name:"+sessionFactoryName+"> in the applicationContext!");
       }
    }

    public SessionFactory getSessionFactory() {
       String sessionFactoryName = CustomerContextHolder.getCustomerType();
       return getSessionFactory(sessionFactoryName);
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
       this.sessionFactory = sessionFactory;
    }


    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#close()
    */
    public void close() throws HibernateException {
       getSessionFactory().close();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evict(java.lang.Class)
    */
    public void evict(Class persistentClass) throws HibernateException {
       getSessionFactory().evict(persistentClass);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evict(java.lang.Class, java.io.Serializable)
    */
    public void evict(Class persistentClass, Serializable id) throws HibernateException {
       getSessionFactory().evict(persistentClass, id);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evictCollection(java.lang.String)
    */
    public void evictCollection(String roleName) throws HibernateException {
       getSessionFactory().evictCollection(roleName);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evictCollection(java.lang.String, java.io.Serializable)
    */
    public void evictCollection(String roleName, Serializable id) throws HibernateException {
       getSessionFactory().evictCollection(roleName, id);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evictEntity(java.lang.String)
    */
    public void evictEntity(String entityName) throws HibernateException {
       getSessionFactory().evictEntity(entityName);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evictEntity(java.lang.String, java.io.Serializable)
    */
    public void evictEntity(String entityName, Serializable id) throws HibernateException {
       getSessionFactory().evictEntity(entityName, id);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evictQueries()
    */
    public void evictQueries() throws HibernateException {
       getSessionFactory().evictQueries();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#evictQueries(java.lang.String)
    */
    public void evictQueries(String cacheRegion) throws HibernateException {
       getSessionFactory().evictQueries(cacheRegion);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getAllClassMetadata()
    */
    public Map getAllClassMetadata() throws HibernateException {
       return getSessionFactory().getAllClassMetadata();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getAllCollectionMetadata()
    */
    public Map getAllCollectionMetadata() throws HibernateException {
       return getSessionFactory().getAllCollectionMetadata();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getClassMetadata(java.lang.Class)
    */
    public ClassMetadata getClassMetadata(Class persistentClass) throws HibernateException {
       return getSessionFactory().getClassMetadata(persistentClass);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getClassMetadata(java.lang.String)
    */
    public ClassMetadata getClassMetadata(String entityName) throws HibernateException {
       return getSessionFactory().getClassMetadata(entityName);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getCollectionMetadata(java.lang.String)
    */
    public CollectionMetadata getCollectionMetadata(String roleName) throws HibernateException {
       return getSessionFactory().getCollectionMetadata(roleName);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getCurrentSession()
    */
    public Session getCurrentSession() throws HibernateException {
       return getSessionFactory().getCurrentSession();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getDefinedFilterNames()
    */
    public Set getDefinedFilterNames() {
       return getSessionFactory().getDefinedFilterNames();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getFilterDefinition(java.lang.String)
    */
    public FilterDefinition getFilterDefinition(String filterName) throws HibernateException {
       return getSessionFactory().getFilterDefinition(filterName);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#getStatistics()
    */
    public Statistics getStatistics() {
       return getSessionFactory().getStatistics();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#isClosed()
    */
    public boolean isClosed() {
       return getSessionFactory().isClosed();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#openSession()
    */
    public Session openSession() throws HibernateException {
       return getSessionFactory().openSession();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#openSession(java.sql.Connection)
    */
    public Session openSession(Connection connection) {
       return getSessionFactory().openSession(connection);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#openSession(org.hibernate.Interceptor)
    */
    public Session openSession(Interceptor interceptor) throws HibernateException {
       return getSessionFactory().openSession(interceptor);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#openSession(java.sql.Connection, org.hibernate.Interceptor)
    */
    public Session openSession(Connection connection, Interceptor interceptor) {
       return getSessionFactory().openSession(connection, interceptor);
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#openStatelessSession()
    */
    public StatelessSession openStatelessSession() {
       return getSessionFactory().openStatelessSession();
    }
    /* (non-Javadoc)
    * @see org.hibernate.SessionFactory#openStatelessSession(java.sql.Connection)
    */
    public StatelessSession openStatelessSession(Connection connection) {
       return getSessionFactory().openStatelessSession(connection);
    }
    /* (non-Javadoc)
    * @see javax.naming.Referenceable#getReference()
    */
    public Reference getReference() throws NamingException {
       return getSessionFactory().getReference();
    }
    }


    然后我用一個常量類來標識sessionFactory
    public class DynamicDataSourceType {
    public static final String A= "aSessionFactory";
    public static final String B= "bSessionFactory";
    public static final String C= "cSessionFactory";
    }

    最后一個關鍵類:用來存放當前正在使用的sessionFactory
    public class CustomerContextHolder {

    private static final ThreadLocal contextHolder = new ThreadLocal();

    public static void setCustomerType(String customerType) {
       Assert.notNull(customerType, "customerType cannot be null");
       contextHolder.set(customerType);
    }

    public static String getCustomerType() {
       return (String) contextHolder.get();
    }

    public static void clearCustomerType() {
       contextHolder.remove();
    }
    }

    可以在action、service、dao中進行數(shù)據(jù)庫切換,切換方式:
    CustomerContextHolder.setCustomerType(DynamicDataSourceType.A);

    以上思路來自javaEye論壇的一個高手,在此標識感謝



    Gavin

    posted on 2011-11-14 16:11 GavinMiao 閱讀(856) 評論(0)  編輯  收藏 所屬分類: other

    主站蜘蛛池模板: 东方aⅴ免费观看久久av| 青青草97国产精品免费观看| 最近中文字幕大全免费版在线| 国产在线19禁免费观看国产| 亚洲hairy多毛pics大全| 国产极品粉嫩泬免费观看| 亚洲AV无码一区二区三区性色| 成人免费毛片观看| 色费女人18女人毛片免费视频| 国产伦精品一区二区三区免费迷| 色婷婷精品免费视频| 中文字幕亚洲天堂| 国产免费AV片在线观看| 亚洲伊人tv综合网色| 在线观看视频免费完整版| 亚洲三级在线观看| 大胆亚洲人体视频| 两个人看的www高清免费观看| 亚洲国产成人一区二区精品区| **一级一级毛片免费观看| 亚洲精品第一综合99久久| 四虎影在线永久免费观看| 久久不见久久见免费影院www日本| 亚洲精品国产精品乱码不卡√ | 亚洲免费视频观看| 日韩黄色免费观看| 好湿好大好紧好爽免费视频| 亚洲国产精品免费视频| 在线免费观看污网站| 乱爱性全过程免费视频| 久久精品国产亚洲av麻豆色欲| 免费黄色一级毛片| 99视频在线免费观看| 亚洲乱码卡一卡二卡三| 亚洲片一区二区三区| 国产一卡二卡四卡免费| 午夜免费国产体验区免费的| 亚洲黄色网址大全| 亚洲成av人片不卡无码久久| 最近2019免费中文字幕视频三| 在线亚洲v日韩v|