Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ mysqlpp连接方法超时-我如何控制它?_C++_Mysql_Database Connection_Mysql++ - Fatal编程技术网

C++ mysqlpp连接方法超时-我如何控制它?

C++ mysqlpp连接方法超时-我如何控制它?,c++,mysql,database-connection,mysql++,C++,Mysql,Database Connection,Mysql++,我试图控制连接方法超时,但没有找到合适的平均值 我要说的是,我不是在说空闲连接超时connecttimeoutoption 我需要处理的情况是数据库消失了,我的服务器必须处理这个问题。我目前的处理方式是ping服务器,如果我注意到ping失败,我将暂停查询100秒。在那之后,我试图重新建立联系。问题是,如果数据库仍然死机,那么连接方法大约需要20秒才能回答,只需拉动网络电缆就可以模拟,这对我来说太多了。这应该适合您 #include <mysql++.h> #include <

我试图控制连接方法超时,但没有找到合适的平均值

我要说的是,我不是在说空闲连接超时connecttimeoutoption


我需要处理的情况是数据库消失了,我的服务器必须处理这个问题。我目前的处理方式是ping服务器,如果我注意到ping失败,我将暂停查询100秒。在那之后,我试图重新建立联系。问题是,如果数据库仍然死机,那么连接方法大约需要20秒才能回答,只需拉动网络电缆就可以模拟,这对我来说太多了。

这应该适合您

#include <mysql++.h>
#include <cstdio>

int main()
{
   mysqlpp::Connection conn;
   conn.set_option(new mysqlpp::ReconnectOption(true));
   conn.set_option(new mysqlpp::ConnectTimeoutOption(5));

   const std::string db="mysql_cpp_data";
   const std::string query_text="SELECT count(*) as total FROM stock";
   conn.connect(db.c_str(), "somehost", "user", "pass");

   try
   {
      mysqlpp::Query query=conn.query();
      query << query_text;
      mysqlpp::StoreQueryResult res=query.store();
      std::cout << "Has " << (*res.begin())[0] << " rows\n";
   }
   catch(const mysqlpp::BadQuery &e)
   {
      std::cout << "EXCEPTION: " << e.what() << std::endl;
   }
   std::cout << "Make database go away now and press a key\n";
   getchar();

   try
   {
      mysqlpp::Query query=conn.query();
      query << query_text;
      mysqlpp::StoreQueryResult res=query.store();
      std::cout << "Has " << (*res.begin())[0] << " rows\n";
   }
   catch(const mysqlpp::BadQuery &e)
   {
      std::cout << "EXCEPTION: " << e.what() << std::endl;
      std::cout << "Make database come back now and press a key\n";
      getchar();
      while(!conn.ping())
      {
         sleep(1);
         std::cout << "Waiting for DB to come back\n";
      }
      if(!conn.select_db(db))
      {
         std::cout << "Failed to change DB\n";
      }
   }

   try
   {
      mysqlpp::Query query=conn.query();
      query=conn.query();
      query << query_text;
      mysqlpp::StoreQueryResult res=query.store();
      std::cout << "Has " << (*res.begin())[0] << " rows\n";
   }
   catch(const mysqlpp::BadQuery &e)
   {
      std::cout << "EXCEPTION: " << e.what() << " " << e.errnum() << std::endl;
   }

   return 0;
}
试试这个

    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    #include <cmdline.h>
    #include <mysql++.h>

    #define DBS "library"
    #define USR "root"
    #define PAS "rootsman"
    using namespace std;
    using namespace mysqlpp;

    int main(int argc, char *argv[]) {
        //for true cgi but in this case it works, kind of baffling!
        mysqlpp::examples::CommandLine cmdline(argc, argv, USR, PAS);
        if (!cmdline) return 1;

        mysqlpp::Connection conn(true);
        conn.set_option(new mysqlpp::ReconnectOption(true));
        conn.set_option(new mysqlpp::ConnectTimeoutOption(5));
        conn.connect(DBS, cmdline.server(), cmdline.user(), cmdline.pass());

        try {
            mysqlpp::String sql("select firstname from person");
            mysqlpp::Query query = conn.query(sql);
            mysqlpp::StoreQueryResult res = query.store();

            mysqlpp::StoreQueryResult::const_iterator it;
            int count = 1;
            for (it = res.begin(); it != res.end(); ++it) {
                mysqlpp::Row row = *it;
                cout << count << "\t" << row[0] << endl;
                ++count;
            }
        } catch (const mysqlpp::BadQuery& bq) {
            cerr << "query error: " << bq.what() << endl;
            return -1;
        }
        cout << "\nmake database fly away now by pressing a key>" << endl;
        getchar();

        try {
            mysqlpp::Query query = conn.query();
            mysqlpp::String sql("select count(*) as total from person");
            query << sql;
            mysqlpp::StoreQueryResult res = query.store();
            cout << "has " << (*res.begin())[0] << " rows" << endl;

        } catch (mysqlpp::BadQuery& e) {
            cerr << "\n bad query 2>" << e.what() << endl;
            cout << "\nmake database fly back now by pressing enter>" << endl;

            while (!conn.ping()) {
                //sleep(1);
                cout << "\nwaiting for database to fly back>" << endl;
            }
            if (!conn.select_db(DBS)) {
                cerr << "\nfailed to reconnect" << endl;
            }
        }

        try {
            mysqlpp::Query query = conn.query();
            //this is how my relation and its attributes looks like
            String sql("insert into person(firstname,lastname,gender,love,angry,"
            "forgiving) values('joy57/qxx','crimson','male','high','medium','high');");
            query << sql;
            query.execute();
            cerr << "\n **inserted successfully**\n" << endl;
        } catch (mysqlpp::BadQuery& e) {
            cerr << "bad query 3>" << e.what() << e.errnum() << endl;
            return -1;
        }

        cerr << "Jesus helps me when i stumble and fall" << endl;
        return 0;
    }