Spring+hibernate Configuring multiple data sources

Source: Internet
Author: User

Configuration Instructions

In practical applications, read and write separations are often used, and this is where the Spring+hibernate multi-data source is configured. The configuration here only allows the Read method to manipulate a database, and write a method to manipulate the other database.

Note: My configuration of the JDBC connection here is placed in the properties file, of course you can also write directly in the Applicationcpntext.xml file or other configuration method.

Configuring step 1, configuring multiple databases

In JDBC. Properties, configure a connection for two databases. Here are the examples below (I am using MySQL here, if you want to use the other one please replace the driver):

[Java]View Plaincopy
    1. # # MySQL Local
    2. Jdbc.driverclassname=com.mysql.jdbc.driver
    3. Jdbc.url= jdbc:mysql://localhost:3306/Database name?createdatabaseifnotexist=true&useunicode=true& Characterencoding=utf-8&zerodatetimebehavior=converttonull
    4. Jdbc.username=root
    5. Jdbc.password=root
    6. # # MYSQL Server
    7. Jdbc_t.driverclassname=com.mysql.jdbc.driver
    8. Jdbc_t.url= jdbc:mysql://Server ip:3306/database name (may not be the same as above)?createdatabaseifnotexist=true&useunicode=true& Characterencoding=utf-8&zerodatetimebehavior=converttonull
    9. Jdbc_t.username=root
    10. jdbc_t.password= Root

Note: Jdbc.driverclassname and JDBC_ T.driverclassname can use Jdbc.driverclassname, if the use of one is in the back of the applicationcontext.xml configuration of the same can be, here to facilitate the description, separate write. Similarly, if the user name and password are the same, you can use just one configuration.

2, Configuration DataSource

The DBCP connection pool is used here.

[XHTML]View Plaincopy
  1. <!--Configure the data source with the DBCP database connection pool--
  2. <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
  3. <property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
  4. <property name= "url" value= "${jdbc.url}"/>
  5. <property name= "username" value= "${jdbc.username}"/>
  6. <property name= "Password" value= "${jdbc.password}"/>
  7. <property name= "maxactive" value= "/>"
  8. <property name= "maxwait" value= "/>"
  9. <property name= "Poolpreparedstatements" value= "true"/>
  10. <property name= "Defaultautocommit" value= "true"/>
  11. <property name= "removeabandoned" value= "true"/>
  12. <property name= "logabandoned" value= "true"/>
  13. <property name= "Minevictableidletimemillis" value= "ten"/>
  14. <property name= "Removeabandonedtimeout" value= "5"/>
  15. </bean>
  16. <bean id= "datasource_t" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
  17. <property name= "Driverclassname" value= "${jdbc_t.driverclassname}"/>
  18. <property name= "url" value= "${jdbc_t.url}"/>
  19. <property name= "username" value= "${jdbc_t.username}"/>
  20. <property name= "Password" value= "${jdbc_t.password}"/>
  21. <property name= "maxactive" value= "/>"
  22. <property name= "maxwait" value= "/>"
  23. <property name= "Poolpreparedstatements" value= "true"/>
  24. <property name= "Defaultautocommit" value= "true"/>
  25. <property name= "removeabandoned" value= "true"/>
  26. <property name= "logabandoned" value= "true"/>
  27. <property name= "Minevictableidletimemillis" value= "ten"/>
  28. <property name= "Removeabandonedtimeout" value= "5"/>
  29. </bean>

Note: IDs cannot be duplicated, so the latter uses datasource_t and DataSource to differentiate, so that a database can be distinguished using that one. If the above driverclassname configuration is the same, you can use jdbc.driverclassname without configuring Jdbc_t.driverclassname.

3, Configuration Sessionfactory

Please pay attention to the marked red and bold places:

[XHTML]View Plaincopy
  1. <bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
  2. <property name= "DataSource" ref= "DataSource"/>
  3. <property name= "Configlocations" >
  4. <list>
  5. <value>classpath:hibernate.cfg.xml</value>
  6. </list>
  7. </property>
  8. <bean id= "sessionfactory_t" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">
  9. <property name= "DataSource" ref= "datasource_t"/>
  10. <property name= "Configlocations" >
  11. <list>
  12. <value>classpath:hibernate_t.cfg.xml</value>
  13. </list>
  14. </property>

Note: Hibernate.cfg.xml and Hibernate_t.cfg.xml can be based on the actual situation to see if it should be differentiated. You can also share one copy.

4. Configure DAO

When you need to use a local database (not necessarily local for your project), configure the following:

<property name= "Sessionfactory" ref= "Sessionfactory"/>

When you need to use the server's database, configure the following:

<property name= "Sessionfactory" ref= "sessionfactory_t"/>

Of course you can also configure multiple, as needed to configure more.

Spring+hibernate Configuring multiple data sources

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.