莎士比亚写女人:在SpringSide 3 中使用多个数据库的方法
来源:百度文库 编辑:偶看新闻 时间:2024/05/16 11:01:40
在SpringSide 3社区中,不断有人提出多数据源配置的问题,但是时至今日却一直没有一个完美的答案。经过一个星期的折腾,我总算搞清楚了在SpringSide 3中配置多数据源的各种困难并加以解决,在这里,特地把我配置SpringSide 3项目中多数据源的过程写出来,与大家分享。
我使用的SpringSide的版本是江南白衣最新发布的3.1.4翻墙版,在上一篇博文中,记录了我折腾的全过程,感兴趣的朋友可以看看:
http://www.blogjava.net/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文件:
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">
Spring公共配置文件
org.hibernate.dialect.MySQL5InnoDBDialect
${hibernate.show_sql}
${hibernate.format_sql}
org.hibernate.cache.EhCacheProvider
${hibernate.ehcache_config_file}
org.hibernate.dialect.MySQL5InnoDBDialect
${hibernate.show_sql}
${hibernate.format_sql}
org.hibernate.cache.EhCacheProvider
${hibernate.ehcache_config_file}
4、由于配置了多个SessionFactory,所以需要在web.xml中配置两个OpenSessionInViewFilter,下面是我的web.xml文件:
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
PureText
contextConfigLocation
classpath*:/applicationContext*.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
hibernateOpenSessionInViewFilterContent
org.springside.modules.orm.hibernate.OpenSessionInViewFilter
excludeSuffixs
js,css,jpg,gif
sessionFactoryBeanName
sessionFactoryContent
hibernateOpenSessionInViewFilterIndex
org.springside.modules.orm.hibernate.OpenSessionInViewFilter
excludeSuffixs
js,css,jpg,gif
sessionFactoryBeanName
sessionFactoryIndex
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
struts2Filter
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
encodingFilter
/*
springSecurityFilterChain
/*
hibernateOpenSessionInViewFilterContent
/*
hibernateOpenSessionInViewFilterIndex
/*
struts2Filter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
20
java.lang.Throwable
/common/500.jsp
500
/common/500.jsp
404
/common/404.jsp
403
/common/403.jsp
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 {
@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 {
@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文件,在文件中加入如下两个 和一个 :
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" />
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" />
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">
Spring公共配置文件
jdbc/dataSourceContent
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
localhost
3306
puretext
***
***
3
jdbc/dataSourceIndex
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
localhost
3306
puretext_index
***
***
3
org.hibernate.dialect.MySQL5InnoDBDialect
${hibernate.show_sql}
${hibernate.format_sql}
org.hibernate.cache.EhCacheProvider
${hibernate.ehcache_config_file}
org.hibernate.dialect.MySQL5InnoDBDialect
${hibernate.show_sql}
${hibernate.format_sql}
org.hibernate.cache.EhCacheProvider
${hibernate.ehcache_config_file}
true
我使用的SpringSide的版本是江南白衣最新发布的3.1.4翻墙版,在上一篇博文中,记录了我折腾的全过程,感兴趣的朋友可以看看:
http://www.blogjava.net/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文件:
4、由于配置了多个SessionFactory,所以需要在web.xml中配置两个OpenSessionInViewFilter,下面是我的web.xml文件:
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 {
@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 {
@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文件,在文件中加入如下两个
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" />
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的相关组件,配置文件如下:
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">
3、在web.xml中配置多个OpenSessionInViewFilter,其配置方法同前。
4、在Dao类中使用@Resource指定使用哪一个sessionFactory。
5、运行项目,成功。
在以上的三个方法中,我强烈推荐第三种,因为该方法只需要将Atomikos的相关文件拷贝到项目的classpath中,并在applicationContext.xml文件中完成配置即可,不需要修改应用服务器的任何文件,是非侵入性的,是最轻量级的,同时,也是配置起来最容易成功的,在我的测试过程中基本上是一次成功,没有报错。
好了,就写到这里了,希望SpringSide的fans们少走弯路,天天开心。
在Excel工作表中,要计算区域A1:C5中值大于等于200的单元格个数,应使用公式()。
在c#的textBox1控件中输入任意多个数用“-”隔开,怎样比较任意多个数的大小并显示在textBox2控件中
3次在0到9中随机抽取1个数~这3个数在1到7之间且无重复的概率是多少~
3次在0到9中随机抽取1个数~这3个数在1到7之间且无重复的概率是多少~
在VF中当文件个数超过3个时候怎么建立查询文件?
从1-8个数中选择4个数,在从0-9当中选择一个数
在BCB中如何统计一个文本文件的字符个数?
在计算机中,一个字节所包含二进制位的个数
如何在FrontPage中限制文本域的文字个数
在asp中实现选择个数的设置
一道数学题:在任意五个整数中,一定有3个数,它们的和可被3整除,为什么?
在2,3,5,9,17,33,65……这组有规律排列的数中,第八个数是
在1.2.3,,,,,,.1000这1000个数中,既能被5整除,又能被7整除的数有( ).
求8个数的平均值,这8个数以表格的形式存放在从TABLE开始的单元中.
在VB中如何编写“从8个数内取出任意个数来按小到大组合,并存入数组”
在c#的textBox1控件中输入数用“-”隔开,怎样比较任意多个数的大小并显示在textBox2控件中
在8~36之间插3个数,使它们成为等差数列,这3个数分别是多少?
在EXCEL中如何在所有日期中求出某个年代的日期个数
请问在vb中怎么在文本中输入一组数组,并不知道数组个数,
数组元素可以是一固定个数的变量吗?如何定义和在函数使用?
在excel中,如何计算一列表格中某字符的个数?
有5个数,平均数是15,,前面3个数的和是26,末尾三个数之和是68,这5个数中中间的那个数字是多少?
有5个数,平均数是16,,把他们从小到大排列起来,前3个数之和为22,末尾3个数之和为73,那这5个数中中?
如果个3数中每2个数之和为36,51,29,求这3个数.