Spring 4+ JPA (hibernate 3/4) +spring MVC Multi-Data source configuration (2)

Source: Internet
Author: User

Next to a blog post, there is no database connection pool, purely with the official JPA link.

So this time we're going to add a connection pool. This article uses Druid Connection pool to implement multi-data source configuration.

Persistence.xml This file can be omitted, all configured in 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:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:jpa= "http://www.springframework.org/schema/data/ JPA "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xmlns:tx=" Http://www.springframework.org/schema/tx " Xmlns:util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/ AOP Http://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/mvc HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC/SPRING-MVC-4.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP www.springframework.oRg/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/ UTIL/SPRING-UTIL-4.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/ Data/jpa/spring-jpa-1.2.xsd "><context:annotation-config/><context:component-scan base-package="        com.tw "/><!--mysql Data source configuration--<bean id=" Mysqldatasource "class=" Com.alibaba.druid.pool.DruidDataSource " Init-method= "Init" destroy-method= "Close" > <!--driver name--<property name= "Driverclassname" Val Ue= "Com.mysql.jdbc.Driver"/> <!--JDBC Connection string--<property name= "url" value= "Jdbc:mysql ://192.168.132.1:3306/twq?useunicode=true&amp;characterencoding=utf-8 "/> <!--database user name--< Property name= "username" value= "ws"/> <!--database Password--<property name= "password" value= "Unionmanw S "/> <!--connection pool maximum use of connections-<propertyName= "maxactive" value= "/> <!--Initialize Size--<property name=" InitialSize "value=" 5 "/> <!--get connection maximum waiting time-<property name= "maxwait" value= "60000"/> <!--connection Pool min Idle--&L T;property name= "Minidle" value= "2"/> <!--eviction connection detection Interval--<property name= "Timebetweenevictionr Unsmillis "value="/> <!--minimum eviction time-<property name= "Minevictableidletimemillis" value= "30        0000 "/> <!--test valid SQL Query--<property name=" validationquery "value=" select ' X ' "/> <!--the test is valid when the connection is idle-<property name= "Testwhileidle" value= "true"/> <!--the test is valid when getting the connection--&gt        ; <property name= "Testonborrow" value= "false"/> <!--when the connection is returned test effective--<property name= "Testonret Urn "value=" false "/> </bean> <!--integration MYSQLJPA--<bean id=" mysqlentitymanagerfactory "class=" Org.s Pringframework.Orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name=" dataSource "ref=" Mysqldatasource "></ property> <property name= "Packagestoscan" value= "Com.tw.entity.sys" ></property> <property name= " Persistenceunitname "value=" MySQLdb "></property> <property name=" Jpavendoradapter "> <bean class=" Org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter "> <property name=" Showsql "value=" true ">                </property> </bean> </property> <property name= "Jpaproperties" > <props>                <!--set the maximum depth of the outside connection crawl tree--<prop key= "Hibernate.max_fetch_depth" >3</prop> <prop key= "Hibernate.jdbc.fetch_size" >18</prop> <prop key= "Hibernate.jdbc.batch_size" ;10</prop> <!--auto-build table type Validate|create|create-drop|update-<!--<prop K ey= "Hibernate.hbm2ddl.auto" &GT;VALIDATE&LT;/PROP&GT                --<!--show SQL---<prop key= "Hibernate.show_sql" >false</prop>                <!--Show if SQL is formatted--<prop key= "Hibernate.format_sql" >false</prop> <!--off Level two cache--<prop key= "Hibernate.cache.provider_class" >org.hibernate.cache.nocacheprovider </prop> <!--off entity field Mapping checksum <prop key= "Javax.persistence.validation.mode" >  none</prop> </props> </property> </bean> <bean id= "Mysqltransactionmanager" class= "Org.springframework.orm.jpa.JpaTransactionManager" > <property name= "entitymanagerfactory" ref= " Mysqlentitymanagerfactory "/> <qualifier value=" Mysqlem "/> </bean><tx:annotation-driven Transaction-manager= "Mysqltransactionmanager" proxy-target-class= "false"/> <!--SQL Server data Source configuration--<be An id= "SQLServerDataSource" class= "Com.alibAba.druid.pool.DruidDataSource "init-method=" Init "destroy-method=" Close "> <!--Drive name--&LT        ;p roperty name= "Driverclassname" value= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <!--JDBC Connection string--        <property name= "url" value= "Jdbc:sqlserver://192.168.130.10:1433;databasename=unionman"/> <!--database user name--<property name= "username" value= "sa"/> <!--Database Password---<propert Y name= "password" value= "123abc"/> <!--connection pool maximum usage connection quantity-<property name= "maxactive" value= "20"/        > <!--initialization size-<property name= "InitialSize" value= "5"/> <!--get connection Max wait Time- <property name= "maxwait" value= "60000"/> <!--connection pool min Free--<property name= "Minidle" Val         Ue= "2"/> <!--eviction connection detection Interval--<property name= "Timebetweenevictionrunsmillis" value= "" "/> <!--minimum eviction time-<property name= "Minevictableidletimemillis" value= "300000"/> <!--test valid SQL Query-- <property name= "Validationquery" value= "select ' x '"/> <!--connect when idle test is effective--<property nam E= "Testwhileidle" value= "true"/> <!--get a connection test is valid--<property name= "Testonborrow" value= "Fals    E "/> <!--when the connection is returned test valid-<property name=" Testonreturn "value=" false "/> </bean> <!--integration SQLSERVERJPA--<bean id= "Sqlserverentitymanagerfactory" class= " Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name=" DataSource "ref=" SQLServerDataSource "></property> <property name=" Packagestoscan "value=" Com.tw.entity.plan "></ property> <property name= "Persistenceunitname" value= "Sqlserverdb" ></property> <property name= " Jpavendoradapter "> <bean class=" org.springframework.orm.jpa.vendor.HibernateJpaVeNdoradapter "> <property name=" Showsql "value=" true "></property> </bean> </property> < Property name= "Jpaproperties" > <props> <!--set the maximum depth of the outer-join crawl tree--and <p ROP key= "hibernate.max_fetch_depth" >3</prop> <prop key= "Hibernate.jdbc.fetch_size" &GT;18&LT;/PR op> <prop key= "hibernate.jdbc.batch_size" >10</prop> <!--auto-build table type Validate|c Reate|create-drop|update-<!--<prop key= "Hibernate.hbm2ddl.auto" >validate</prop>--&G                T <!--Show SQL--<prop key= "Hibernate.show_sql" >false</prop> <!--display SQL                 Whether to format-<prop key= "Hibernate.format_sql" >false</prop> <!--turn off level two cache--                <prop key= "Hibernate.cache.provider_class" >org.hibernate.cache.NoCacheProvider</prop> <! --Turn off entity field Mapping checksum <prop key= "Javax.persistence.validation.mode" >none</prop> </ props> </property> </bean> <bean id= "Sqlservertransactionmanager" class= "org.springframework.or M.jpa.jpatransactionmanager "> <property name=" entitymanagerfactory "ref=" sqlserverentitymanagerfactory "/ > <qualifier value= "sqlserverem"/> </bean><tx:annotation-driven transaction-manager= " Sqlservertransactionmanager "proxy-target-class=" false "/></beans>

other places do not need to be changed by the last configuration. That's good.

reprinted from: Http://www.loveweir.com/html/19.html

Spring 4+ JPA (hibernate 3/4) +spring MVC Multi-Data source configuration (2)

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.