From fb59286dd47c77101c9f3d9842365cb493bca5f9 Mon Sep 17 00:00:00 2001 From: jisangs Date: Wed, 7 Oct 2026 13:59:59 +0900 Subject: [PATCH] =?UTF-8?q?PHP=20-=20DB=20=EC=97=B0=EA=B2=B0=EC=97=90=20co?= =?UTF-8?q?nnection=20pool=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- deploy/stage-git/Dockerfile | 5 +++++ src/web/server/setup/connect_db.php | 22 +++++++++++++++++----- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ae2f51a..ab238ef 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ __pycache__/ *.pyc # mac -.DS_Store \ No newline at end of file +.DS_Store +/doc/obsidian \ No newline at end of file diff --git a/deploy/stage-git/Dockerfile b/deploy/stage-git/Dockerfile index c45387b..ad7295e 100644 --- a/deploy/stage-git/Dockerfile +++ b/deploy/stage-git/Dockerfile @@ -20,6 +20,11 @@ RUN { \ ENV TZ=Asia/Seoul RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone +RUN { \ + echo 'mysqli.max_persistent = 1'; \ + echo 'mysqli.max_links = -1'; \ +} > /usr/local/etc/php/conf.d/mysqli-pool.ini + RUN printf '#!/bin/sh\ncp /var/www/html/mouse_typing/index.html /var/www/html/index.html\nexec "$@"\n' \ > /usr/local/bin/entrypoint.sh \ && chmod +x /usr/local/bin/entrypoint.sh diff --git a/src/web/server/setup/connect_db.php b/src/web/server/setup/connect_db.php index 5880045..27aaf50 100644 --- a/src/web/server/setup/connect_db.php +++ b/src/web/server/setup/connect_db.php @@ -13,15 +13,27 @@ if($hasServiceDBSetting == true) { if (!isset($db_port)) $db_port = 3306; -// connect db -$db_conn = new mysqli($db_host, $db_user, $db_passwd, $db_name, $db_port); +// connect db with persistent connection (connection pool) +$db_conn = new mysqli("p:".$db_host, $db_user, $db_passwd, $db_name, $db_port); + if ($db_conn->connect_error) { set_error_message("DB connection failed."); send_result_fail(); exit; -} else { - $query = "USE chocomae"; - $result = mysqli_query($db_conn, $query); } +// verify connection is alive (handle wait_timeout from server) +if (!$db_conn->ping()) { + $db_conn->close(); + $db_conn = new mysqli("p:".$db_host, $db_user, $db_passwd, $db_name, $db_port); + if ($db_conn->connect_error) { + set_error_message("DB connection failed."); + send_result_fail(); + exit; + } +} + +$query = "USE chocomae"; +$result = mysqli_query($db_conn, $query); + ?> \ No newline at end of file