資料庫MySql類庫系列 二 DBService

2021-07-27 23:03:56 字數 1410 閱讀 6739

第二個工具類: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 struct st_mysql;

typedef struct st_mysql mysql;

namespace common; }}

#endif

dbservice.cpp

#include "dbservice.h"

#include "logger.h"

using namespace common::tool;

#include "dbdefine.h"

#include "dboperator.h"

namespace common

dbservice::~dbservice()

bool dbservice::start(const std::string& hostname,

unsigned int port,

const std::string& username,

const std::string& userkey,

const std::string& dbname)

bool dbservice::processstart()

void dbservice::ping()

}} void dbservice::stop()

void dbservice::processstop()

bool dbservice::connect()

else

}else

}void dbservice::disconnect()

}}}

mysql資料庫 DB類

mysql資料庫 db類 package db author yytcpt 無影 version 2008 03 27 copyrigth class db else if this connection id if mysql select db db config database this c...

mysql資料庫類 MySQL資料庫類的定義

俗話說 好的開始是成功的一半 而php mysql專案中資料庫的操作是重點之一,能否簡化資料庫操作程式的編寫,就成了影響工作效率的關鍵之一。所以小陽並不是一開始就做頁面,而是先建立乙個 dbclass.php 檔案,開始編寫操作mysql資料庫的類 dbclass 即在 dbclass.php 中編...

Oracle資料庫系列二

1.查詢結果排序 在查詢排序中,根據指定列排序,除了使用列名之外,還可以使用列的序號 如order by empno,empname order by 1,2 根據多個字段排序 order by a asc,b desc 2.字串擷取函式substr 語法 substr 字串,擷取開始位置,擷取長度...