1

I have a class to connect MySQL database. This class has 4 methods. (insert, getResults etc.) I don't want to create database connection in every method. So i want an init() when we create this object. Is connection pool solution of my problem? How can i solve?

Have 4 methods like that:

bool DataAccessObject::getResults(short int data, std::vector<FaceRecord>* rec)
{
//    DataAccessObject *temp = new DataAccessObject();

    bool ret = false;

    try{
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;
        sql::PreparedStatement *prepStmt;

        /* Create a connection */
        driver = get_driver_instance();
        con = driver->connect("tcp://127.0.0.1:3306", "root", "root");

        /* Connect to the MySQL test database */
        con->setSchema("test");

        std::stringstream s;
        s << "SELECT * FROM Amts WHERE "<< data <<" = "<< data <<"";

        prepStmt = con->prepareStatement (s.str());
        res = prepStmt->executeQuery();

        while(res->next()){
            tempFR.uuId = res->getInt64("uuId");
            tempFR.cameraNo = res->getInt("cameraNo");
            tempFR.age = res->getInt("age");
            tempFR.gender = res->getInt("gender");
            tempFR.time = res->getString("time");
            tempFR.image = res->getString("image");
            rec->push_back(tempFR);
        }


        //return true;
        ret = true;
    }

    catch (sql::SQLException &e)
    {

        std::cout << "# ERR: SQLException in " << __FILE__;
        std::cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl;
        std::cout << "# ERR: " << e.what();
        std::cout << " (MySQL error code: " << e.getErrorCode();
        std::cout << ", SQLState: " << e.getSQLState() << " )" << std::endl;

    }

    return ret;

}
Ulfsark
  • 902
  • 1
  • 11
  • 26
  • 3
    You just solved the problem yourself. It's unclear what you're asking, since you didn't even type all the methods, and noone can possibly know what class are you really using. – Paweł Stawarz Jan 14 '14 at 16:00
  • Check the documentation for the database driver. It may natively implement connection pooling, in which case there's no need for you to do so. – Sean Jan 14 '14 at 16:17
  • i have added a method to my question. I want to remove "Create a connection" field and before. – Ulfsark Jan 14 '14 at 16:20
  • You do not want to pass the connection? – user2672165 Jan 14 '14 at 16:49
  • I want to pass the connection. So I want to create connection in constructor of this class, or in an init function, then use this methods(insert,retrieve) without create connection. – Ulfsark Jan 14 '14 at 16:54
  • I meant that the connection could be passed into the function as an argument or you could use the suggestion by Dannie Sim. – user2672165 Jan 14 '14 at 19:36

1 Answers1

1

You can use the C++ Singleton design pattern so that your init is called only once when you create it.

Community
  • 1
  • 1
Dannie
  • 2,430
  • 14
  • 16