Spring,mybatis,druid, multiple data sources configuration __ notes

Source: Internet
Author: User
Tags aop rollback

1, the realization of multiple data sources, Kua Library query;

2, through the configuration of Spring-mybatis.xml transactions and AOP to complete a service layer of multiple data source transaction support exception rollback;

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:jdbc=" Http://www.springframework.org/schema/jdbc "xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/conte XT Http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/ JDBC Http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http ://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http:// Www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <contexT:annotation-config/> <bean name= "Datasource_c" class= "Com.alibaba.druid.pool.DruidDataSource" > < Property name= "Driverclassname" > <value>com.mysql.jdbc.Driver</value> </property> <propert
			Y name= "url" > <value>${jdbc_url_gx_data}</value> </property> <property name= "username" > <value>${jdbc_username_gx_data}</value> </property> <property name= "password" > <value >${jdbc_password_gx_data}</value> </property> <property name= "InitialSize" value= "5"/> <pro Perty name= "maxactive value="/> <property name= "Minidle" value= "3"/> <property name= "maxWait" value= "60000"/> <!--Configure how often the interval is detected to detect idle connections that need to be closed, in milliseconds--> <property name= "Timebetweenevictionrunsmillis" value= The "60000"/> <!--configures the minimum time for a connection to survive in the pool, in milliseconds--> <property name= "Minevictableidletimemillis" value= "300000"/&G

		T <property name= "ValidatioNquery "value=" select ' X ' "/> <property name=" Testwhileidle "value=" true "/> <property name=" TestOnBorrow " Value= "false"/> <property name= "Testonreturn" value= "false"/> <!--open Pscache and specify the size of Pscache on each connection
		; <property name= "Poolpreparedstatements" value= "true"/> <property " Maxpoolpreparedstatementperconnectionsize "value="/> <!--Configuration Monitoring statistical interception filters--> <property name= "fil Ters "value=" stat "/> </bean> <bean id=" Sqlsessionfactory_c "class=" Org.mybatis.spring.SqlSessionFactoryBean "> <property name=" dataSource "ref=" Datasource_c "/> <!-- Automatically scan entity directory, save configuration.xml manual configuration--> <property name= "Mapperlocations" value= Newmapper/*.xml "/> </bean> <bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer "> < Property Name= "Basepackage" value= "Com/tm/openapi/newmapper"/> <property name= "Sqlsessionfactorybeanname" VAlue= "Sqlsessionfactory_c"/> </bean> <!--configuration transaction manager gx_data default things--> <bean id= "TransactionManager" C lass= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" Datasource_c "/> <qualifier value=" Gx_data "/> </bean> <!--full annotation mode <tx:annotation-driven transact ion-manager= "TransactionManager"/>--> <tx:advice id= "Usertxadvice"
				TransactionManager "> <tx:attributes> <tx:method name=" delete* "propagation=" REQUIRED "read-only=" false " Rollback-for= "Exception"/> <tx:method name= "insert*" propagation= "REQUIRED" read-only= "false" rollback-f Or= "Exception"/> <tx:method name= "save*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/ > <tx:method name= "add*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:me Thod name= "update*" propagation= "REQUIRED" read-onlY= "false" rollback-for= "Exception"/> <tx:method name= "del*" propagation= "REQUIRED" read-only= "false" ro Llback-for= "Exception"/> <tx:method name= "batch*" propagation= "REQUIRED" read-only= "false" rollback-for= "EXC"
			Eption "/> <tx:method name=" bach* "propagation=" REQUIRED "false" read-only= "rollback-for=" Exception  <tx:method name= "cancel*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method Name= "dis*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "en*" Propa gation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "query*" read-only= "true"/&G

			T <tx:method name= "find*" read-only= "true"/> <tx:method name= "get*" read-only= "true"/> <tx:method nam E= "select*" read-only= "true"/> <tx:method name= "count*" read-only= "true"/> </tx:attributes> </t X:advice> &Lt;aop:config> <aop:pointcut id= "PC" expression= Execution (public * Com.tm.openapi.service ... *(..))" /> <!--control the transaction in the service layer--> <aop:advisor pointcut-ref= "PC" advice-ref= "Usertxadvice"/> </aop:config&


	Gt <bean name= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" > <property name= "Driverclassname" > <value>com.mysql.jdbc.Driver</value> </property> <property name= "url" > <value&gt ;${jdbc_url_gx}</value> </property> <property name= "username" > <value>${jdbc_username_gx}& lt;/value> </property> <property name= "password" > <value>${jdbc_password_gx}</value> & lt;/property> <property name= "InitialSize" value= "5"/> <property name= "maxactive" value= "/>" < !--<property name= "maxidle" value= "/>"--> <property name= "Minidle" value= "3"/> <property-name= "Maxwait" value= "60000"/> <!-- How often does the configuration interval be tested to detect idle connections that need to be turned off, in milliseconds--> <property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!-- Configures the minimum time to live a connection in a pool, in milliseconds--> <property name= "Minevictableidletimemillis" value= "300000"/> <property name= "Validationquery" value= "SELECT ' X" "/> <property name=" Testwhileidle "value=" true "/> <property name=" tes Tonborrow "value= false"/> <property name= "Testonreturn" value= "false"/> <!--open Pscache, and specify Pscach on each connection The size of e--> <property name= "poolpreparedstatements" value= "true"/> <property "name=" 

	Perconnectionsize "value="/> <!--Configuration Monitoring statistical interception filters--> <property name= "Filters" value= "stat"/> </bean> <!--mybatis file--> <bean id= "Sqlsessionfactory" class=
		An "> <property name=" dataSource "ref=" DataSource "/> <!--automatically scans the entity directory, eliminating configuration.xml manual configuration--> <property name= "MappeRlocations "value=" Classpath:com/tm/openapi/mapper/*.xml "/> </bean> <bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer "> <property name=" basepackage "value=" com/tm/openapi/ Mapper "/> <property name= sqlsessionfactorybeanname" value= "Sqlsessionfactory"/> </bean> <!-- Place transaction manager GX--> <bean id= "TransactionManager2" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "> <property name=" DataSource "ref=" DataSource "/> <qualifier value=" GX "/> </bean> <!--full annotation mode should be added @transactional <tx:annotation-drive n transaction-manager= "TransactionManager2"/>--> <tx:advice id= "Usertxadviceb" TransactionManager2 "> <tx:attributes> <tx:method name=" delete* "propagation=" REQUIRED "read-only=" false "Rollback-for=" Exception/> <tx:method name= "insert*" propagation= "REQUIRED" read-only= "false" rollback -for= "Exception"/>
			<tx:method name= "save*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:metho D name= "add*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "update*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "del*" propagation= "REQ" Uired "read-only=" false "rollback-for=" Exception "/> <tx:method name=" batch* "propagation=" REQUIRED "read-onl Y= "false" rollback-for= "Exception"/> <tx:method name= "bach*" propagation= "REQUIRED" read-only= "false" ro Llback-for= "Exception"/> <tx:method name= "cancel*" propagation= "REQUIRED" read-only= "false" rollback-for= "Ex
			Ception "/> <tx:method name=" dis* "propagation=" REQUIRED "false" read-only= "rollback-for=" Exception <tx:method name= "en*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method nam E= "query*" read-only= "trUE "/> <tx:method name=" find* "read-only=" true "/> <tx:method name=" get* "read-only=" true "/> ; Tx:method name= "select*" read-only= "true"/> <tx:method "name=" count* "true" read-only= s> </tx:advice> <aop:config> <aop:pointcut id= "PC2 expression=" Execution (Public * com.tm.openap I.service.. *(..))" /> <!--control the transaction in the service layer--> <aop:advisor pointcut-ref= "PC2" advice-ref= "Usertxadviceb"/> g> <!--add db--> <bean name= "Datasource_wms_ware" class= "Com.alibaba.druid.pool.DruidDataSource" > < Property name= "Driverclassname" > <value>com.mysql.jdbc.Driver</value> </property> <propert 
			Y name= "url" > <value>${jdbc_url_wms_ware}</value> </property> <property name= "username" > <value>${jdbc_username_wms_ware}</value> </property> <property name= "password" > <val Ue>${jdbc_password_wms_ware}</value> </property> <property name= "InitialSize" value= "5"/> <proper Ty name= "maxactive" value= "a"/> <!--<property name= "maxidle" value= "/>,-->, <property name=" mi
		Nidle "value=" 3/> <property name= "maxwait" value= "60000"/> <!--how often the configuration interval is detected, and the idle connections that need to be closed are detected in milliseconds--> <property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!--Configure the minimum time that a connection is to survive in the pool, in milliseconds--> <pro Perty name= "Minevictableidletimemillis" value= "300000"/> <property name= "validationquery" value= "select ' X '"/&
		Gt  <property name= "Testwhileidle" value= "true"/> <property name= "Testonborrow" value= "false"/> <property Name= "Testonreturn" value= "false"/> <!--opens Pscache, and specifies the size of pscache on each connection--> <property name= "poolprepared Statements "value=" true "/> <property name=" maxpoolpreparedstatementperconnectionsize "value="/> "< !--Configure the filter for monitoring statistical interceptions--> <property name= "Filters" value= "stat"/> </bean> <bean id= "Sqlsessionfactory_wms_ware" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "Datasource_wms_ware"/> <! --Automatically scan entity directory, save configuration.xml manual configuration--> <property name= "Mapperlocations" value= Openapi/newmapper/*.xml "/> </bean> <bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer " > <property name= "basepackage" value= "Com/tm/openapi/newmapper"/> <property name= " Sqlsessionfactorybeanname "value=" Sqlsessionfactory_wms_ware/> </bean> <!--configuration transaction manager GX--> <bean I D= "TransactionManager3" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > < Property Name= "DataSource" ref= "DataSource"/> <qualifier value= "Wms_ware"/> </bean> <!--full annotation method needs to be added @Transactional <tx:annotation-driven transaction-manager= "TransactionManager2"
		/>--> <tx:advice id= "USERTXADVICEC" transaction-manager= "TransactionManager3" > <tx:attributes>  <tx:method name= "delete*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method Name= "insert*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "save*" propagation= "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "add*" propagation= "requ" Ired "read-only=" false "rollback-for=" Exception "/> <tx:method name=" update* "propagation=" REQUIRED "read-onl Y= "false" rollback-for= "Exception"/> <tx:method name= "del*" propagation= "REQUIRED" read-only= "false" ro Llback-for= "Exception"/> <tx:method name= "batch*" propagation= "REQUIRED" read-only= "false" rollback-for= "EXC"
			Eption "/> <tx:method name=" bach* "propagation=" REQUIRED "false" read-only= "rollback-for=" Exception <tx:method Name= "cancel* "propagation=" REQUIRED "read-only=" false "rollback-for=" Exception "/>" <tx:method name= "dis*" propagation = "REQUIRED" read-only= "false" rollback-for= "Exception"/> <tx:method name= "en*" propagation= "REQUIRED" Read-o Nly= "false" rollback-for= "Exception"/> <tx:method name= "query*" read-only= "true"/> <tx:method nam E= "find*" read-only= "true"/> <tx:method name= "get*" read-only= "true"/> <tx:method "name=" select* Only= "true"/> <tx:method name= "count*" read-only= "true"/> </tx:attributes> </tx:advice> ;aop:config> <aop:pointcut id= "PC3 expression=" Execution (public * Com.tm.openapi.service ... *(..))" /> <!--control the transaction in the service layer--> <aop:advisor pointcut-ref= "PC3" advice-ref= "USERTXADVICEC"/> g> <!--AOP Annotations support--> <aop:aspectj-autoproxy proxy-target-class= "true"/> </beans>


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.