springboot 线程池和数据库链接池配置以及多线程效率实测
使用spring多线程往mysql数据库插入100万条数据效率对比,结果如下:
a) 20个线程*100000条/线程 = 200万条数据, 用时7分43秒(同样情况跑了2次,第一次是7分42秒,第二次是7分44秒)
b)1个线程*2000000条/线程 = 200万条数据,用时11分27秒。两者差别不大,重新调试参数,应该会有更快的结果
直接上代码:
配置文件:threadPoolConfig.xml (放在resource/META-INF目录下)
<?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:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "> <!-- spring thread pool executor --> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <!-- 线程池维护线程的最少数量 --> <property name="corePoolSize" value="20" /> <!-- 允许的空闲时间 --> <property name="keepAliveSeconds" value="200" /> <!-- 线程池维护线程的最大数量 --> <property name="maxPoolSize" value="50" /> <!-- 缓存队列 --> <property name="queueCapacity" value="100" /> <!-- 对拒绝task的处理策略 --> <property name="rejectedExecutionHandler"> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" /> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 数据库连接驱动 --> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <!-- 数据库连接url --> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/sampledb?useSSL=false&characterEncoding=utf8"/> <!-- 数据库连接用户名 --> <property name="user" value="root"/> <!-- 数据库连接密码 --> <property name="password" value="root"/> <property name="automaticTestTable" value="test_timeout"/> <!-- 隔多少秒检查所有连接池中的空闲时间 --> <property name="idleConnectionTestPeriod" value="60"/> <!-- 最大空闲时间,超过空闲时间的连接将被丢弃 --> <property name="maxIdleTime" value="900"/> <!-- 初始化连接池数量 --> <property name="initialPoolSize" value="10"/> <!-- 最小连接池数量 --> <property name="minPoolSize" value="10"/> <!-- 最大连接池数量 --> <property name="maxPoolSize" value="50"/> <!-- 当连接池连接用完时,C3PO一次性创建新连接的数据 --> <property name="acquireIncrement" value="10"/> <!-- 数据源内加载的PreparedStatement数量 --> <property name="maxStatements" value="200" /> <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false --> <property name="testConnectionOnCheckout" value="false"/> <!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false --> <property name="testConnectionOnCheckin" value="true"/> </bean></beans>
1 OperationService.java
@Service@ContextConfiguration(classes = Config.class)public class OperationService { @Autowired private JdbcTemplate jdbcTemplate; private Logger logger = LoggerFactory.getLogger(OperationService.class); /** * 每个线程根据自己的线程号插入相应的一段数据。例如:线程5插入id为501~600之间的数据 */ public void dataInsert(int from, int to){ String insertSql = "insert into t_user_test values (?, 'john', '123', ?)"; LocalDateTime start = LocalDateTime.now(); for (int i = from; i <= to; i++) { jdbcTemplate.update(insertSql, new Object[]{i, LocalDateTime.now()}); } LocalDateTime end = LocalDateTime.now(); logger.info("开始时间:" + start + ", 结束时间:" + end); } } 2 MyThread.java
public class MyThread implements Runnable { private OperationService operationService; private int from; private int to; public MyThread(){ } public MyThread(OperationService operationService, int from, int to){ this.operationService = operationService; this.from = from; this.to = to; } @Override public void run() { operationService.dataInsert(from, to); }} 3 Config.java
@Configuration@ComponentScan(basePackages = { "common.use.multiThread" })@ImportResource(value = {"classpath:META-INF/threadPoolConfig.xml" })@EnableSchedulingpublic class Config {} 4 测试类 MyTest.java
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = Config.class)public class MyTest { @Autowired private ThreadPoolTaskExecutor taskExecutor; @Autowired private OperationService operationService; @Test public void test(){ for (int i = 1; i <= 20; i++) { int unit = 100000; int from = (i-1)*unit; int to = i*unit - 1; taskExecutor.execute(new MyThread(operationService, from, to)); System.out.println("int i is " + i + ", now threadpool active threads totalnum is " + taskExecutor.getActiveCount()); } try { System.in.read(); } catch (IOException e) { throw new RuntimeException(e); } } //注意:System.in.read()要保留,如果不保留,测试类主线程执行完,直接关闭jvm,不等待子线程执行完,这是坑。放在main方法里则可以省略。
}
转载于:https://www.cnblogs.com/katsu2017/p/7887375.html
springboot 线程池和数据库链接池配置以及多线程效率实测相关推荐
- 数据库链接池c3p0的配置
由于我看的是远古教程,所以里面各种驱动jar包还有c3p0包都是远古版本,对于最新版本的jdbc已经失去的作用,所以我在这里重写一下! 1.首先是c3p0的位置,package的外面,src的里面 2 ...
- springboot 数据库链接池常用配置
保留一下springboot常用的配置 spring.datasource.primary.url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=tr ...
- ORM + 数据库链接池
db_pool.py from DBUtils.PooledDB import PooledDB import pymysqlPOOL = PooledDB(creator=pymysql, # 使用 ...
- SpringMVC数据库链接池,以及其他相关配置
1.applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans x ...
- 数据库链接池终于搞对了,这次直接从100ms优化到3ms!
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 本文来源: https://www.jianshu.com/p/a ...
- [转自microsoft]NET 数据访问架构指南,-数据库连接的测试.即监视链接池化
NET 数据访问架构指南 Alex Mackman, Chris Brooks, Steve Busby, 和 Ed Jezierski 微软公司 2001年10月 概述:本文提供了在多层.NET应用 ...
- Flask 蓝图,数据库链接
蓝图 使用场景 如果代码非常多,要进行归类.不同的功能放在不同的文件,把相关的视图函数也放进去. 蓝图也就是对flask的目录结构进行分配(应用于小,中型的程序) 当然对于大型项目也可以通过 ur ...
- spring 数据库 链接db2_Druid,Java语言中最好的数据库连接池
Druid是Java语言中最好的数据库连接池,这话不是我说的,是Druid官方文档自己这样描述的,这是何等的自信! 连接池的作用,跟线程池的作用大同小异,都是为了减少频繁的创建销毁连接IO,提升性能. ...
- Druid链接池的配置和使用
1.URL参数配置 1.1.实例: jdbc:mysql://192.168.1.8:3306/mytest?serverTimezone=GMT%2B8&autoReconnect=true ...
- springboot集成mongodb 连接池 多数据库源,源码demo
上一篇介绍了 springboot集成mongodb 多数据库源切换 源码demo 开发环境: windows 7 idea windows64 mongodb 如果没安装运行 点这里 navicat ...
最新文章
- 将ubuntu系统设置静态ip及ssh
- PageRequestManagerServerError
- python介绍和用途-Python字典简介以及用法详解
- idea 解决查看源码没有注释
- vmware linux
- 简单的makefile模板
- Spring Boot 中使用@KafkaListener并发批量接收消息(转载)
- 电商业务中的五大机器学习问题!
- 字符串的getBytes方法
- 数理统计与描述性分析
- 【前端】静态网页和动态网页
- 阿里云国际版短信发送
- SQL server安装时:“以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机”错误
- 会计基础-会计科目+会计账户+复式记账+会计分录+会计凭证
- OpenCV基础课程笔记09模糊图像(1)
- 电脑性能,如何提高电脑性能 方法介绍【图文教程】
- 捕获计算机屏幕++方法,在Win10中获取屏幕截图的五大方法
- BMS(电池管理系统)第11课—动力电池系统安全
- 捷联惯导系统学习6.2(序贯滤波 )
- 网络安全SSRF漏洞检测
热门文章
- PAT之算法/技巧:01背包
- dude由于目标计算机,The Dude的教程
- react 获取url参数_十分钟上手 React+MirrorX,从此前端大神代码不再难懂
- 【2020牛客寒假基础算法训练营】第二场总结
- html5怎么插入一段文字,HTML5教程—文字插入进度动画_HTML5教程_文字插入_动画进度_课课家...
- es 吗 查询必须有sort_elasticsearch使用小结(ES使用小结)
- Dropout 丢弃法 动手学深度学习v2 pytorch
- Docker MySQL 8 慢查询日志监控详解
- 无约束优化问题的相关讨论
- 第三届Apache Flink 极客挑战赛暨AAIG CUP电商推荐“抱大腿”攻击识别