acl/lib_acl_cpp/include/acl_cpp/db/db_pool.hpp

73 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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