//------------------------------------------------------------------------------------- bool ServerApp::initialize() { if(!initThreadPool()) return false; if(!installSignals()) return false; if(!loadConfig()) return false; if(!initializeBegin()) return false; if(!inInitialize()) return false; bool ret = initializeEnd(); #ifdef ENABLE_WATCHERS return ret && initializeWatcher(); #else return ret; #endif }
//------------------------------------------------------------------------------------- bool ServerApp::initialize() { if(!initThreadPool()) return false; if(!installSingnals()) return false; if(!loadConfig()) return false; if(!initializeBegin()) return false; if(!inInitialize()) return false; // 广播自己的地址给网上上的所有kbemachine // 并且从kbemachine获取basappmgr和cellappmgr以及dbmgr地址 Componentbridge::getSingleton().getComponents().pHandler(this); this->getMainDispatcher().addFrequentTask(&Componentbridge::getSingleton()); bool ret = initializeEnd(); #ifdef ENABLE_WATCHERS return ret && initializeWatcher(); #else return ret; #endif }
//------------------------------------------------------------------------------------- bool DBInterfaceMysql::attach(const char* databaseName) { if(!_g_installedWatcher) { initializeWatcher(); } if(db_port_ == 0) db_port_ = 3306; if(databaseName != NULL) kbe_snprintf(db_name_, MAX_BUF, "%s", databaseName); hasLostConnection_ = false; try { pMysql_ = mysql_init(0); if(pMysql_ == NULL) { ERROR_MSG("DBInterfaceMysql::attach: mysql_init is error!\n"); return false; } DEBUG_MSG(boost::format("DBInterfaceMysql::attach: connect: %1%:%2% starting...\n") % db_ip_ % db_port_); if(mysql_real_connect(mysql(), db_ip_, db_username_, db_password_, db_name_, db_port_, NULL, 0)) // CLIENT_MULTI_STATEMENTS { if(mysql_select_db(mysql(), db_name_) != 0) { ERROR_MSG(boost::format("DBInterfaceMysql::attach: Could not set active db[%1%]\n") % db_name_); return false; } } else { ERROR_MSG(boost::format("DBInterfaceMysql::attach: mysql_errno=%1%, mysql_error=%2%\n") % mysql_errno(pMysql_) % mysql_error(pMysql_)); return false; } if (mysql_set_character_set(mysql(), "utf8") != 0) { ERROR_MSG("DBInterfaceMysql::attach: Could not set client connection character set to UTF-8\n" ); } char characterset_sql[MAX_BUF]; kbe_snprintf(characterset_sql, MAX_BUF, "ALTER DATABASE CHARACTER SET %s COLLATE %s", characterSet_.c_str(), collation_.c_str()); query(&characterset_sql[0], strlen(characterset_sql), false); } catch (std::exception& e) { ERROR_MSG(boost::format("DBInterfaceMysql::attach: %1%\n") % e.what()); hasLostConnection_ = true; return false; } bool ret = mysql() != NULL && ping(); if(ret) { DEBUG_MSG(boost::format("DBInterfaceMysql::attach: successfully! addr: %1%:%2%\n") % db_ip_ % db_port_); } return ret; }
//------------------------------------------------------------------------------------- bool DBInterfaceMysql::attach(const char* databaseName) { if(!_g_installedWatcher) { initializeWatcher(); } if(db_port_ == 0) db_port_ = 3306; if(databaseName != NULL) kbe_snprintf(db_name_, MAX_BUF, "%s", databaseName); hasLostConnection_ = false; try { pMysql_ = mysql_init(0); if(pMysql_ == NULL) { ERROR_MSG("DBInterfaceMysql::attach: mysql_init is error!\n"); return false; } DEBUG_MSG(fmt::format("DBInterfaceMysql::attach: connect: {}:{} starting...\n", db_ip_, db_port_)); int ntry = 0; __RECONNECT: if(mysql_real_connect(mysql(), db_ip_, db_username_, db_password_, db_name_, db_port_, NULL, 0)) // CLIENT_MULTI_STATEMENTS { if(mysql_select_db(mysql(), db_name_) != 0) { ERROR_MSG(fmt::format("DBInterfaceMysql::attach: Could not set active db[{}]\n", db_name_)); detach(); return false; } } else { if (mysql_errno(pMysql_) == 1049 && ntry++ == 0) { if (mysql()) { ::mysql_close(mysql()); pMysql_ = NULL; } pMysql_ = mysql_init(0); if (pMysql_ == NULL) { ERROR_MSG("DBInterfaceMysql::attach: mysql_init is error!\n"); return false; } if (mysql_real_connect(mysql(), db_ip_, db_username_, db_password_, NULL, db_port_, NULL, 0)) // CLIENT_MULTI_STATEMENTS { this->createDatabaseIfNotExist(); if (mysql_select_db(mysql(), db_name_) != 0) { goto __RECONNECT; } } else { goto __RECONNECT; } } else { ERROR_MSG(fmt::format("DBInterfaceMysql::attach: mysql_errno={}, mysql_error={}\n", mysql_errno(pMysql_), mysql_error(pMysql_))); detach(); return false; } } if (mysql_set_character_set(mysql(), "utf8") != 0) { ERROR_MSG("DBInterfaceMysql::attach: Could not set client connection character set to UTF-8\n" ); return false; } // 不需要关闭自动提交,底层会START TRANSACTION之后再COMMIT // mysql_autocommit(mysql(), 0); char characterset_sql[MAX_BUF]; kbe_snprintf(characterset_sql, MAX_BUF, "ALTER DATABASE CHARACTER SET %s COLLATE %s", characterSet_.c_str(), collation_.c_str()); query(&characterset_sql[0], strlen(characterset_sql), false); } catch (std::exception& e) { ERROR_MSG(fmt::format("DBInterfaceMysql::attach: {}\n", e.what())); hasLostConnection_ = true; detach(); return false; } bool ret = mysql() != NULL && ping(); if(ret) { DEBUG_MSG(fmt::format("DBInterfaceMysql::attach: successfully! addr: {}:{}\n", db_ip_, db_port_)); } return ret; }