Druid作连接池的数据库的主从动态切换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | 那么首先,既然说到Druid是“数据库连接池”,那么我们就需要写配置文件来连接到数据库,直接贴码我的配置文件,参数含义就不赘述了,代码中我都写了注释 <?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:c= "http://www.springframework.org/schema/c" xmlns:context= "http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans.xsd http: //www.springframework.org/schema/tx/spring-tx.xsd"> <!--注入properties文件--> <context:property-placeholder location= "classpath:dataSource.properties" ignore-unresolvable= "true" /> <!--spring相关注解都需要声明Bean,包括 @Autowired , @Resource 等等,单个声明太过繁琐,spring就提供了这种简便的方式 注:由于<context:component-scan base- package =”xx.xx”/>也包含了自动注入上述Bean的功能,所以<context:annotation-config/> 可以省略。 如果两者都进行了配置,则只有前者有效。 同时<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的 package 下扫描以及注册javabean 。--> <context:annotation-config /> <!--配置数据库--> <bean id= "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" > <property name= "driverClassName" value= "${jdbc.driver.class.name}" /> <!--druid开启日志 value是日志间隔时间,单位是ss--> <property name= "timeBetweenLogStatsMillis" value= "300000" /> <!--初始连接数--> <property name= "initialSize" value= "${jdbc.initialSize}" /> <!--连接池中可同时连接的最大的连接数 一些版本maxActive配置选项已重命名为maxTotal--> <property name= "maxActive" value= "${jdbc.maxActive}" /> <!--最小连接池数量 maxIdle已不再使用,配置了也没效果--> <property name= "minIdle" value= "${jdbc.minIdle}" /> <!--最大等待毫秒数, 单位为 ms, 超过时间会出错误信息--> <property name= "maxWait" value= "${jdbc.maxWait}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name= "timeBetweenEvictionRunsMillis" value= "60000" /> <!-- 配置一个连接在池中最小生存的时间,最小可被驱逐时间,单位是毫秒 --> <property name= "minEvictableIdleTimeMillis" value= "300000" /> <!--验证数据库连接的有效性 Oracle : select 1 from dual Mysql : select 1 --> <property name= "validationQuery" value= "select 1" /> <!-- testOnBorrow和testOnReturn在生产环境一般是不开启的,主要是性能考虑。 失效连接主要通过testWhileIdle保证,如果获取到了不可用的数据库连接,一般由应用处理异常。--> <property name= "testWhileIdle" value= "true" /> <property name= "testOnBorrow" value= "false" /> <property name= "testOnReturn" value= "false" /> <property name= "proxyFilters" > <list> <ref bean= "log-filter" /> <ref bean= "stat-filter" /> </list> </property> <!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall--> <property name= "filters" value= "stat,wall" /> </bean> <!--druid监控--> <bean id= "stat-filter" class = "com.alibaba.druid.filter.stat.StatFilter" > <!--慢查询--> <property name= "mergeSql" value= "true" /> <property name= "slowSqlMillis" value= "1000" /> <property name= "logSlowSql" value= "true" /> </bean> <!--将监控到的数据持久化到日志--> <bean id= "log-filter" class = "com.alibaba.druid.filter.logging.Slf4jLogFilter" > <!--表示是否显示SQL语句--> <!--<property name= "statementExecutableSqlLogEnable" value= "true" />--> <!--表示是否显示结果集--> <property name= "resultSetLogEnabled" value= "false" /> <!--所有DataSource相关的日志--> <property name= "dataSourceLogEnabled" value= "true" /> <!--所有statement相关的日志--> <property name= "statementLogEnabled" value= "true" /> <!--所有连接相关的日志--> <property name= "connectionLogEnabled" value= "true" /> </bean> <!--主库--> <bean id= "masterDateSource" parent= "dataSource" init-method= "init" destroy-method= "close" > <property name= "url" value= "${master.jdbc.url}" /> <property name= "username" value= "${master.jdbc.username}" /> <property name= "password" value= "${master.jdbc.password}" /> </bean> <!--从库--> <bean id= "slaveDateSource" parent= "dataSource" init-method= "init" destroy-method= "close" > <property name= "url" value= "${slave.jdbc.url}" /> <property name= "username" value= "${slave.jdbc.username}" /> <property name= "password" value= "${slave.jdbc.password}" /> </bean> <!--主从库动态切换切面--> <bean class = "com.abc.framework.db.DataSourceAspect" id= "dataSourceAspect" /> <aop:config> <aop:aspect ref= "dataSourceAspect" > <aop:pointcut id= "dbPonitCut" expression= "execution(* com.abc.service.*.*(..))" /> <aop:before method= "before" pointcut-ref= "dbPonitCut" /> <aop:around method= "doAround" pointcut-ref= "dbPonitCut" /> <aop:after method= "doAfter" pointcut-ref= "dbPonitCut" /> </aop:aspect> </aop:config> <!--整合数据库--> <bean id= "dynamicDataSource" class = "com.abc.framework.db.DynamicDataSource" > <property name= "defaultDataSourceName" value= "slaveDateSource" /> <property name= "masterDataSourceName" value= "masterDateSource" /> <property name= "slaveDataSourceNames" > <list> <value>slaveDateSource</value> </list> </property> <property name= "targetDataSources" > <map key-type= "java.lang.String" > <entry key= "slaveDateSource" value-ref= "slaveDateSource" /> <entry key= "masterDateSource" value-ref= "masterDateSource" /> </map> </property> </bean> <!--spring管理事物--> <bean id= "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" c:dataSource-ref= "dynamicDataSource" /> <!--启动对事物注解的支持--> <tx:annotation-driven transaction-manager= "txManager" /> <!-- 配置sqlSessionFactory --> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "configLocation" value= "classpath:context/mybaties/page-plugin.xml" /> <property name= "dataSource" ref= "dynamicDataSource" /> <property name= "mapperLocations" > <list> <value>classpath:mapper/*Mapper.xml</value> </list> </property> </bean> <!-- 通过扫描的模式,自动注入bean --> <bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "sqlSessionFactoryBeanName" value= "sqlSessionFactory" /> <property name= "basePackage" value= "com.abc.dao" /> </bean> </beans> |
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
2019-10-23 tomcat 下 base64图片上传超过2m的解决方案
2015-10-23 关于 java,nio,bufferedreader,bytebuffer