全家桶Spring、HikariCP、Mybatis和Oracle配置,你想要的都在这里

news2024/11/23 23:02:59

目录

  • 1、HikariCP配置说明
  • 2、spring配置文件里,配置HikariCP数据库连接池
  • 3、注意连接池大小设置,重点推荐官方说明文档
  • 4、HikariCP配置
  • 5、数据库配置文件

1、HikariCP配置说明

HikariCP: https://github.com/brettwooldridge/HikariCP

2、spring配置文件里,配置HikariCP数据库连接池



	<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">

		<property name="driverClassName" value="${db.driverClassName}" />
		<property name="jdbcUrl" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
		<property name="connectionTimeout" value="${db.connectionTimeout}" />
		<property name="readOnly" value="${db.readOnly}" />
		<property name="maxLifetime" value="${db.maxLifetime}" />
		<property name="maximumPoolSize" value="${db.maximumPoolSize}" />
		<property name="idleTimeout" value="${db.idleTimeout}" />
		<property name="poolName" value="${db.poolName}" />
		<property name="connectionTestQuery" value="${db.connectionTestQuery}" />


		<property name="dataSourceProperties">
			<props>
				<prop key="cachePrepStmts">${db.cachePrepStmts}</prop>
				<prop key="prepStmtCacheSize">${db.prepStmtCacheSize}</prop>
				<prop key="prepStmtCacheSqlLimit">${db.prepStmtCacheSqlLimit}</prop>
				<prop key="useServerPrepStmts">${db.useServerPrepStmts}</prop>
				<prop key="useLocalSessionState">${db.useLocalSessionState}</prop>
				<prop key="useLocalTransactionState">${db.useLocalTransactionState}</prop>
				<prop key="rewriteBatchedStatements">${db.rewriteBatchedStatements}</prop>
				<prop key="cacheResultSetMetadata">${db.cacheResultSetMetadata}</prop>
				<prop key="cacheServerConfiguration">${db.cacheServerConfiguration}</prop>
				<prop key="elideSetAutoCommits">${db.elideSetAutoCommits}</prop>
				<prop key="maintainTimeStats">${db.maintainTimeStats}</prop>
			</props>
		</property>
	</bean>

3、注意连接池大小设置,重点推荐官方说明文档

https://github.com/brettwooldridge/HikariCP/wiki/About-Pool-Sizing

Configuring a connection pool is something that developers often get wrong. There are several, possibly counter-intuitive for some, principles that need to be understood when configuring the pool.

10,000 Simultaneous Front-End Users
Imagine that you have a website that while maybe not Facebook-scale still often has 10,000 users making database requests simultaneously -- accounting for some 20,000 transactions per second. How big should your connection pool be? You might be surprised that the question is not how big but rather how small!

Watch this short video from the Oracle Real-World Performance group for an eye-opening demonstration (~10 min.):



{Spoiler Alert} if you didn't watch the video. Oh come on! Watch it then come back here.

You can see from the video that reducing the connection pool size alone, in the absence of any other change, decreased the response times of the application from ~100ms to ~2ms -- over 50x improvement.

But why?
We seem to have understood in other parts of computing recently that less is more. Why is it that with only 4-threads an nginx web-server can substantially out-perform an Apache web-server with 100 processes? Isn't it obvious if you think back to Computer Science 101?

Even a computer with one CPU core can "simultaneously" support dozens or hundreds of threads. But we all [should] know that this is merely a trick by the operating system though the magic of time-slicing. In reality, that single core can only execute one thread at a time; then the OS switches contexts and that core executes code for another thread, and so on. It is a basic Law of Computing that given a single CPU resource, executing A and B sequentially will always be faster than executing A and B "simultaneously" through time-slicing. Once the number of threads exceeds the number of CPU cores, you're going slower by adding more threads, not faster.

That is almost true...

Limited Resources
It is not quite as simple as stated above, but it's close. There are a few other factors at play. When we look at what the major bottlenecks for a database are, they can be summarized as three basic categories: CPU, Disk, Network. We could add Memory in there, but compared to Disk and Network there are several orders of magnitude difference in bandwidth.

