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
- # # MySQL Local
- Jdbc.driverclassname=com.mysql.jdbc.driver
- Jdbc.url= jdbc:mysql://localhost:3306/Database name?createdatabaseifnotexist=true&useunicode=true& Characterencoding=utf-8&zerodatetimebehavior=converttonull
- Jdbc.username=root
- Jdbc.password=root
- # # MYSQL Server
- Jdbc_t.driverclassname=com.mysql.jdbc.driver
- 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
- Jdbc_t.username=root
- 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
- <!--Configure the data source with the DBCP database connection pool--
- <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
- <property name= "Driverclassname" value= "${jdbc.driverclassname}"/>
- <property name= "url" value= "${jdbc.url}"/>
- <property name= "username" value= "${jdbc.username}"/>
- <property name= "Password" value= "${jdbc.password}"/>
- <property name= "maxactive" value= "/>"
- <property name= "maxwait" value= "/>"
- <property name= "Poolpreparedstatements" value= "true"/>
- <property name= "Defaultautocommit" value= "true"/>
- <property name= "removeabandoned" value= "true"/>
- <property name= "logabandoned" value= "true"/>
- <property name= "Minevictableidletimemillis" value= "ten"/>
- <property name= "Removeabandonedtimeout" value= "5"/>
- </bean>
- <bean id= "datasource_t" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
- <property name= "Driverclassname" value= "${jdbc_t.driverclassname}"/>
- <property name= "url" value= "${jdbc_t.url}"/>
- <property name= "username" value= "${jdbc_t.username}"/>
- <property name= "Password" value= "${jdbc_t.password}"/>
- <property name= "maxactive" value= "/>"
- <property name= "maxwait" value= "/>"
- <property name= "Poolpreparedstatements" value= "true"/>
- <property name= "Defaultautocommit" value= "true"/>
- <property name= "removeabandoned" value= "true"/>
- <property name= "logabandoned" value= "true"/>
- <property name= "Minevictableidletimemillis" value= "ten"/>
- <property name= "Removeabandonedtimeout" value= "5"/>
- </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
- <bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
- <property name= "DataSource" ref= "DataSource"/>
- <property name= "Configlocations" >
- <list>
- <value>classpath:hibernate.cfg.xml</value>
- </list>
- </property>
- <bean id= "sessionfactory_t" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">
- <property name= "DataSource" ref= "datasource_t"/>
- <property name= "Configlocations" >
- <list>
- <value>classpath:hibernate_t.cfg.xml</value>
- </list>
- </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