京山游俠

          專注技術,拒絕扯淡
          posts - 50, comments - 868, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          搜索

          •  

          積分與排名

          • 積分 - 658186
          • 排名 - 73

          最新評論

          在SpringSide 3社區中,不斷有人提出多數據源配置的問題,但是時至今日卻一直沒有一個完美的答案。經過一個星期的折騰,我總算搞清楚了在SpringSide 3中配置多數據源的各種困難并加以解決,在這里,特地把我配置SpringSide 3項目中多數據源的過程寫出來,與大家分享。

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

          下面進入正題:

          結論:在基于SpringSide 3的項目中,如果要使用多個數據庫,首先要配置多個數據源,然后配置多個SessionFactory,這本身沒有問題,但是一涉及到事務,問題就來了,在多數據源的環境下,必須使用JTATransactionManager,而使用JTATransactionManager,就必須得有提供JTA功能的應用服務器或提供JTA功能的別的什么組件。

          以上結論絕對正確,是屬于SpringSide 3中關于使用多個數據庫的最權威解答,下面來看具體過程:

          方法一、使用GlassFish應用服務器

          1、準備GlassFish服務器,下載地址為http://download.java.net/glassfish/v3/promoted/,我選擇的是08-Jul-2009 17:20發布的大小為72M的latest-glassfish.zip,這里需要強調的一點是千萬不要選擇latest-glassfish-windows.exe這個版本,因為這個版本在Windows環境中只安裝GlassFish而不提供合理的初始化配置,對于新手來說使用門檻太高,而ZIP版一解壓縮就可以使用,其服務器是配置好了的;

          2、在GlassFish中配置多個數據源,啟動GlassFish后,訪問4848端口就可以進入到GlassFish的管理界面,在其中配置兩個數據源,其資源名稱分別為jdbc/dataSourceContent和jdbc/dataSourceIndex,如下圖:


          3、在項目中配置多個DataSource和多個SessionFactory,并選擇JTATransactionManager作為事務管理器,這里的DataSource是使用JNDI查找從應用服務器中獲得的。下面是我項目中的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>

              
          <!-- 定義受環境影響易變的變量 -->
              
          <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>
                          
          <!-- 標準配置 -->
                          
          <value>classpath*:/application.properties</value>
                      
          </list>
                  
          </property>
              
          </bean>

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

              
          <!-- 數據源配置,使用應用服務器的數據庫連接池 -->
              
          <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>
              
              
          <!-- 事務管理器配置,多數據源JTA事務-->
              
          <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
              
              
          <!-- 使用annotation定義事務 -->
              
          <tx:annotation-driven transaction-manager="transactionManager" />
           
          </beans>

          4、由于配置了多個SessionFactory,所以需要在web.xml中配置兩個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配置文件的路徑,可使用通配符,多個路徑用,號分隔
                  此參數用于后面的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防止內存泄露 -->
              
          <listener>
                  
          <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
              
          </listener>

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

              
          <!-- 出錯頁面定義 -->
              
          <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、由于項目中有多個SessionFactory,所以編寫Dao層的時候需要使用@Resource注解來明確指定使用哪一個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中部署項目,部署項目的時候依然使用前面提到的GlassFish的管理界面,這里不贅述。

          經過以上六步,就可以成功的在基于SpringSide 3的項目中使用多個數據庫。如果你確實很不相使用GlassFish,而對Tomcat情有獨鐘的話,就要使用我前面提到的“提供JTA功能的其它組件”了。在這里,我推薦使用Atomikos,這是一個很優秀的JTA實現,它的官方網站為www.atomikos.com,它提供開源版和商業版,下面是從其官方網站上截取的圖片:


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

          方法二、使用Tomcat服務器和Atomikos

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

          2、在Tomcat中配置JNDI數據源,方法是修改Tomcat的content.xml文件,在文件中加入如下兩個<Resource/>和一個<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的整合方案有時候或多或少出現一點問題,這些問題基本上都和JNDI有關,我想可能是Tomcat實現的JNDI配置有問題。如果出現這樣的問題無法解決的話,還有第三種方案,那就是直接在Spring的配置文件中配置Atomikos的JTA相關組件。

          方法三、直接在Spring的配置文件中配置Atomikos的JTA相關組件

          1、將下載的Atomikos中的jta.properties拷貝到項目的classpath中,將Atomikos的相關jar文件拷貝到項目的classpath中。

          2、在項目的applicationContext.xml文件中配置JTA的相關組件,配置文件如下:

           

          <?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>

              
          <!-- 定義受環境影響易變的變量 -->
              
          <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>
                          
          <!-- 標準配置 -->
                          
          <value>classpath*:/application.properties</value>
                      
          </list>
                  
          </property>
              
          </bean>

              
          <!-- 使用annotation 自動注冊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>
              
              
          <!-- 事務管理器配置,多數據源JTA事務-->
               
          <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定義事務 -->
              
          <tx:annotation-driven transaction-manager="transactionManager" />
           
          </beans>


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

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

          5、運行項目,成功。

          在以上的三個方法中,我強烈推薦第三種,因為該方法只需要將Atomikos的相關文件拷貝到項目的classpath中,并在applicationContext.xml文件中完成配置即可,不需要修改應用服務器的任何文件,是非侵入性的,是最輕量級的,同時,也是配置起來最容易成功的,在我的測試過程中基本上是一次成功,沒有報錯。

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


          評論

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法[未登錄]  回復  更多評論   

          2009-07-25 20:21 by 海邊沫沫
          這幾行不需要任何變動,只需要多配置幾個數據源就可以了。

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

          2009-07-29 11:16 by xfan
          為什么不用虛擬數據源

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          方法二倒是老出錯。

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法[未登錄]  回復  更多評論   

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

          # re: 在SpringSide 3 中使用多個數據庫的方法  回復  更多評論   

          2012-06-26 16:37 by 小趙
          多謝樓主分享,我現在按方法三配置數據源跟事務,查詢沒問題,但更新一直更新不了,但是能出SQL語句,想請教會是什么問題,是不是配置的事務沒有生效啊?
          主站蜘蛛池模板: 昌邑市| 平利县| 琼结县| 靖安县| 柳江县| 治多县| 万源市| 梅河口市| 赣州市| 九寨沟县| 崇明县| 永登县| 鄂尔多斯市| 大英县| 平远县| 舒兰市| 温州市| 蒙阴县| 渝北区| 伊吾县| 曲阜市| 东方市| 水富县| 曲松县| 武隆县| 邹城市| 沭阳县| 开化县| 图木舒克市| 乐昌市| 精河县| 辉县市| 柘荣县| 育儿| 乡城县| 汤原县| 龙口市| 博乐市| 中超| 齐齐哈尔市| 吴忠市|