赞
踩
第二个工具类:DBService
用于MySQL数据库服务类的基类。主要处理数据库连接的建立(Start),断开(Stop),维持心跳(Ping)。
基于之前的DBOperator实现。
该基类提供两个虚接口,ProcessStart,ProcessStop,交由子类去实现子类自己的初始化工作,和释放工作。
建立连接(Start)时,会断开现有连接(如果有的话,会先调用ProcessStop做清理工作),然后建立新连接,连接建立好之后调用ProcessStart。
断开连接(Stop)时,调用ProcessStop做清理工作,然后断开现有连接。
维持心跳(Ping)时,如果发现连接断开,则重新连接。如果连不上则放弃,等待下一次Ping,或逻辑主动调用Start重新建立连接。
DBService中保存一个建立好的数据库连接,并加锁,保证多线程同时访问DBService时线程安全。
具体会在这个连接上,做哪些数据库操作,做什么样的增删改查,由子类实现,它跟具体业务逻辑相关。
DBService.h
- #ifndef __DBService_H__
- #define __DBService_H__
-
- #include <boost/thread.hpp>
-
- struct st_mysql;
- typedef struct st_mysql MYSQL;
-
- namespace common{
- namespace db{
-
- class DBService
- {
- public:
- DBService();
- virtual ~DBService();
-
- public:
- // 建立数据库连接
- bool Start(const std::string& hostname,
- unsigned int port,
- const std::string& username,
- const std::string& userkey,
- const std::string& dbname);
- virtual bool ProcessStart();
-
- // 维持数据库连接心跳,校验链接是否断开,断开则重连
- void Ping();
-
- // 断开数据库连接
- void Stop();
- virtual void ProcessStop();
-
- protected:
- // 数据库连接
- boost::mutex m_Lock;
- MYSQL *m_Connect;
-
- private:
- // 建立新链接
- bool Connect();
- // 终止链接
- void DisConnect();
-
- // 数据库连接参数
- std::string m_HostName;
- unsigned int m_Port;
- std::string m_UserName;
- std::string m_UserKey;
- std::string m_DBName;
- };
-
- }
- }
-
- #endif
DBService.cpp
- #include "DBService.h"
-
- #include "Logger.h"
-
- using namespace common::tool;
-
- #include "DBDefine.h"
- #include "DBOperator.h"
-
- namespace common{
- namespace db{
-
- DBService::DBService()
- {
- m_Connect = NULL;
- }
-
- DBService::~DBService()
- {
- DisConnect();
- }
-
- bool DBService::Start(const std::string& hostname,
- unsigned int port,
- const std::string& username,
- const std::string& userkey,
- const std::string& dbname)
- {
- boost::mutex::scoped_lock lock(m_Lock);
-
- DisConnect();
-
- m_HostName = hostname;
- m_Port = port;
- m_UserName = username;
- m_UserKey = userkey;
- m_DBName = dbname;
-
- return Connect();
- }
-
- bool DBService::ProcessStart()
- {
- return false;
- }
-
- void DBService::Ping()
- {
- boost::mutex::scoped_try_lock lock(m_Lock);
- if (lock.owns_lock())
- {
- if (!DBOperator::Ping(m_Connect))
- {
- DisConnect();
- Connect();
- }
- }
- }
-
- void DBService::Stop()
- {
- boost::mutex::scoped_lock lock(m_Lock);
-
- DisConnect();
- }
-
- void DBService::ProcessStop()
- {
-
- }
-
- bool DBService::Connect()
- {
- m_Connect = DBOperator::Connect(m_HostName.c_str(), m_Port, m_UserName.c_str(), m_UserKey.c_str(), m_DBName.c_str());
- if (NULL != m_Connect)
- {
- if (ProcessStart())
- {
- LOG_INFO(g_LibDBLog) << "connect " << m_HostName << ":" << m_DBName << "@" << m_UserName << " success";
- return true;
- }
- else
- {
- LOG_INFO(g_LibDBLog) << "connect " << m_HostName << ":" << m_DBName << "@" << m_UserName << " error";
- DisConnect();
- return false;
- }
- }
- else
- {
- LOG_INFO(g_LibDBLog) << "connect " << m_HostName << ":" << m_DBName << "@" << m_UserName << " error";
- return false;
- }
- }
-
- void DBService::DisConnect()
- {
- if (NULL != m_Connect)
- {
- LOG_INFO(g_LibDBLog) << "close " << m_HostName << ":" << m_DBName << "@" << m_UserName;
-
- ProcessStop();
-
- DBOperator::DisConnect(m_Connect);
- m_Connect = NULL;
- }
- }
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。