Spring 4 + jpa (hibernate 3/4) + spring mvc multi-data source configuration (2) + Druid connection pool, mvcdruid

Source: Internet
Author: User

Spring 4 + jpa (hibernate 3/4) + spring mvc multi-data source configuration (2) + Druid connection pool, mvcdruid

Pick up a blog post (http://www.loveweir.com/html/18.html), no database connection pool, purely using the official link of jpa.

Therefore, we need to add the connection pool. In this article, we use the Druid connection pool to configure multiple data sources.

The persistence. xml 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: u Til = "http://www.springframework.org/schema/util" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http: // w Ww.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/data/jpa G-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" value = "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 = "unionmanws"/> <! -- Maximum number of connections used by the connection pool --> <property name = "maxActive" value = "20"/> <! -- Initialization size --> <property name = "initialSize" value = "5"/> <! -- Get the maximum connection wait time --> <property name = "maxWait" value = "60000"/> <! -- Minimum idle connection pool --> <property name = "minIdle" value = "2"/> <! -- Interval of disconnection detection --> <property name = "timeBetweenEvictionRunsMillis" value = "3000"/> <! -- Minimum eviction time --> <property name = "minEvictableIdleTimeMillis" value = "300000"/> <! -- Test the valid SQL Query --> <property name = "validationQuery" value = "SELECT 'x'"/> <! -- Test whether the connection is valid when idle --> <property name = "testWhileIdle" value = "true"/> <! -- Test validity when obtaining the connection --> <property name = "testOnBorrow" value = "false"/> <! -- Whether the test is valid when the connection is returned --> <property name = "testOnReturn" value = "false"/> </bean> <! -- Integrate mysqljpa --> <bean id = "mysqlEntityManagerFactory" class = "org. springframework. 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. j Pa. vendor. hibernateJpaVendorAdapter "> <property name =" showSql "value =" true "> </property> </bean> </property> <property name =" jpaProperties "> <props> <! -- Set the maximum depth of the Spanning Tree captured by external connections --> <prop key = "hibernate. max_fetch_depth "> 3 </prop> <prop key =" hibernate. jdbc. fetch_size "> 18 </prop> <prop key =" hibernate. jdbc. batch_size "> 10 </prop> <! -- Automatically create TABLE type validate | create-drop | update --> <! -- <Prop key = "hibernate. hbm2ddl. auto"> validate </prop> --> <! -- Whether to display SQL --> <prop key = "hibernate. show_ SQL"> false </prop> <! -- Display whether SQL is formatted --> <prop key = "hibernate. format_ SQL"> false </prop> <! -- Disable secondary cache --> <prop key = "hibernate. cache. provider_class"> org. hibernate. cache. NoCacheProvider </prop> <! -- Disable object field ing verification --> <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 = "fals E "/> <! -- Sqlserver data source configuration --> <bean id = "sqlserverDataSource" class = "com. alibaba. druid. pool. druidDataSource "init-method =" init "destroy-method =" close "> <! -- Driver name --> <property name = "DriverClassName" value = "com. microsoft. sqlserver. jdbc. SQLServerDriver"/> <! -- JDBC connection string --> <property name = "url" value = "jdbc: SQL SERVER: // 192.168.130.10: 1433; DatabaseName = unionman"/> <! -- Database User name --> <property name = "username" value = "sa"/> <! -- Database password --> <property name = "password" value = "123abc"/> <! -- Maximum number of connections used by the connection pool --> <property name = "maxActive" value = "20"/> <! -- Initialization size --> <property name = "initialSize" value = "5"/> <! -- Get the maximum connection wait time --> <property name = "maxWait" value = "60000"/> <! -- Minimum idle connection pool --> <property name = "minIdle" value = "2"/> <! -- Interval of disconnection detection --> <property name = "timeBetweenEvictionRunsMillis" value = "3000"/> <! -- Minimum eviction time --> <property name = "minEvictableIdleTimeMillis" value = "300000"/> <! -- Test the valid SQL Query --> <property name = "validationQuery" value = "SELECT 'x'"/> <! -- Test whether the connection is valid when idle --> <property name = "testWhileIdle" value = "true"/> <! -- Test validity when obtaining the connection --> <property name = "testOnBorrow" value = "false"/> <! -- Whether the test is valid when the connection is returned --> <property name = "testOnReturn" value = "false"/> </bean> <! -- Integrate 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. spri Ngframework. orm. jpa. vendor. hibernateJpaVendorAdapter "> <property name =" showSql "value =" true "> </property> </bean> </property> <property name =" jpaProperties "> <props> <! -- Set the maximum depth of the Spanning Tree captured by external connections --> <prop key = "hibernate. max_fetch_depth "> 3 </prop> <prop key =" hibernate. jdbc. fetch_size "> 18 </prop> <prop key =" hibernate. jdbc. batch_size "> 10 </prop> <! -- Automatically create TABLE type validate | create-drop | update --> <! -- <Prop key = "hibernate. hbm2ddl. auto"> validate </prop> --> <! -- Whether to display SQL --> <prop key = "hibernate. show_ SQL"> false </prop> <! -- Display whether SQL is formatted --> <prop key = "hibernate. format_ SQL"> false </prop> <! -- Disable secondary cache --> <prop key = "hibernate. cache. provider_class"> org. hibernate. cache. NoCacheProvider </prop> <! -- Disable object field ing verification --> <prop key = "javax. persistence. validation. mode "> none </prop> </props> </property> </bean> <bean id =" sqlservertransactionManager "class =" org. springframework. orm. jpa. jpaTransactionManager "> <property name =" entityManagerFactory "ref =" sqlserverEntityManagerFactory "/> <qualifier value =" sqlserverEM "/> </bean> <tx: annotation-driven transaction-manager = "sqlservertransactionManager" proxy-target-class = "false"/> </beans>

Do not change others.

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.