当前位置: 首页 > news >正文

东莞人才网官网网站seo在线诊断分析

东莞人才网官网,网站seo在线诊断分析,网站脚本怎么做,临清网站制作公司连接池初始化 OVERVIEW 连接池初始化1.Connection类Connection.hConnection.cpp 2.CommonConnectionPool类CommonConnectionPool.hCommonConnectionPool.cpp 1.Connection类 封装Connection类,在该类内调用mysql提供的接口实现对数据库的增删改查, Con…

连接池初始化


OVERVIEW

  • 连接池初始化
      • 1.Connection类
        • Connection.h
        • Connection.cpp
      • 2.CommonConnectionPool类
        • CommonConnectionPool.h
        • CommonConnectionPool.cpp

1.Connection类

封装Connection类,在该类内调用mysql提供的接口实现对数据库的增删改查,

Connection.h
#ifndef _CONNECTION_H
#define _CONNECTION_H#include <string.h>
#include <iostream>
#include <mysql/mysql.h>
using namespace std;#define LOG(str)									\cout << __FILE__ << ":" << __LINE__ << " " <<	\__TIMESTAMP__ << ":" << str << endl;// 封装Connection对象 实现数据库增删改查
class Connection {
public:// 初始化数据库连接Connection();// 释放数据库连接资源~Connection();// 连接数据库bool connect(string ip, unsigned short port, string user, string password, string dbname);// 更新数据库操作 insert delete updatebool update(string sql);
private:// 表示和 MYSQL Server的一条连接MYSQL *_conn;
};#endif
Connection.cpp
#include "Connection.h"Connection::Connection()
{_conn = mysql_init(nullptr);
}Connection::~Connection()
{if (_conn != nullptr) mysql_close(_conn);
}bool Connection::connect(string ip, unsigned short port, string user, string password, string dbname)
{MYSQL *p = mysql_real_connect(_conn, ip.c_str(), user.c_str(), password.c_str(), dbname.c_str(), port, nullptr, 0);return p != nullptr;
}// insert delete update
bool Connection::update(string sql)
{if (mysql_query(_conn, sql.c_str())) {LOG("update failed!~" + sql);return false;}return mysql_use_result(_conn);
}

2.CommonConnectionPool类

  • 需求分析:

    在整个系统中数据库连接池对象出现一次即可,有数据库连接需求时,从同一个数据库连接池中获取 Connection 对象即可,

    连接池不需要创建多个,所以可以通过单例模式来设计数据库连接池类,但是服务器肯定是多线程的,肯定会有多个线程都来获取连接池的单例,所以还需要保证数据库连接池的线程安全问题,

  • CommonConnectionPool类设计:需要通过单例模式实现,并保证数据库连接池的线程安全问题(线程安全的单例),

  • 功能实现:启动连接池构造函数,读取基本的配置文件、初始化连接池数量initSize

CommonConnectionPool.h
#ifndef _COMMONCONNECTIONPOOL_H
#define _COMMONCONNECTIONPOOL_H#include <queue>
#include <string>
#include <mutex>
#include <atomic>
#include <thread>
#include "Connection.h"
using namespace std;// 实现连接池功能模块
class ConnectionPool {
public:static ConnectionPool* getConnectionPool();
private:ConnectionPool();bool loadConfigFile();// 连接池参数int _initSize;				// 初始连接量int _maxSize;				// 最大连接量int _maxIdleTime;			// 最大空闲时间int _connectionTimeout;		// 连接超时时间// 数据库信息string _ip;unsigned short _port;string _username;string _password;string _dbname;// 数据库连接存储queue<Connection*> _connectionq;		// 存储mysql连接的队列mutex _queueMutex;						// 维护连接队列的线程安全的互斥锁atomic_int _connectionCnt;				// 创建连接的数量 ++操作是线程安全的
};#endif
CommonConnectionPool.cpp
#include "public.h"
#include "CommonConnectionPool.h"// 懒汉单例模式
// 对于静态局部变量的初始化 由编译器自动进行 lock与unlock
ConnectionPool* ConnectionPool::getConnectionPool()
{static ConnectionPool pool;return &pool;
}// 初始化连接池
ConnectionPool::ConnectionPool() {// 1.加载配置项if (!loadConfigFile()) return;// 2.创建初始数量的连接for (int i = 0; i < _initSize; ++i) {Connection *p = new Connection();p->connect(_ip, _port, _username, _password, _dbname);_connectionq.push(p);_connectionCnt++;}// 3.启动一个新的线程作为连接的生产者
}// 读取配置文件
bool ConnectionPool::loadConfigFile()
{FILE *fp = fopen("config.conf", "r");if (fp == nullptr) {LOG("Config file error, file not exists!");return false;}while (!feof(fp)) {// 逐行读取配置文件char line[1024] = { 0 };fgets(line, 1024, fp);string str = line;// 查找配置项int idx = str.find('=', 0);if (idx == -1) continue;// 无效配置项int edx = str.find('\n', 0);string key = str.substr(0, idx);string value = str.substr(idx + 1, edx - idx - 1);// 初始化成员变量if (key == "ip") {_ip = value;} else if (key == "port") {_port = atoi(value.c_str());} else if (key == "username") {_username = value;} else if (key == "password") {_password = value;} else if (key == "dbname") {_dbname = value;} else if (key == "initSize") {_initSize = atoi(value.c_str());} else if (key == "maxSize") {_maxSize = atoi(value.c_str());} else if (key == "maxIdleTime") {_maxIdleTime = atoi(value.c_str());} else if (key == "connectionTimeout") {_connectionTimeout = atoi(value.c_str());}}return true;
}
http://www.hkea.cn/news/524349/

相关文章:

  • 地球人--一家只做信誉的网站帮忙推广的平台
  • 网站建设外包协议天津网站排名提升
  • 邯郸教育行业网站建设百度推广代理商查询
  • 政府网站有哪些网站seo最新优化方法
  • 做广告牌子seo外链工具
  • 微信页面设计网站兰州网络推广技术
  • 上门做网站搜狗站长工具
  • wordpress用户邮箱验证码百度seo搜索引擎优化培训
  • 360极速怎么屏蔽网站新闻热点大事件
  • 购物app开发价格表站长工具seo排名
  • 微餐饮网站建设营销型网站建设方案
  • 高端网站建设公司好不好2020国内搜索引擎排行榜
  • 网站建设服务公司选哪家比较好?苏州优化收费
  • 中国建设银行河南省分行网站推广信息哪个平台好
  • 网站建设官网免费模板杭州seo优化
  • 绍兴网站建设谷歌搜索引擎在线
  • 网站的会员认证怎么做黑龙江新闻头条最新消息
  • 做网站如何分工百度推广登录平台客服
  • 网站建设如何提案万网域名注册信息查询
  • 创意二维码制作网站企业网络营销推广案例
  • 论坛型网站怎么做百度高级检索入口
  • 做百度移动网站排搜素引擎优化
  • 公司创建一个网站需要多少钱想做百度推广找谁
  • 做文献ppt模板下载网站有哪些常德政府网站
  • 青岛网站建设公司排行外链工具在线
  • 网站怎么做显得简洁美观seo数据是什么意思
  • 阿里巴巴开通诚信通后网站怎么做网络优化网站
  • 东莞手机网站价格便宜个人免费建站软件
  • 电子商务网站建设的步骤一般为百度100%秒收录
  • 做企业网站怎么样免费的推广软件下载