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

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

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

    京山游俠

    專注技術(shù),拒絕扯淡
    posts - 50, comments - 868, trackbacks - 0, articles - 0
      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
    在SpringSide 3社區(qū)中,不斷有人提出多數(shù)據(jù)源配置的問(wèn)題,但是時(shí)至今日卻一直沒(méi)有一個(gè)完美的答案。經(jīng)過(guò)一個(gè)星期的折騰,我總算搞清楚了在SpringSide 3中配置多數(shù)據(jù)源的各種困難并加以解決,在這里,特地把我配置SpringSide 3項(xiàng)目中多數(shù)據(jù)源的過(guò)程寫出來(lái),與大家分享。

    我使用的SpringSide的版本是江南白衣最新發(fā)布的3.1.4翻墻版,在上一篇博文中,記錄了我折騰的全過(guò)程,感興趣的朋友可以看看:
    http://www.tkk7.com/youxia/archive/2009/07/12/286454.html

    下面進(jìn)入正題:

    結(jié)論:在基于SpringSide 3的項(xiàng)目中,如果要使用多個(gè)數(shù)據(jù)庫(kù),首先要配置多個(gè)數(shù)據(jù)源,然后配置多個(gè)SessionFactory,這本身沒(méi)有問(wèn)題,但是一涉及到事務(wù),問(wèn)題就來(lái)了,在多數(shù)據(jù)源的環(huán)境下,必須使用JTATransactionManager,而使用JTATransactionManager,就必須得有提供JTA功能的應(yīng)用服務(wù)器或提供JTA功能的別的什么組件。

    以上結(jié)論絕對(duì)正確,是屬于SpringSide 3中關(guān)于使用多個(gè)數(shù)據(jù)庫(kù)的最權(quán)威解答,下面來(lái)看具體過(guò)程:

    方法一、使用GlassFish應(yīng)用服務(wù)器

    1、準(zhǔn)備GlassFish服務(wù)器,下載地址為http://download.java.net/glassfish/v3/promoted/,我選擇的是08-Jul-2009 17:20發(fā)布的大小為72M的latest-glassfish.zip,這里需要強(qiáng)調(diào)的一點(diǎn)是千萬(wàn)不要選擇latest-glassfish-windows.exe這個(gè)版本,因?yàn)檫@個(gè)版本在Windows環(huán)境中只安裝GlassFish而不提供合理的初始化配置,對(duì)于新手來(lái)說(shuō)使用門檻太高,而ZIP版一解壓縮就可以使用,其服務(wù)器是配置好了的;

    2、在GlassFish中配置多個(gè)數(shù)據(jù)源,啟動(dòng)GlassFish后,訪問(wèn)4848端口就可以進(jìn)入到GlassFish的管理界面,在其中配置兩個(gè)數(shù)據(jù)源,其資源名稱分別為jdbc/dataSourceContent和jdbc/dataSourceIndex,如下圖:


    3、在項(xiàng)目中配置多個(gè)DataSource和多個(gè)SessionFactory,并選擇JTATransactionManager作為事務(wù)管理器,這里的DataSource是使用JNDI查找從應(yīng)用服務(wù)器中獲得的。下面是我項(xiàng)目中的applicationContext.xml文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee
    ="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context
    ="http://www.springframework.org/schema/context"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
        default-lazy-init
    ="true">

        
    <description>Spring公共配置文件 </description>

        
    <!-- 定義受環(huán)境影響易變的變量 -->
        
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            
    <property name="ignoreResourceNotFound" value="true" />
            
    <property name="locations">
                
    <list>
                    
    <!-- 標(biāo)準(zhǔn)配置 -->
                    
    <value>classpath*:/application.properties</value>
                
    </list>
            
    </property>
        
    </bean>

        
    <!-- 使用annotation 自動(dòng)注冊(cè)bean,并保證@Required,@Autowired的屬性被注入 -->
        
    <context:component-scan base-package="cn.puretext" />

        
    <!-- 數(shù)據(jù)源配置,使用應(yīng)用服務(wù)器的數(shù)據(jù)庫(kù)連接池 -->
        
    <jee:jndi-lookup id="dataSourceContent" jndi-name="jdbc/dataSourceContent" />
        
    <jee:jndi-lookup id="dataSourceIndex" jndi-name="jdbc/dataSourceIndex" />

        
    <!-- Hibernate配置 -->
        
    <bean id="sessionFactoryContent" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            
    <property name="dataSource" ref="dataSourceContent" />
            
    <property name="namingStrategy">
                
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
            
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
                    
    </prop>
                    
    <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                
    </props>
            
    </property>
            
    <property name="packagesToScan" value="cn.puretext.entity.*" />
        
    </bean>
        
    <bean id="sessionFactoryIndex" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            
    <property name="dataSource" ref="dataSourceIndex" />
            
    <property name="namingStrategy">
                
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
            
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
                    
    </prop>
                    
    <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                
    </props>
            
    </property>
            
    <property name="packagesToScan" value="cn.puretext.entity.*" />
        
    </bean>
        
        
    <!-- 事務(wù)管理器配置,多數(shù)據(jù)源JTA事務(wù)-->
        
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
        
        
    <!-- 使用annotation定義事務(wù) -->
        
    <tx:annotation-driven transaction-manager="transactionManager" />
     
    </beans>

    4、由于配置了多個(gè)SessionFactory,所以需要在web.xml中配置兩個(gè)OpenSessionInViewFilter,下面是我的web.xml文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        
    <display-name>PureText</display-name>
        
    <!-- Spring ApplicationContext配置文件的路徑,可使用通配符,多個(gè)路徑用,號(hào)分隔
            此參數(shù)用于后面的Spring Context Loader 
    -->
        
    <context-param>
            
    <param-name>contextConfigLocation</param-name>
            
    <param-value>classpath*:/applicationContext*.xml</param-value>
        
    </context-param>

        
    <!-- Character Encoding filter -->
        
    <filter>
            
    <filter-name>encodingFilter</filter-name>
            
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            
    <init-param>
                
    <param-name>encoding</param-name>
                
    <param-value>UTF-8</param-value>
            
    </init-param>
            
    <init-param>
                
    <param-name>forceEncoding</param-name>
                
    <param-value>true</param-value>
            
    </init-param>
        
    </filter>

        
    <filter>
            
    <filter-name>hibernateOpenSessionInViewFilterContent</filter-name>
            
    <filter-class>org.springside.modules.orm.hibernate.OpenSessionInViewFilter</filter-class>
            
    <init-param>
                
    <param-name>excludeSuffixs</param-name>
                
    <param-value>js,css,jpg,gif</param-value>
            
    </init-param>
            
    <init-param>      
                   
    <param-name>sessionFactoryBeanName</param-name>
                
    <param-value>sessionFactoryContent</param-value>   
            
    </init-param>    
        
    </filter>
        
    <filter>
            
    <filter-name>hibernateOpenSessionInViewFilterIndex</filter-name>
            
    <filter-class>org.springside.modules.orm.hibernate.OpenSessionInViewFilter</filter-class>
            
    <init-param>
                
    <param-name>excludeSuffixs</param-name>
                
    <param-value>js,css,jpg,gif</param-value>
            
    </init-param>
            
    <init-param>      
                   
    <param-name>sessionFactoryBeanName</param-name>
                
    <param-value>sessionFactoryIndex</param-value>   
            
    </init-param>    
        
    </filter>
        
    <!-- SpringSecurity filter-->
        
    <filter>
            
    <filter-name>springSecurityFilterChain</filter-name>
            
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        
    </filter>

        
    <!-- Struts2 filter -->
        
    <filter>
            
    <filter-name>struts2Filter</filter-name>
            
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        
    </filter>

        
    <filter-mapping>
            
    <filter-name>encodingFilter</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>


        
    <filter-mapping>
            
    <filter-name>springSecurityFilterChain</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>
        
    <filter-mapping>
            
    <filter-name>hibernateOpenSessionInViewFilterContent</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>
        
    <filter-mapping>
            
    <filter-name>hibernateOpenSessionInViewFilterIndex</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>
        
    <filter-mapping>
            
    <filter-name>struts2Filter</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>

        
    <!--Spring的ApplicationContext 載入 -->
        
    <listener>
            
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        
    </listener>

        
    <!-- Spring 刷新Introspector防止內(nèi)存泄露 -->
        
    <listener>
            
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
        
    </listener>

        
    <!-- session超時(shí)定義,單位為分鐘 -->
        
    <session-config>
            
    <session-timeout>20</session-timeout>
        
    </session-config>

        
    <!-- 出錯(cuò)頁(yè)面定義 -->
        
    <error-page>
            
    <exception-type>java.lang.Throwable</exception-type>
            
    <location>/common/500.jsp</location>
        
    </error-page>
        
    <error-page>
            
    <error-code>500</error-code>
            
    <location>/common/500.jsp</location>
        
    </error-page>
        
    <error-page>
            
    <error-code>404</error-code>
            
    <location>/common/404.jsp</location>
        
    </error-page>
        
    <error-page>
            
    <error-code>403</error-code>
            
    <location>/common/403.jsp</location>
        
    </error-page>
    </web-app>

    5、由于項(xiàng)目中有多個(gè)SessionFactory,所以編寫Dao層的時(shí)候需要使用@Resource注解來(lái)明確指定使用哪一個(gè)SessionFactory,如下面代碼所示,ArticleDao使用sessionFactoryContent,而ArticleIndexDao使用sessionFactoryIndex:
    package cn.puretext.dao;

    import javax.annotation.Resource;

    import org.hibernate.SessionFactory;
    import org.springframework.stereotype.Repository;
    import org.springside.modules.orm.hibernate.HibernateDao;

    import cn.puretext.entity.web.Article;

    @Repository
    public class ArticleDao extends HibernateDao<Article, Long> {

        @Override
        @Resource(name 
    = "sessionFactoryContent")
        
    public void setSessionFactory(SessionFactory sessionFactory) {
            
    // TODO Auto-generated method stub
            super.setSessionFactory(sessionFactory);
        }

    }

    package cn.puretext.dao;

    import javax.annotation.Resource;

    import org.hibernate.SessionFactory;
    import org.springframework.stereotype.Repository;
    import org.springside.modules.orm.hibernate.HibernateDao;

    import cn.puretext.entity.web.ArticleIndex;

    @Repository
    public class ArticleIndexDao extends HibernateDao<ArticleIndex, Long> {
        @Override
        @Resource(name 
    = "sessionFactoryIndex")
        
    public void setSessionFactory(SessionFactory sessionFactory) {
            
    // TODO Auto-generated method stub
            super.setSessionFactory(sessionFactory);
        }
    }

    6、在GlassFish中部署項(xiàng)目,部署項(xiàng)目的時(shí)候依然使用前面提到的GlassFish的管理界面,這里不贅述。

    經(jīng)過(guò)以上六步,就可以成功的在基于SpringSide 3的項(xiàng)目中使用多個(gè)數(shù)據(jù)庫(kù)。如果你確實(shí)很不相使用GlassFish,而對(duì)Tomcat情有獨(dú)鐘的話,就要使用我前面提到的“提供JTA功能的其它組件”了。在這里,我推薦使用Atomikos,這是一個(gè)很優(yōu)秀的JTA實(shí)現(xiàn),它的官方網(wǎng)站為www.atomikos.com,它提供開(kāi)源版和商業(yè)版,下面是從其官方網(wǎng)站上截取的圖片:


    很煩人的是,該網(wǎng)站不直接提供下載地址,如果要下載,就必須先填寫姓名郵箱和電話,如果大家不想填寫這些信息,可以直接進(jìn)入這個(gè)網(wǎng)址下載http://www.atomikos.com/Main/InstallingTransactionsEssentials,我選擇的是3.5.5版。

    方法二、使用Tomcat服務(wù)器和Atomikos

    1、將Atomikos整合到Tomcat服務(wù)器中,其步驟可以參考Atomikos的文檔,如下:
    http://www.atomikos.com/Documentation/Tomcat6Integration33

    2、在Tomcat中配置JNDI數(shù)據(jù)源,方法是修改Tomcat的content.xml文件,在文件中加入如下兩個(gè)<Resource/>和一個(gè)<Transaction/>:

     

        <Transaction factory="com.atomikos.icatch.jta.UserTransactionFactory" />

        
    <Resource name="jdbc/dataSourceContent" auth="Container"
            type
    ="com.atomikos.jdbc.AtomikosDataSourceBean" factory="com.atomikos.tomcat.BeanFactory"
            uniqueResourceName
    ="jdbc/myDB" xaDataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"
            xaProperties.databaseName
    ="puretext" xaProperties.serverName="localhost"
            xaProperties.port
    ="3306" xaProperties.user="USER"
            xaProperties.password
    ="PASSWORD" xaProperties.url="jdbc:mysql://localhost:3306/puretext" />
        
    <Resource name="jdbc/dataSourceIndex" auth="Container"
            type
    ="com.atomikos.jdbc.AtomikosDataSourceBean" factory="com.atomikos.tomcat.BeanFactory"
            uniqueResourceName
    ="jdbc/myDB" xaDataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"
            xaProperties.databaseName
    ="puretext_index" xaProperties.serverName="localhost"
            xaProperties.port
    ="3306" xaProperties.user="USER"
            xaProperties.password
    ="PASSWORD" xaProperties.url="jdbc:mysql://localhost:3306/puretext_index" />

    剩下的四步就和使用GlassFish的第3、4、5、6步一模一樣了,這里不贅述。

    以上Atomikos和Tomcat的整合方案有時(shí)候或多或少出現(xiàn)一點(diǎn)問(wèn)題,這些問(wèn)題基本上都和JNDI有關(guān),我想可能是Tomcat實(shí)現(xiàn)的JNDI配置有問(wèn)題。如果出現(xiàn)這樣的問(wèn)題無(wú)法解決的話,還有第三種方案,那就是直接在Spring的配置文件中配置Atomikos的JTA相關(guān)組件。

    方法三、直接在Spring的配置文件中配置Atomikos的JTA相關(guān)組件

    1、將下載的Atomikos中的jta.properties拷貝到項(xiàng)目的classpath中,將Atomikos的相關(guān)jar文件拷貝到項(xiàng)目的classpath中。

    2、在項(xiàng)目的applicationContext.xml文件中配置JTA的相關(guān)組件,配置文件如下:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jee
    ="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context
    ="http://www.springframework.org/schema/context"
        xsi:schemaLocation
    ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
        default-lazy-init
    ="true">

        
    <description>Spring公共配置文件 </description>

        
    <!-- 定義受環(huán)境影響易變的變量 -->
        
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
            
    <property name="ignoreResourceNotFound" value="true" />
            
    <property name="locations">
                
    <list>
                    
    <!-- 標(biāo)準(zhǔn)配置 -->
                    
    <value>classpath*:/application.properties</value>
                
    </list>
            
    </property>
        
    </bean>

        
    <!-- 使用annotation 自動(dòng)注冊(cè)bean,并保證@Required,@Autowired的屬性被注入 -->
        
    <context:component-scan base-package="cn.puretext" />

        
    <bean id="dataSourceContent" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">      
            
    <property name="uniqueResourceName">      
                
    <value>jdbc/dataSourceContent</value>      
            
    </property>      
            
    <property name="xaDataSourceClassName">      
                
    <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>      
            
    </property>      
            
    <property name="xaProperties">      
                
    <props>    
                    
    <prop key="serverName">localhost</prop>    
                    
    <prop key="portNumber">3306</prop>    
                    
    <prop key="databaseName">puretext</prop>    
                    
    <prop key="user">***</prop>    
                    
    <prop key="password">***</prop>    
                
    </props>          
            
    </property>          
            
    <property name="poolSize">      
                
    <value>3</value>      
            
    </property>       
        
    </bean>
        
    <bean id="dataSourceIndex" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">      
            
    <property name="uniqueResourceName">      
                
    <value>jdbc/dataSourceIndex</value>      
            
    </property>      
            
    <property name="xaDataSourceClassName">      
                
    <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>      
            
    </property>      
            
    <property name="xaProperties">      
                
    <props>    
                    
    <prop key="serverName">localhost</prop>    
                    
    <prop key="portNumber">3306</prop>    
                    
    <prop key="databaseName">puretext_index</prop>    
                    
    <prop key="user">***</prop>    
                    
    <prop key="password">***</prop>    
                
    </props>     
            
    </property>           
            
    <property name="poolSize">      
                
    <value>3</value>      
            
    </property>         
        
    </bean>


        
    <!-- Hibernate配置 -->
        
    <bean id="sessionFactoryContent" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            
    <property name="dataSource" ref="dataSourceContent" />
            
    <property name="namingStrategy">
                
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
            
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
                    
    </prop>
                    
    <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                
    </props>
            
    </property>
            
    <property name="packagesToScan" value="cn.puretext.entity.*" />
        
    </bean>
        
    <bean id="sessionFactoryIndex" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            
    <property name="dataSource" ref="dataSourceIndex" />
            
    <property name="namingStrategy">
                
    <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
            
    </property>
            
    <property name="hibernateProperties">
                
    <props>
                    
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                    
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                    
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                    
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
                    
    </prop>
                    
    <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                
    </props>
            
    </property>
            
    <property name="packagesToScan" value="cn.puretext.entity.*" />
        
    </bean>
        
        
    <!-- 事務(wù)管理器配置,多數(shù)據(jù)源JTA事務(wù)-->
         
    <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">   
            
    <property name="forceShutdown"><value>true</value></property>   
        
    </bean>   
           
        
    <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">   
            
    <property name="transactionTimeout" value="300"/>    
        
    </bean>   
        
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
            
    <property name="transactionManager" ref="atomikosTransactionManager" />
            
    <property name="userTransaction" ref="atomikosUserTransaction"/>
        
    </bean>
        
        
    <!-- 使用annotation定義事務(wù) -->
        
    <tx:annotation-driven transaction-manager="transactionManager" />
     
    </beans>


    3、在web.xml中配置多個(gè)OpenSessionInViewFilter,其配置方法同前。

    4、在Dao類中使用@Resource指定使用哪一個(gè)sessionFactory。

    5、運(yùn)行項(xiàng)目,成功。

    在以上的三個(gè)方法中,我強(qiáng)烈推薦第三種,因?yàn)樵摲椒ㄖ恍枰獙tomikos的相關(guān)文件拷貝到項(xiàng)目的classpath中,并在applicationContext.xml文件中完成配置即可,不需要修改應(yīng)用服務(wù)器的任何文件,是非侵入性的,是最輕量級(jí)的,同時(shí),也是配置起來(lái)最容易成功的,在我的測(cè)試過(guò)程中基本上是一次成功,沒(méi)有報(bào)錯(cuò)。

    好了,就寫到這里了,希望SpringSide的fans們少走彎路,天天開(kāi)心。


    評(píng)論

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-07-17 09:35 by qzoo
    不知道前輩能不能幫忙解答一下這個(gè)問(wèn)題,萬(wàn)分感謝。
    http://forum.springside.org.cn/viewthread.php?tid=3850

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-07-21 16:30 by 虎嘯龍吟
    把源碼提供給我,好?

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-07-21 21:14 by 海邊沫沫
    @虎嘯龍吟
    過(guò)兩天我會(huì)提供下載

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-07-25 19:36 by 虎嘯龍吟
    博主:
    如果有三個(gè)或三個(gè)以上的數(shù)據(jù)源怎么辦?
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager" ref="atomikosTransactionManager" />
    <property name="userTransaction" ref="atomikosUserTransaction"/>
    </bean>
    怎么寫?

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法[未登錄](méi)  回復(fù)  更多評(píng)論   

    2009-07-25 20:21 by 海邊沫沫
    這幾行不需要任何變動(dòng),只需要多配置幾個(gè)數(shù)據(jù)源就可以了。

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-07-29 11:16 by xfan
    為什么不用虛擬數(shù)據(jù)源

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-07-29 18:18 by 海邊沫沫
    @xfan
    虛擬數(shù)據(jù)源也不錯(cuò),不過(guò)我是最近看BlogJava上的一篇文章(http://www.tkk7.com/Werther/archive/2009/07/27/288643.html)才知道有這個(gè)東西。
    所以說(shuō)我要學(xué)習(xí)的東西還有很多。

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-08-26 19:20 by 石上清泉
    請(qǐng)問(wèn)博主,按照方法3的配置,是否依賴于web服務(wù)器,我用3的配置,然后做測(cè)試,總是提示Could not find UserTransaction in JNDI,請(qǐng)問(wèn)用atomikos,怎么才能不依賴web容器運(yùn)行?謝謝。

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-08-27 19:13 by 海邊沫沫
    @石上清泉
    使用方法三,是不需要和JNDI打交道的,dataSource、userTransaction和transactionManager都是在Spring的配置文件中配置的,本來(lái)就不依賴于服務(wù)器。

    方法二倒是老出錯(cuò)。

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2009-09-20 19:36 by sw
    分布式事物的意義不大,都是盡量避免跨DB的事務(wù)。

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2010-07-15 11:46 by tong
    請(qǐng)教:我使用第三種方法配成功也能正常回滾,但是自定義主鍵(@TableGenerator)不能用了。

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法[未登錄](méi)  回復(fù)  更多評(píng)論   

    2010-10-14 10:01 by 小馬
    使用方法3成功了,正好解決我手頭上的問(wèn)題....多謝博主分享..

    # re: 在SpringSide 3 中使用多個(gè)數(shù)據(jù)庫(kù)的方法  回復(fù)  更多評(píng)論   

    2012-06-26 16:37 by 小趙
    多謝樓主分享,我現(xiàn)在按方法三配置數(shù)據(jù)源跟事務(wù),查詢沒(méi)問(wèn)題,但更新一直更新不了,但是能出SQL語(yǔ)句,想請(qǐng)教會(huì)是什么問(wèn)題,是不是配置的事務(wù)沒(méi)有生效啊?
    主站蜘蛛池模板: 国产色爽免费视频| 又长又大又粗又硬3p免费视频| 日本特黄特色AAA大片免费| 久久综合国产乱子伦精品免费| 日韩免费一区二区三区| 久久精品国产亚洲77777| 成人无码a级毛片免费| 亚洲一级片免费看| 毛片在线看免费版| 久久亚洲精品成人| gogo免费在线观看| 免费无码又爽又刺激毛片| 亚洲高清资源在线观看| 二个人看的www免费视频| 免费**毛片在线播放直播| 亚洲国产日韩在线| 久久青草国产免费观看| 亚洲欧洲中文日韩av乱码| 亚洲成a人片在线观看无码| 在线观看亚洲免费视频| 久久99九九国产免费看小说| 四虎影院免费视频| 亚洲最大的成网4438| 中国性猛交xxxxx免费看| 亚洲av再在线观看| 亚洲AV无码国产剧情| 91精品视频免费| 亚洲黄色片在线观看| 精品熟女少妇aⅴ免费久久| 全部免费a级毛片| 亚洲中文字幕乱码一区| 波多野结衣中文字幕免费视频| 老司机亚洲精品影院| 两个人www免费高清视频| 亚洲爽爽一区二区三区| 亚洲成av人影院| 色在线亚洲视频www| 最近中文字幕免费完整| 久久久久亚洲AV成人无码网站 | 国产日产亚洲系列| 亚洲AV无码一区二区大桥未久|