If we ignored Disk and Network it would be simple. On a server with 8 computing cores, setting the number of connections to 8 would provide optimal performance, and anything beyond this would start slowing down due to the overhead of context switching. But we cannot ignore Disk and Network. Databases typically store data on a Disk, which traditionally is comprised of spinning plates of metal with read/write heads mounted on a stepper-motor driven arm. The read/write heads can only be in one place at a time (reading/writing data for a single query) and must "seek" to a new location to read/write data for a different query. So there is a seek-time cost, and also a rotational cost whereby the disk has to wait for the data to "come around again" on the platter to be read/written. Caching of course helps here, but the principle still applies.

During this time ("I/O wait"), the connection/query/thread is simply "blocked" waiting for the disk. And it is during this time that the OS could put that CPU resource to better use by executing some more code for another thread. So, because threads become blocked on I/O, we can actually get more work done by having a number of connections/threads that is greater than the number of physical computing cores.

How many more? We shall see. The question of how many more also depends on the disk subsystem, because newer SSD drives do not have a "seek time" cost or rotational factors to deal with. Don't be tricked into thinking, "SSDs are faster and therefore I can have more threads". That is exactly 180 degrees backwards. Faster, no seeks, no rotational delays means less blocking and therefore fewer threads [closer to core count] will perform better than more threads. More threads only perform better when blocking creates opportunities for executing.

Network is similar to disk. Writing data out over the wire, through the ethernet interface, can also introduce blocking when the send/receive buffers fill up and stall. A 10-Gig interface is going to stall less than Gigabit ethernet, which will stall less than a 100-megabit. But network is a 3rd place runner in terms of resource blocking and some people often omit it from their calculations.

Here's another chart to break up the wall of text.

在这里插入图片描述

You can see in the above PostgreSQL benchmark that TPS rates start to flatten out at around 50 connections. And in Oracle's video above they showed dropping the connections from 2048 down to just 96. We would say that even 96 is probably too high, unless you're looking at a 16 or 32-core box.

The Formula
The formula below is provided by the PostgreSQL project as a starting point, but we believe it will be largely applicable across databases. You should test your application, i.e. simulate expected load, and try different pool settings around this starting point:

connections = ((core_count * 2) + effective_spindle_count)
A formula which has held up pretty well across a lot of benchmarks for years is
that for optimal throughput the number of active connections should be somewhere
near ((core_count * 2) + effective_spindle_count). Core count should not include
HT threads, even if hyperthreading is enabled. Effective spindle count is zero if
the active data set is fully cached, and approaches the actual number of spindles
as the cache hit rate falls. ... There hasn't been any analysis so far regarding
how well the formula works with SSDs.
Guess what that means? Your little 4-Core i7 server with one hard disk should be running a connection pool of: 9 = ((4 * 2) + 1). Call it 10 as a nice round number. Seem low? Give it a try, we'd wager that you could easily handle 3000 front-end users running simple queries at 6000 TPS on such a setup. If you run load tests, you will probably see TPS rates starting to fall, and front-end response times starting to climb, as you push the connection pool much past 10 (on that given hardware).

Axiom: You want a small pool, saturated with threads waiting for connections.
If you have 10,000 front-end users, having a connection pool of 10,000 would be shear insanity. 1000 still horrible. Even 100 connections, overkill. You want a small pool of a few dozen connections at most, and you want the rest of the application threads blocked on the pool awaiting connections. If the pool is properly tuned it is set right at the limit of the number of queries the database is capable of processing simultaneously -- which is rarely much more than (CPU cores * 2) as noted above.

We never cease to amaze at the in-house web applications we've encountered, with a few dozen front-end users performing periodic activity, and a connection pool of 100 connections. Don't over-provision your database.

"Pool-locking"
The prospect of "pool-locking" has been raised with respect to single actors that acquire many connections. This is largely an application-level issue. Yes, increasing the pool size can alleviate lockups in these scenarios, but we would urge you to examine first what can be done at the application level before enlarging the pool.

The calculation of pool size in order to avoid deadlock is a fairly simple resource allocation formula:

   pool size = Tn x (Cm - 1) + 1

Where Tn is the maximum number of threads, and Cm is the maximum number of simultaneous connections held by a single thread.

For example, imagine three threads (Tn=3), each of which requires four connections to perform some task (Cm=4). The pool size required to ensure that deadlock is never possible is:

   pool size = 3 x (4 - 1) + 1 = 10

Another example, you have a maximum of eight threads (Tn=8), each of which requires three connections to perform some task (Cm=3). The pool size required to ensure that deadlock is never possible is:

   pool size = 8 x (3 - 1) + 1 = 17

👉 This is not necessarily the optimal pool size, but the minimum required to avoid deadlock.

