mysql连接池的使用工具类代码示例
mysql连接池代码工具示例(scala):
import java.sql.{Connection,PreparedStatement,ResultSet}
import org.apache.commons.dbcp.BasicDataSource
object ConnectPoolUtil{
private var bs:BasicDataSource = null
/**
*创建数据源
*/
def getDataSource():BasicDataSource = {
if (bs = null){
bs = new BasicDataSource()
bs.setDriverClassName("com.mysql.jdbc.Driver")
bs.setUrl("jdbc:mysql://localhost:3306/${db_name}")
bs.setUsername("root")
bs.setPassword("123456")
bs.setMaxActive(200) //设置最大的并发数
bs.setInitialSize(30) //数据库初始化时,创建的连接个数
bs.setMinIdle(50) //最小空闲连接数
bs.setMaxIdle(200) //数据库最大连接数
bs.setMaxWait(1000)
bs.setMinEvictableIdleTimeMillis(60 * 1000) //空闲连接60秒之后释放
bs.setTimeBetweenEvictionRunsMills(5 * 60 * 1000) //5分钟检测一次是否有进程死掉
}
bs
}
/**
*释放数据源
*/
def shutDownDataSource(){
if (bs != null) {
bs.close()
}
}
/**
*获取数据库的连接
*/
def getConnection():Connection = {
val conn :Connection = null
try{
if (bs != null){
conn = bs.getConnection()
}else{
conn = getDataSource().getConnection()
}
}catch {
case e:Exception => println(e.getMessage)
}
conn
}
/**
*关闭连接
*/
def closeConn(rs:ResultSet,ps:PreparedStatement,conn:Connection){
if (rs != null)
rs.close()
if (ps != null)
ps.close()
if (conn != null)
conn.close()
}
}
mysql连接池的使用工具类代码示例的更多相关文章
- 【知了堂学习心得】浅谈c3p0连接池和dbutils工具类的使用
1. C3P0概述 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等. 2. C3P ...
- 数据库连接池与SQL工具类
数据库连接池与SQL工具类 1.数据库连接池 依赖包 pymysql dbutils # -*- coding: utf-8 -*- ''' @Time : 2021/11/19 16:45 @Aut ...
- Jedis工具类代码
安装Redis可以参考 https://www.cnblogs.com/dddyyy/p/9763098.html Redis的学习可以参考https://www.cnblogs.com/dddyyy ...
- workerman如何写mysql连接池
首先要了解为什么用连接池,连接池能为你解决什么问题 连接池主要的作用1.减少与数据服务器建立TCP连接三次握手及连接关闭四次挥手的开销,从而降低客户端和mysql服务端的负载,缩短请求响应时间2.减少 ...
- Swoole4-swoole创建Mysql连接池
一 .什么是mysql连接池 场景:每秒同时有1000个并发,但是这个mysql同时只能处理400个连接,mysql会宕机. 解决方案:连接池,这个连接池建立了200个和mysql的连接,这1000个 ...
- swoole4创建Mysql连接池
一 .什么是mysql连接池 场景:每秒同时有1000个并发,但是这个mysql同时只能处理400个连接,mysql会宕机. 解决方案:连接池,这个连接池建立了200个和mysql的连接,这100 ...
- Java Mysql连接池配置和案例分析--超时异常和处理
前言: 最近在开发服务的时候, 发现服务只要一段时间不用, 下次首次访问总是失败. 该问题影响虽不大, 但终究影响用户体验. 观察日志后发现, mysql连接因长时间空闲而被关闭, 使用时没有死链检测 ...
- MySQL连接池
1. using System; using System.Collections; using MySql.Data.MySqlClient; namespace Helper { /// < ...
- 用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...
随机推荐
- 浅谈k短路算法
An Old but Classic Problem 给定一个$n$个点,$m$条边的带正权有向图.给定$s$和$t$,询问$s$到$t$的所有权和为正路径中,第$k$短的长度. Notice 定义两 ...
- Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心
Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...
- Plantuml画图工具
1,Plantuml画图工具 安装指南: Mac sublimetext http://blog.csdn.net/zhangjikuan/article/details/53365730 win i ...
- 求最大流dinic算法模板
//最短增广路,Dinic算法 struct Edge { int from,to,cap,flow; };//弧度 void AddEdge(int from,int to,int cap) //增 ...
- Python3基础 list del 从内存中删除整个列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 给sublime设置格式化代码的快捷键
sublime中自建的有格式化按钮: Edit -> Line -> Reindent 只是sublime并没有给他赋予快捷键,所以只需加上快捷键即可 Preference -& ...
- 三星固态sm863,pm863,sm865,sm865a颗粒
目录 左pm863,右sm863: sm865: sm865a: 主控,缓存: 颗粒: 左pm863,右sm863: sm865: sm865a: 主控,缓存: 颗粒:
- Oracle联合多个子查询(inner join)
select aaa.*,bbb.xh from (select xn,xq,kcdm,kcmc,xf,xkkh,kcxz from jxrwbview where xn='2017-2018' gr ...
- CF161D Distance in Tree(点分治)
点分治是一种处理树的优秀暴力 这是一道板子题 #include <cstdio> #include <cstring> #include <algorithm> u ...
- 6、lvs使用进阶(02)
把web server服务和443服务绑定在一起之后呢? 假设一种场景,对web服务器来讲需要session保持.一个在线购物网站,在购物时,如果不结账,一般是http协议,当结账时,需要网站跳转,可 ...