Skip to content

Commit e8a81e0

Browse files
committed
recode the program
1 parent d031f99 commit e8a81e0

File tree

138 files changed

+3847
-8293
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+3847
-8293
lines changed

CGImysql/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
校验 & 数据库连接池
3+
===============
4+
数据库连接池
5+
> * 单例模式,保证唯一
6+
> * list实现连接池
7+
> * 连接池为静态大小
8+
> * 互斥锁实现线程安全
9+
10+
校验
11+
> * HTTP请求采用POST方式
12+
> * 登录用户名和密码校验
13+
> * 用户注册及多线程注册安全

CGImysql/sql_connection_pool.cpp

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include <mysql/mysql.h>
2+
#include <stdio.h>
3+
#include <string>
4+
#include <string.h>
5+
#include <stdlib.h>
6+
#include <list>
7+
#include <pthread.h>
8+
#include <iostream>
9+
#include "sql_connection_pool.h"
10+
11+
using namespace std;
12+
13+
connection_pool::connection_pool()
14+
{
15+
m_CurConn = 0;
16+
m_FreeConn = 0;
17+
}
18+
19+
connection_pool *connection_pool::GetInstance()
20+
{
21+
static connection_pool connPool;
22+
return &connPool;
23+
}
24+
25+
//构造初始化
26+
void connection_pool::init(string url, string User, string PassWord, string DBName, int Port, int MaxConn, int close_log)
27+
{
28+
m_url = url;
29+
m_Port = Port;
30+
m_User = User;
31+
m_PassWord = PassWord;
32+
m_DatabaseName = DBName;
33+
m_close_log = close_log;
34+
35+
for (int i = 0; i < MaxConn; i++)
36+
{
37+
MYSQL *con = NULL;
38+
con = mysql_init(con);
39+
40+
if (con == NULL)
41+
{
42+
LOG_ERROR("MySQL Error");
43+
exit(1);
44+
}
45+
con = mysql_real_connect(con, url.c_str(), User.c_str(), PassWord.c_str(), DBName.c_str(), Port, NULL, 0);
46+
47+
if (con == NULL)
48+
{
49+
LOG_ERROR("MySQL Error");
50+
exit(1);
51+
}
52+
connList.push_back(con);
53+
++m_FreeConn;
54+
}
55+
56+
reserve = sem(m_FreeConn);
57+
58+
m_MaxConn = m_FreeConn;
59+
}
60+
61+
62+
//当有请求时,从数据库连接池中返回一个可用连接,更新使用和空闲连接数
63+
MYSQL *connection_pool::GetConnection()
64+
{
65+
MYSQL *con = NULL;
66+
67+
if (0 == connList.size())
68+
return NULL;
69+
70+
reserve.wait();
71+
72+
lock.lock();
73+
74+
con = connList.front();
75+
connList.pop_front();
76+
77+
--m_FreeConn;
78+
++m_CurConn;
79+
80+
lock.unlock();
81+
return con;
82+
}
83+
84+
//释放当前使用的连接
85+
bool connection_pool::ReleaseConnection(MYSQL *con)
86+
{
87+
if (NULL == con)
88+
return false;
89+
90+
lock.lock();
91+
92+
connList.push_back(con);
93+
++m_FreeConn;
94+
--m_CurConn;
95+
96+
lock.unlock();
97+
98+
reserve.post();
99+
return true;
100+
}
101+
102+
//销毁数据库连接池
103+
void connection_pool::DestroyPool()
104+
{
105+
106+
lock.lock();
107+
if (connList.size() > 0)
108+
{
109+
list<MYSQL *>::iterator it;
110+
for (it = connList.begin(); it != connList.end(); ++it)
111+
{
112+
MYSQL *con = *it;
113+
mysql_close(con);
114+
}
115+
m_CurConn = 0;
116+
m_FreeConn = 0;
117+
connList.clear();
118+
}
119+
120+
lock.unlock();
121+
}
122+
123+
//当前空闲的连接数
124+
int connection_pool::GetFreeConn()
125+
{
126+
return this->m_FreeConn;
127+
}
128+
129+
connection_pool::~connection_pool()
130+
{
131+
DestroyPool();
132+
}
133+
134+
connectionRAII::connectionRAII(MYSQL **SQL, connection_pool *connPool){
135+
*SQL = connPool->GetConnection();
136+
137+
conRAII = *SQL;
138+
poolRAII = connPool;
139+
}
140+
141+
connectionRAII::~connectionRAII(){
142+
poolRAII->ReleaseConnection(conRAII);
143+
}

CGImysql/sql_connection_pool.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifndef _CONNECTION_POOL_
2+
#define _CONNECTION_POOL_
3+
4+
#include <stdio.h>
5+
#include <list>
6+
#include <mysql/mysql.h>
7+
#include <error.h>
8+
#include <string.h>
9+
#include <iostream>
10+
#include <string>
11+
#include "../lock/locker.h"
12+
#include "../log/log.h"
13+
14+
using namespace std;
15+
16+
class connection_pool
17+
{
18+
public:
19+
MYSQL *GetConnection(); //获取数据库连接
20+
bool ReleaseConnection(MYSQL *conn); //释放连接
21+
int GetFreeConn(); //获取连接
22+
void DestroyPool(); //销毁所有连接
23+
24+
//单例模式 这个是利用局部静态变量懒汉模式实现单例(多线程不友好)
25+
//对于多线程而言,多个线程可能同时访问到该静态变量,并发现其没有被初始化(C++实现机制是)
26+
//(该看静态变量内部一标志位是为1,为1则说明已被初始化)
27+
//多个线程同时判定其没有初始化而后均初始化就会造成错误(即它不是一个原子操作)
28+
static connection_pool *GetInstance();
29+
30+
void init(string url, string User, string PassWord, string DataBaseName, int Port, int MaxConn, int close_log);
31+
32+
private:
33+
connection_pool();
34+
~connection_pool();
35+
36+
int m_MaxConn; //最大连接数
37+
int m_CurConn; //当前已使用的连接数
38+
int m_FreeConn; //当前空闲的连接数
39+
locker lock;
40+
list<MYSQL *> connList; //连接池
41+
sem reserve;
42+
43+
public:
44+
string m_url; //主机地址
45+
string m_Port; //数据库端口号
46+
string m_User; //登陆数据库用户名
47+
string m_PassWord; //登陆数据库密码
48+
string m_DatabaseName; //使用数据库名
49+
int m_close_log; //日志开关
50+
};
51+
52+
class connectionRAII{
53+
54+
public:
55+
connectionRAII(MYSQL **con, connection_pool *connPool);
56+
~connectionRAII();
57+
58+
private:
59+
MYSQL *conRAII;
60+
connection_pool *poolRAII;
61+
};
62+
63+
#endif

0 commit comments

Comments
 (0)