👉 In some environments, using a JTA (Java Transaction Manager) can dramatically reduce the number of connections required by returning the same Connection from getConnection() to a thread that is already holding a Connection in the current transaction.

这个就不翻译了,大家应该能看懂,其实不需要很大的链接数

4、HikariCP配置

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
</bean>

HikariCP配置数据库信息

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:hibernate.properties</value>
        </list>
    </property>
</bean>

配套软件版本:
Obs.: the versions are
log4j: 1.2.16
springframework: 3.1.4.RELEASE
HikariCP: 1.4.0

5、数据库配置文件

Properties file (hibernate.properties):

hibernate.dataSourceClassName=oracle.jdbc.pool.OracleDataSource
hibernate.hikari.maximumPoolSize=10
hibernate.hikari.idleTimeout=30000
dataSource.url=jdbc:oracle:thin:@localhost:1521:xe
dataSource.username=admin
dataSource.password=

参照:https://stackoverflow.com/questions/23172643/how-to-set-up-datasource-with-spring-for-hikaricp

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/43468.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

毕业设计-机器学习人眼检测活体检测-opencv

目录 前言 课题背景和意义 实现技术思路 实现效果图样例 前言 &#x1f4c5;大四是整个大学期间最忙碌的时光,一边要忙着备考或实习为毕业后面临的就业升学做准备,一边要为毕业设计耗费大量精力。近几年各个学校要求的毕设项目越来越难,有不少课题是研究生级别难度的,对本科…

视效剧情口碑双爆棚!Netflix 现象级剧集《怪奇物语》第四季神级视效专访大揭秘!

刷新 Netflix 收视记录的超火剧集《怪奇物语》&#xff08;Stranger Things&#xff09;第四季视效剧情口碑双爆棚&#xff0c;无疑是2022年最值得一看的现象级剧集之一。第四季共九集&#xff0c;分上下两部&#xff0c;分别在今年5月和7月上线&#xff0c;目前豆瓣评分已经稳…

分享知识付费系统变现的方式_知识付费系统开发步骤

一、知识付费赚钱的方式 首先给大家讲讲知识付费赚钱的两大方式&#xff0c;大家可以根据自己的情况来选择做哪种。 1、自己做知识付费赚钱 自己做知识付费需要自己有一套成熟的理念观点&#xff0c;能输出成优质的内容传授给他人。可以将自己的知识技能制作成音频、视频、图…

【Python】三、内置函数

文章目录实验目的一、abs()二、int() / float() / str() / pow()1、int()2、float()3、str()4、pow()三、len() / id() / type()1、len()2、id()3、type()四、编写程序&#xff0c;实现输入一个正的实数x&#xff0c;分别输出x的整数部分和小数部分。1.设计思路2.设计算法3.参考…

南芯科技在科创板提交注册:业绩增速迅猛,股东包括红杉、顺为等

近日&#xff0c;上海南芯半导体科技股份有限公司&#xff08;下称“南芯科技”&#xff09;在上海证券交易所科创板递交招股书&#xff08;注册稿&#xff09;。据贝多财经了解&#xff0c;南芯科技于2022年6月21日在科创板递交上市申请&#xff0c;11月18日获得上市委会议通过…

堆(堆排序和模拟堆)

如何手写一个堆 下标从1开始&#xff0c;如果从0开始的话&#xff0c;他的左儿子的下标就等于0*2 0&#xff0c;麻烦 手写堆可以实现的操作&#xff1a;1&#xff0c;插入一个数 2&#xff0c;求集合当中的最小值 3&#xff0c;删除最小值 4&#xff0c;删除任意一个元素…

基于樽海鞘群算法的线性规划求解matlab程序

基于樽海鞘群算法的线性规划求解matlab程序 1 樽海鞘群优化算法 1.1 生物启示 通过研究海底生物樽海鞘在觅食过程中群体呈链状向食物方向移动的行为活动&#xff0c;学者Mirjalili在2017年提出的一种新型启发式仿生算法—樽海鞘群智能优化算法&#xff08;Salp Swarm Algori…

javascript三种事件模型 + Dom事件流 +事件委托

目录三种事件模型● DOM0 级模型&#xff1a;● IE 事件模型&#xff1a;● DOM2 级事件模型&#xff1a;DOM事件流事件委托target/currentTarget/relateTarget的区别三种事件模型 ● DOM0 级模型&#xff1a; 这种模型不会传播&#xff0c;所以没有事件流的概念&#xff0c;…

