Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
navono opened this issue Apr 3, 2024 · 3 comments
Open

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

navono opened this issue Apr 3, 2024 · 3 comments

Comments

@navono
Copy link

navono commented Apr 3, 2024

每次查询前都是以下面代码获取 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查询,会导致整体查询异常缓慢。

@navono navono changed the title 使用 connection pool 使用 connection pool,每次查询都会新建连接 Apr 3, 2024
@qicosmos
Copy link
Owner

qicosmos commented Apr 3, 2024

谢谢报告,稍后看一下。

@Jacyking
Copy link
Collaborator

Jacyking commented Apr 7, 2024

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

@navono
Copy link
Author

navono commented Apr 23, 2024

我用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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants