当前位置:网站首页>自定义MySQL连接池
自定义MySQL连接池
2022-06-27 21:33:00 【InfoQ】
commons-pool2jediscommons-pool2commons-pool2可池化对象
com.funtester.db.mysql.FunMySqlpackage com.funtester.db.mysql;
import com.funtester.base.interfaces.IMySqlBasic;
import com.funtester.config.SqlConstant;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* mysql操作的基础类
* <p>用于存储数据,多用于爬虫</p>
*/
public class FunMySql extends SqlBase implements IMySqlBasic {
/**
* {@link SqlConstant#FUN_SQL_URL}会替换IP到URL
*/
String url;
/**
* 库
*/
String database;
/**
* 用户
*/
String user;
/**
* 密码
*/
String password;
Connection connection;
Statement statement;
/**
* 私有构造方法
*
* @param url 连接地址,包括端口
* @param database 库
* @param user 用户名
* @param password 密码
*/
public FunMySql(String url, String database, String user, String password) {
this.url = url;
this.database = database;
this.user = user;
this.password = password;
getConnection(database);
}
/**
* 初始化连接
*/
@Override
public void getConnection() {
getConnection(EMPTY);
}
/**
* 执行sql语句,非query语句,并不关闭连接
*
* @param sql
*/
@Override
public void executeUpdateSql(String sql) {
SqlBase.executeUpdateSql(connection, statement, sql);
}
/**
* 查询功能
*
* @param sql
* @return
*/
@Override
public ResultSet executeQuerySql(String sql) {
return SqlBase.executeQuerySql(connection, statement, sql);
}
/**
* 关闭query连接
*/
@Override
public void over() {
SqlBase.close(connection, statement);
}
@Override
public void getConnection(String database) {
if (connection == null)
connection = SqlBase.getConnection(SqlConstant.FUN_SQL_URL.replace("ip", url).replace("database", database), user, password);
if (statement == null) statement = SqlBase.getStatement(connection);
}
}
池化工厂
com.funtester.db.mysql.FunMySqlcom.funtester.db.mysql.MysqlPool.FunTester#destroyObject /**
* 池化工厂类
*/
private class FunTester extends BasePooledObjectFactory<FunMySql> {
@Override
FunMySql create() throws Exception {
return new FunMySql(url, database, user, password)
}
@Override
PooledObject<FunMySql> wrap(FunMySql obj) {
return new DefaultPooledObject<FunMySql>(obj)
}
@Override
void destroyObject(PooledObject<FunMySql> p) throws Exception {
p.getObject().over()
super.destroyObject(p)
}
}
对象池
com.funtester.db.mysql.MysqlPoolcom.funtester.db.mysql.FunMySql/**
* 自定义MySQL连接池对象
*/
class MysqlPool extends PoolConstant {
private static final Logger logger = LogManager.getLogger(MysqlPool.class);
/**
* {@link com.funtester.config.SqlConstant#FUN_SQL_URL}会替换IP到URL*/
String url;
/**
* 库
**/
String database;
/**
* 用户
**/
String user;
/**
* 密码
**/
String password;
private GenericObjectPool<FunMySql> pool
MysqlPool(String url, String database, String user, String password) {
this.url = url
this.database = database
this.user = user
this.password = password
init()
}
/**
* 初始化连接池
* @return
*/
def init() {
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(MAX);
poolConfig.setMinIdle(MIN_IDLE);
poolConfig.setMaxIdle(MAX_IDLE);
poolConfig.setMaxWaitMillis(MAX_WAIT_TIME);
poolConfig.setMinEvictableIdleTimeMillis(MAX_IDLE_TIME);
pool = new GenericObjectPool<FunMySql>(new FunTester(), poolConfig);
}
}
API封装
/**
* 借出对象
* @return
*/
def borrow() {
try {
return pool.borrowObject()
} catch (e) {
logger.warn("获取${JSONObject.class} 失败", e)
} finally {
new JSONObject()
}
}
/**
* 归还对象
* @param funMySql
* @return
*/
def back(FunMySql funMySql) {
pool.returnObject(funMySql)
}
/**
* 执行update SQL
* @param sql
* @return
*/
def execute(def sql) {
def driver = borrow()
try {
driver.executeUpdateSql(sql)
} catch (e) {
logger.warn("执行:{}失败", sql)
} finally {
back(driver)
}
}
/**
* 执行查询SQL
* @param sql
* @return
*/
def query(def sql) {
def driver = borrow()
try {
return driver.executeQuerySql(sql)
} catch (e) {
logger.warn("执行:{}失败", sql)
} finally {
back(driver)
}
}
- 性能测试专题
- Java、Groovy、Go、Python
- 单测&白盒
- FunTester社群风采
- 测试理论鸡汤
- 接口功能测试专题
- FunTester视频专题
- 案例分享:方案、BUG、爬虫
- UI自动化专题
- 测试工具专题
阅读原文,跳转我的仓库地址边栏推荐
- Super outline exercises
- 【微服务|Sentinel】sentinel数据持久化
- How to use raspberry pie (and all kinds of pies)
- How to find Chinese documents corresponding to foreign documents?
- [sword finger offer] 48 Longest substring without duplicate characters
- 【AI应用】NVIDIA Tesla V100-PCIE-32GB的详情参数
- [AI application] detailed parameters of NVIDIA geforce RTX 3060
- Elk in Windows Environment - logstash+mysql (4)
- 超纲练习题不超纲
- Excel print settings public header
猜你喜欢

What if Fiddler fails to listen to the interface

Halcon's region: features of multiple regions (6)

安全省油環保 駱駝AGM啟停電池魅力十足

halcon之区域:多种区域(Region)特征(6)

C language character pointer and string initialization

Storage structure of graph

Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm

Cornernet understands from simple to profound

【PCL自学:PCLVisualizer】点云可视化工具PCLVisualizer

Zero foundation self-study SQL course | case function
随机推荐
virtualbox扩展动态磁盘大小的坑
Instructions for vivado FFT IP
如何找到外文文献对应的中文文献?
Zero foundation self-study SQL course | if function
PAT乙级1013
Usage of vivado vio IP
用两个栈实现队列[两次先进后出便是先进先出]
【PCL自学:PCLPlotter】PCLPlotter绘制数据分析图
100 questions for an enterprise architect interview
Zero foundation self-study SQL course | case function
使用cef3开发的浏览器不支持flash问题的解决
MySQL read / write separation configuration
[sword finger offer] 47 Maximum value of gifts
获取基因有效长度的N种方法
matlab axis坐标轴相关设置详解
The file or assembly 'cefsharp.core.runtime.dll' or one of its dependencies could not be loaded. Is not a valid Win32 Application. (exception from hresult:0x800700c1)
ICML 2022: UFRGS |作为最优策略转移基础的乐观线性支持和后继特征
Structure de stockage des graphiques
[AI application] detailed parameters of Jetson Xavier nx
How vivado adds timing constraints