Golang远程调试Debug环境

目录背景软件版本环境搭建安装Golang环境安装dlv环境启动远程环境Goland 连接远程环境参考背景 最近在做 Operator 的二次开发&#xff0c;开发语言是Golang。Operator 开发时候需要用到k8s集群&#xff0c;遗憾的是k8s编排的容器网络与本地网络不通&#xff0c;无法直接进行d…

十部必看特种部队电影之《勇者行动》

这部特种部队题材电影是馆长收藏了很久的网盘资源&#xff0c;今天拿出来分享给大家。

远程直接连接 MySQL 数据库,阿里云腾讯云允许远程连接教程

不使用SSH登录远程主机直接连接远程数据库 文章目录修改MySQL登录权限1、登录MySQL2、修改mysql库的user表3、防火墙开放3306端口Navicat直接连接远程数据库报错【报错】Cant connect to MySQL server (10060)1、网络问题2、mysql账户设置3、防火墙端口未开放4、查看云服务器商…

JWT -- Json Web token

JWT 的背景知识可以看这篇文章: JSON Web Token 入门教程 JWT 由三个部分组成&#xff1a; Header&#xff08;头部&#xff09;Payload&#xff08;负载&#xff09;Signature&#xff08;签名&#xff09; 在分布式系统下&#xff0c;存在跨session的问题&#xff0c;则使用…

[附源码]Python计算机毕业设计Django毕业生就业管理系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

cdn加速华为云obs桶文件配置过程(详细)

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注微信公众号&#xff1a;雄雄的小课堂。 前言 前面写了一篇文章&#xff0c;jeecg-boot中上传图片到华为云obs云存储中 主要介绍了下&#xff0c;如何使用jeect-boot微服务将文件上传至obs中。 但是上传是没有问题&#xff0c;…

目前看过最全的一线大厂面试题(题 + 详解),你所不知道的都在这

前言 在过 1 个月即将进入 2023&#xff0c;然而面对今年的大环境而言&#xff0c;跳槽成功的难度比往年高了很多&#xff0c;很明显的感受就是&#xff1a;对于今年的 java 开发朋友跳槽面试&#xff0c;无论一面还是二面&#xff0c;都开始考验一个 Java 程序员的技术功底和…

FL Studio2022水果编曲音乐制作软件自带完整插件

FL Studio 2022是一款非常好用的音乐制作软件&#xff0c;又称水果编曲软件&#xff0c;软件集合了录音、混音、编辑等多种功能于一体&#xff0c;能够完成各种各样的音乐编曲工作&#xff0c;强大的音乐制作功能受到了很多用户的喜爱&#xff0c;帮你完成各种类型音乐的编曲制…

《500强高管谈VE》-企业经营与VE活动

文章出处&#xff1a;日本VE协会杂志文章翻译&#xff1a;泰泽项目部 关注泰泽&#xff1a;实现高利润企业 《500强高管谈VE》-企业经营与VE活动 作者&#xff1a;兄弟工业常务董事渡边共祥 由墨西哥货币不稳定引发的此次日元升值&#xff0c;一度跌破80日元&#xff0c;呈现…

Servlet程序及部署方式(Tomcat+Smart Tomcat)

目录 1、Servlet是什么&#xff1f; 2、Servlet程序【例——hello world】 2.1、创建项目 2.2、引入Servlet依赖 2.3、创建目录结构 2.4、编写代码 2.5、打包程序 2.6、部署程序 2.7、验证程序 3、更方便的部署方式——Smart Tomcat 1、Servlet是什么&#xff1f; Se…

初识计算机网络

目录 网络的发展 重新看待计算机结构 大型存储平台 认识 "协议" 网络和OS之间的关系 初识网络协议 协议分层 OSI七层模型 TCP/IP五层(或四层)模型 网络传输基本流程 局域网通信的原理 如果进行跨网络传输 网络通信里面的基本轮廓 数据包封装和分用…

多线程同步

文章目录一、多线程同步竞争与协作互斥的概念同步的概念互斥与同步的实现和使⽤锁信号量⽣产者-消费者问题经典同步问题读者-写者问题一、多线程同步 竞争与协作 在单核 CPU 系统⾥&#xff0c;为了实现多个程序同时运⾏的假象&#xff0c;操作系统通常以时间⽚调度的⽅式&am…