The javaEE project establishes multiple data sources and configures transactions.

Source: Internet
Author: User

The javaEE project establishes multiple data sources and configures transactions.
The following three data sources are available: jdbc1.driver = oracle. jdbc. driver. oracleDriverjdbc1.url = jdbc: oracle: thin: @ 127.0.0.1: 1521: jdbc1jdbc1. user = jdbc1jdbc1. pwd = jdbc1jdbc1. validationQuery = select 1 from dualjdbc1.pagesize = 12 jdbc2.jdbc. driver = oracle. jdbc. driver. oracleDriverjdbc2.jdbc. url = jdbc: oracle: thin: @ 127.0.0.1: 152: jdbc2jdbc2. jdbc. user = jdbc2jdbc2. jdbc. pwd = jdbc2jdbc2. jdbc. validationQuery = select 1 from dualjdbc2.jdbc. pagesiz E = 12 jdbc3.driver = oracle. jdbc. driver. oracleDriverjdbc3.url = jdbc: oracle: thin: @ 127.0.0.1: 1521: jdbc3jdbc3. user = jdbc3jdbc3. pwd = jdbc3jdbc3. validationQuery = select 1 from dualjdbc3.pagesize = 12 spring configuration file configuration data source: <? 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: tx = "http://www.springframework.org/schema/tx" xmlns: aop = "http://www.springframework.org/schema/aop" 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.spr Ingframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <! -- Performance1 --> <bean id = "performance1" class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc1.driver} "/> <property name =" url "value =" $ {jdbc1.url} "/> <property name =" username "value =" $ {jdbc1.user} "/> <property name =" password "value =" $ {jdbc1.pwd} "/> <property name = "maxActive" value = "30"/> <property name = "maxIdle" value = "30"/> <p Roperty name = "maxWait" value = "10000"/> <property name = "logAbandoned" value = "true"/> <property name = "removeAbandoned" value = "true "/> <property name = "removeAbandonedTimeout" value = "150"/> <property name = "testWhileIdle" value = "true"/> <property name = "timeBetweenEvictionRunsMillis" value = "300000 ""/> <property name =" minEvictableIdleTimeMillis "value =" 320000 "/> </bean> <! -- Performance2 --> <bean id = "performance2" class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc2.jdbc. driver} "/> <property name =" url "value =" $ {jdbc2.jdbc. url} "/> <property name =" username "value =" $ {jdbc2.jdbc. user} "/> <property name =" password "value =" $ {jdbc2.jdbc. pwd} "/> <property name =" maxActive "value =" 30 "/> <property name =" maxId Le "value =" 30 "/> <property name =" maxWait "value =" 10000 "/> <property name =" logAbandoned "value =" true "/> <property name = "removeAbandoned" value = "true"/> <property name = "removeAbandonedTimeout" value = "150"/> <property name = "testWhileIdle" value = "true"/> <property name = "timeBetweenEvictionRunsMillis" value = "300000"/> <property name = "minEvictableIdleTimeMillis" value = "320000"/> </bean> <bean id = "Cmds3." class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {jdbc3.driver} "/> <property name =" url "value =" $ {jdbc3.url} "/> <property name =" username "value =" $ {jdbc3.user} "/> <property name =" password "value =" $ {jdbc3.pwd} "/> <property name = "maxActive" value = "30"/> <property name = "maxIdle" value = "30"/> <property name = "maxWait" va Lue = "10000"/> <property name = "logAbandoned" value = "true"/> <property name = "removeAbandoned" value = "true"/> <property name =" removeAbandonedTimeout "value =" 150 "/> <property name =" testWhileIdle "value =" true "/> <property name =" timeBetweenEvictionRunsMillis "value =" 300000 "/> <property name = "minEvictableIdleTimeMillis" value = "320000"/> </bean> <bean id = "nativeJdbcExtractor" class = "org. springframe Work. jdbc. support. nativejdbc. simpleNativeJdbcExtractor "/> <bean id =" oracleLobHandle "class =" org. springframework. jdbc. support. lob. extends lelobhandler "lazy-init =" true "> <property name =" nativeJdbcExtractor "ref =" nativeJdbcExtractor "/> </bean> <bean id =" sessionFactory "class =" org. springframework. orm. hibernate3.LocalSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" lo BHandler "ref =" oracleLobHandle "/> <property name =" mappingDirectoryLocations "> <list> <value> classpath *: resource/hibernate </value> </list> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect ">$ {hibernate. dialect} </prop> <prop key = "hibernate. show_ SQL ">$ {hibernate. show_ SQL} </prop> <prop key = "hibernate. cache. use_query_cache ">$ {hibernate. cache. use_query_cache} </prop> <Prop key = "hibernate. cache. provider_class ">$ {hibernate. cache. provider_class }</prop> <prop key = "hibernate. query. factory_class ">$ {hibernate. query. factory_class }</prop> </props> </property> </bean> <! -- Hibernate TransactionManager --> <bean id = "transactionManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <property name =" dataSource "> <ref bean =" dataSource "/> <! -- DataSource bean id --> </property> <property name = "globalrollbackonparticipant ipationfailure" value = "false"/> </bean> <aop: config proxy-target-class = "true"> <! -- The transaction is controlled at the service layer and dao layer --> <aop: pointcut id = "serviceMethods" expression = "execution (**.. * ServiceImpl. *(..)) "/> <aop: pointcut id =" managerMethods "expression =" execution (**.. * ManagerImpl. *(..)) "/> <aop: pointcut id =" serviceMethods2 "expression =" execution (**.. * Dao. *(..)) "/> <aop: advisor pointcut-ref =" serviceMethods "advice-ref =" txAdvice "/> <aop: aspect ref =" logMonitor "> <! -- Customize LogProfiler to print database logs --> <aop: Und und pointcut-ref = "serviceMethods" method = "profileMethod"/> </aop: aspect> </aop: config> <bean id = "logMonitor" class = "LogProfiler"/> <tx: advice id = "txAdvice" transaction-manager = "transactionManager"> <tx: attributes> <tx: method name = "save *"/> <tx: method name = "insert *"/> <tx: method name = "remove *"/> <tx: method name = "delete *"/> <tx: method name = "update *"/> <tx: method name = "add *"/> <tx: method name = "*" read-only = "true"/> </tx: attributes> </tx: advice> </beans> write a class that inherits JdbcGenericDao and inherits JdbcTemplate. The JdbcTemplate must be passed to dataSource; public class JdbcGenericDao extends JdbcTemplate {protected Log = LogFactory. getLog (getClass (); protected LobHandler lobHandler; public void setLobHandler (LobHandler lobHandler) {this. lobHandler = lobHandler;} 2. inherit JdbcGenericDao from the dao classes (such as FundInfoDao, CommonLog, and FundDirectDao) of different modules of the project, and transmit the dataSource specified by the person; <bean id = "fundInfoDao" class = "FundInfoDao"> <property name = "dataSource" ref = "performance1"/> </bean> <bean id = "commonLogDao" class = "CommonLog"> <property name = "dataSource" ref = "cece2"/> </bean> <bean id = "fundDirectDao" class = "FundDirectDao"> <property name =" dataSource "ref =" rjc4"/> </bean> 3. summary interface polymorphism. spring integrates jdbc's JdbcTemplate and requires the dataSource bean. How can this bean be implemented;

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.