Using multiple databases in Springside 2.0

Source: Internet
Author: User

In order to improve the future of the site to expand the ability, I was using the data segmentation architecture, which requires my website to be flexible access to multiple databases, if it is direct operation of JDBC, this is not a problem. However, in SpringSide2.0, because of the integration of Hibernate, access to data is most often achieved through configuration.

The easiest way to use multiple databases in a SpringSide2.0 is to configure multiple sessionfactory, and here's my step.

First, to modify the Jdbc.properties file in the Src/resources/config directory, define the connections for multiple databases, as follows:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.urlYumDaysIndex=jdbc:mysql://localhost:3306/YumDaysIndex?useUnicode=true&characterEncoding=utf8
jdbc.urlYumDays001=jdbc:mysql://localhost:3306/YumDays001?useUnicode=true&characterEncoding=utf8

Then, modify the Dataaccesscontext-hibernate.xml file in the Src/resources/spring directory to define multiple data sources, which have only different URL attributes:

<!--data source definitions, use the Apache DBCP connection pool;
<bean id= "Datasourceyumdaysindex class=" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" "Close"
<property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
<property name= "url" value= "${jdbc.urlyumdaysindex}"/>
<property    Name= "username" value= "${jdbc.username}"/>
<property name= "password" value= "${jdbc.password}"/>
</bean>
<!--data source definition, use Apache DBCP connection pool;
<bean id= "dataSourceYumDays001" class= "org.apache.co" Mmons.dbcp.BasicDataSource "destroy-method=" Close "
<property name=" Driverclassname "value=" ${ Jdbc.driverclassname} "/>
<property name=" url "value=" ${jdbc.urlyumdays001} "/>
<property Name = "username" value= "${jdbc.username}"/>
<property name= "password" value= "${jdbc.password}"/>
</ Bean>

Then, or modify the file, define multiple sessionfactory, which differ only by the properties of the data source:

&lt;!--Hibernate sessionfatory--&gt;


&lt;bean id= "Sessionfactoryindex" class= " Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "&gt;


&lt;property name= "DataSource" ref= datasourceyumdaysindex "/&gt;"


&lt;property name= "annotatedclasses" &gt;


&lt;list&gt;


&lt;value&gt;com.yumdays.model.Catalog&lt;/value&gt;


&lt;value&gt;com.yumdays.model.Statistic&lt;/value&gt;


&lt;/list&gt;


&lt;/property&gt;


&lt;property name= "Hibernateproperties" &gt;


&lt;props&gt;


&lt;!--see/components/orm/hibernate/meta/dialect.properties for all dialect--&gt;


&lt;prop key= "Hibernate.dialect" &gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;


&lt;prop key= "Hibernate.show_sql" &gt;true&lt;/prop&gt;


&lt;prop key= "Hibernate.cache.provider_class" &gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;


&lt;prop key= "Hibernate.cache.use_query_cache" &gt;true&lt;/prop&gt;


&lt;/props&gt;


&lt;/property&gt;


&lt;/bean&gt;


&lt;!--Hibernate sessionfatory--&gt;


&lt;bean id= "sessionFactory001" class= " Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "&gt;


&lt;property name= "DataSource" ref= dataSourceYumDays001 "/&gt;"


&lt;property name= "annotatedclasses" &gt;


&lt;list&gt;


&lt;/list&gt;


&lt;/property&gt;


&lt;property name= "Hibernateproperties" &gt;


&lt;props&gt;


&lt;!--see/components/orm/hibernate/meta/dialect.properties for all dialect--&gt;


&lt;prop key= "Hibernate.dialect" &gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;


&lt;prop key= "Hibernate.show_sql" &gt;true&lt;/prop&gt;


&lt;prop key= "Hibernate.cache.provider_class" &gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;


&lt;prop key= "Hibernate.cache.use_query_cache" &gt;true&lt;/prop&gt;


&lt;/props&gt;


&lt;/property&gt;


&lt;/bean&gt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.