qicosmos / ormpp

modern C++ ORM, C++17, support mysql, postgresql,sqlite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

使用 connection pool,每次查询都会新建连接

navono opened this issue · comments

每次查询前都是以下面代码获取 conn

  auto& pool = connection_pool<dbng<postgresql>>::instance();
  auto conn = pool.get();
  if (nullptr == conn) {
    XLOG_ERROR("get sql connection failed");
    return std::nullopt;
  }
  conn_guard guard(conn);

初始化代码

  auto& pool = connection_pool<dbng<postgresql>>::instance();
  const auto& config = config::TheWebServerConfig::get_instance().get_db_config();
  try {
    pool.init(MAX_POOL_SIZE, config.ip.c_str(), config.username.c_str(), config.password.c_str(), config.db.c_str(), 30, config.port);
  } catch (const std::exception& e) {
    XLOG_ERROR("pool init failed: {}", e.what());
    return false;
  }

但是每次查询,均是新建连接:

[2024-04-03 16:08:15.609487] [46748:9288] [trace] [HttpServer::HttpServer::register_handler::<lambda_1>::operator ()] request path: /version with [GET] from 10.30.26.102
select id,name,password,admin,login_at,logout_at,operation_user_id,created_at,updated_at  from UserInfo  where 1=1 and name='admin' 

host = 127.0.0.1   user = postgres   password = 1234   dbname = postgres   connect_timeout = 30   port = 5432   
[2024-04-03 16:08:16.355078] [46748:31768] [trace] [HttpServer::HttpServer::register_handler::<lambda_2>::operator ()] response path: /global/industry with [GET] to 10.30.26.102
select id,name,created_at,updated_at  from Industry 
[2024-04-03 16:08:16.355389] [46748:31768] [trace] [HttpServer::HttpServer::register_handler::<lambda_1>::operator ()] request path: /global/vendor with [GET] from 10.30.26.102

host = 127.0.0.1   user = postgres   password = 1234   dbname = postgres   connect_timeout = 30   port = 5432   
select id,name,password,admin,login_at,logout_at,operation_user_id,created_at,updated_at  from UserInfo  where 1=1 and name='admin' 

如果业务代码中,在遍历结果中再次使用 db查询,会导致整体查询异常缓慢。

谢谢报告,稍后看一下。

我用mysql看是没问题的,这段代码如何证明新建了连接?有可视化工具嘛

我用mysql看是没问题的,这段代码如何证明新建了连接?有可视化工具嘛

数据库用的是 pg。
每次从 pool 中 get 实例时,pool 每次都是 create connection。一般正常情况下,ormpp 只 print 查询语句。如果出现了

host = 127.0.0.1 user = postgres password = 1234 dbname = postgres connect_timeout = 30 port = 5432

一般都是进行了连接,因为它是新建连接的日志。