在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)心。