2019-07-27 22:44:32 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
#include <list>
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../db/db_handle.hpp"
|
|
|
|
|
#include "../connpool/connect_pool.hpp"
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
2019-05-19 16:54:20 +08:00
|
|
|
|
#if !defined(ACL_DB_DISABLE)
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
namespace acl {
|
|
|
|
|
|
|
|
|
|
class db_handle;
|
|
|
|
|
class locker;
|
|
|
|
|
|
2015-08-12 16:48:24 +08:00
|
|
|
|
class ACL_CPP_API db_pool : public connect_pool
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 数据库构造函数
|
|
|
|
|
* @param dbaddr {const char*} 数据库地址
|
|
|
|
|
* @param count {size_t} 连接池最大连接个数限制
|
|
|
|
|
* @param idx {size_t} 该连接池对象在集合中的下标位置(从 0 开始)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-14 17:28:30 +08:00
|
|
|
|
db_pool(const char* dbaddr, size_t count, size_t idx = 0);
|
2015-08-12 16:48:24 +08:00
|
|
|
|
virtual ~db_pool() {};
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 从数据库连接池获得一个数据库对象,并且要求打开数据库连接,即用户不必
|
|
|
|
|
* 显式地再调用 db_handle::open 过程;
|
|
|
|
|
* 用完后必须调用 db_pool->put(db_handle*) 将连接归还至数据库连接池,
|
|
|
|
|
* 由该函数获得的连接句柄不能 delete,否则会造成连接池的内部计数器出错
|
|
|
|
|
* @return {db_handle*} 数据库连接对象,返回空表示出错
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2016-11-03 11:19:11 +08:00
|
|
|
|
db_handle* peek_open();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得当前数据库连接池的最大连接数限制
|
2015-08-14 17:28:30 +08:00
|
|
|
|
* @return {size_t}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-14 17:28:30 +08:00
|
|
|
|
size_t get_dblimit() const
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2015-08-12 16:48:24 +08:00
|
|
|
|
return get_max();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得当前数据库连接池当前的连接数
|
2015-08-14 17:28:30 +08:00
|
|
|
|
* @return {size_t}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-14 17:28:30 +08:00
|
|
|
|
size_t get_dbcount() const
|
2014-11-19 00:25:21 +08:00
|
|
|
|
{
|
2015-08-12 16:48:24 +08:00
|
|
|
|
return get_count();
|
2014-11-19 00:25:21 +08:00
|
|
|
|
}
|
2015-08-12 16:48:24 +08:00
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置数据库连接池中空闲连接的生存周期(秒)
|
|
|
|
|
* @param ttl {int} 生存周期(秒)
|
2014-11-19 00:25:21 +08:00
|
|
|
|
*/
|
2015-08-12 16:48:24 +08:00
|
|
|
|
void set_idle(int ttl)
|
|
|
|
|
{
|
|
|
|
|
set_idle_ttl(ttl);
|
|
|
|
|
}
|
2014-11-19 00:25:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
2016-01-17 13:35:12 +08:00
|
|
|
|
class ACL_CPP_API db_guard : public connect_guard
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
db_guard(db_pool& pool) : connect_guard(pool) {}
|
|
|
|
|
~db_guard(void);
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-19 00:25:21 +08:00
|
|
|
|
} // namespace acl
|
2019-05-19 16:54:20 +08:00
|
|
|
|
|
|
|
|
|
#endif // !defined(ACL_DB_DISABLE)
|