简体   繁体   English

C ++-MySQL连接器

[英]C++ - Mysql connector

I'm using the c++ mysql connector to do operations in my mysql database. 我正在使用c ++ mysql连接器在mysql数据库中进行操作。

My c++ program is a real time application (rest api) which is always running in the cloud, always waiting for user requests. 我的c ++程序是一个实时应用程序(rest api),始终在云中运行,始终在等待用户请求。

When i start my program for the first type i automatically create a connection to the database (the fields for the connector i load from a configuration file). 当我为第一种类型启动程序时,我会自动创建与数据库的连接(我从配置文件中加载连接器的字段)。 Example: 例:

conDataBase = new ConDatabase;
if (!conDataBase->Init()) return false;

The conDataBase is a global pointer accessible to all classes. conDataBase是所有类均可访问的全局指针。 The Init() function: Init()函数:

bool conDatabase::Init()
{
  GetParameterStr("DATABASE", "HOST", "", hostname, 255);
  db_hostname = hostname;
  GetParameterStr("DATABASE", "USER", "", user, 255);
  db_user = user;
  GetParameterStr("DATABASE", "PASSWORD", "", password, 255);
  db_password = password;
  GetParameterStr("DATABASE", "SCHEMA", "", schema, 255);
  db_schema = schema;
  printf("DATABASE: Connecting to %s \n",db_hostname.c_str());
  printf("DATABASE: Connecting at %s with user %s \n",db_schema.c_str(), db_user.c_str());

  try
  {
    driver = get_driver_instance();
    con = driver->connect(db_hostname.c_str(), db_user.c_str(), db_password.c_str());
    con->setSchema(db_schema.c_str());
    stmt = con->createStatement();
    printf("DATABASE: Connected to database... OK \n");
    return 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 false;
  }
}

So when i receive a request for example to list the userInfo in the userInfo request class i call the global pointer for the database class like this: 因此,当我收到一个请求,例如在userInfo请求类中列出userInfo时,我像这样调用数据库类的全局指针:

conDataBase->GetUserInfo(// the parameters);

Inside the GetUserInfo() i build my query like this: GetUserInfo()内部,我像这样构建查询:

res = stmt->executeQuery(query);

Its works but my real doubt is: Its is necessary to delete the pointer from mysqlconnector ( res, pstmt, con, etc )?. 它的工作原理,但我真正的疑问是:是否有必要从mysqlconnector中删除指针( res, pstmt, con, etc )? I'm scary about memory leaks in future. 我担心将来会发生内存泄漏。 I'm only delete the pointers when the program exits but it is a real time program reason why he is not expected to be finished. 我只是在程序退出时才删除指针,但这是实时程序的原因,因此不能期望他完成操作。 If i delete the pointer in each query, insert etc (like the mysqlconnector examples do) in next time i have segmentation fault because when i run the program in first time i create the database pointers con, res, etc, so i cannot delete these pointers in each database operation because if i do this, in next time the pointers are deleted and i dont have access to him and its result in segmentation fault. 如果我在每个查询中删除指针,则在下一次出现分段错误时插入等(如mysqlconnector示例一样),因为在我第一次运行程序时,我会创建数据库指针con, res, etc,因此我无法删除这些指针指针在每个数据库操作中,因为如果执行此操作,则下次删除指针时,我将无法访问他,从而导致分段错误。 Whats is the solution in these case to prevent memory leaks in future? 在这种情况下,如何防止将来发生内存泄漏?

For such cases you can write a connectionManager class. 在这种情况下,您可以编写connectionManager类。 It can be used to provide api's for : 它可以用来为以下对象提供api:

1- creating and maintaining connection pool , 1-创建和维护连接池

2- getConnection api to get a connection instance from the pool, 2-getConnection api从池中获取连接实例,

3- a release connection api to put the connection instance back into opened connection's pool, 3-一个释放连接API,用于将连接实例放回打开的连接池中,

4- you should be using stl containers to store opened connections, etc 4-您应该使用stl容器存储打开的连接等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM