From e9be0888eb61b4ac524cbd6637e7561977d0eb09 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Thu, 25 Sep 2014 14:09:49 +0200 Subject: [PATCH] MT#9067 NGCPConfig: check connection before --- ngcp/ngcp.lua | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index c74c561..5609697 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -79,9 +79,29 @@ NGCPConfig_MT = { __index = NGCPConfig } return t end + local function check_connection(c) + local cur = c:execute("SELECT 1") + local row = cur:fetch() + local result = false + if cur:numrows() == 1 then + result = true + end + cur:close() + return result + end + function NGCPConfig:getDBConnection() - if not self.con then + if not self.env then self.env = assert (luasql.mysql()) + end + if self.con then + local ok,err = pcall(check_connection, self.con) + if not ok then + self.con = nil + sr.log("warn", "connection check error") + end + end + if not self.con then sr.log("dbg","connecting to mysql") self.con = self.env:connect( self.db_database, self.db_username, self.db_pass, self.db_host, self.db_port)