Mercurial > prosody-modules
comparison mod_mam_sql/mod_mam_sql.lua @ 1207:c6b8ae5a8369
mod_mam_sql: Reconnect if the connection should be dropped
author | Rob Hoelz <rob@hoelz.ro> |
---|---|
date | Mon, 02 Sep 2013 20:10:10 +0200 |
parents | 04bf76c3e4c6 |
children | 7dbde05b48a9 |
comparison
equal
deleted
inserted
replaced
1206:04bf76c3e4c6 | 1207:c6b8ae5a8369 |
---|---|
47 port = params.port, | 47 port = params.port, |
48 path = params.database, | 48 path = params.database, |
49 }; | 49 }; |
50 end | 50 end |
51 | 51 |
52 local connect | |
53 | |
52 local function test_connection() | 54 local function test_connection() |
53 if not connection then return nil; end | 55 if not connection then return nil; end |
54 if connection:ping() then | 56 if connection:ping() then |
55 return true; | 57 return true; |
56 else | 58 else |
57 module:log("debug", "Database connection closed"); | 59 module:log("debug", "Database connection closed"); |
60 module:log("debug", "Attempting to reconnect"); | |
58 connection = nil; | 61 connection = nil; |
59 connections[dburi] = nil; | 62 return connect(); |
60 end | 63 end |
61 end | 64 end |
62 local function connect() | 65 function connect() |
63 if not test_connection() then | 66 if not test_connection() then |
64 prosody.unlock_globals(); | 67 prosody.unlock_globals(); |
65 local dbh, err = DBI.Connect( | 68 local dbh, err = DBI.Connect( |
66 params.driver, params.database, | 69 params.driver, params.database, |
67 params.username, params.password, | 70 params.username, params.password, |
113 function getsql(sql, ...) | 116 function getsql(sql, ...) |
114 if params.driver == "PostgreSQL" then | 117 if params.driver == "PostgreSQL" then |
115 sql = sql:gsub("`", "\""); | 118 sql = sql:gsub("`", "\""); |
116 end | 119 end |
117 if not connection then | 120 if not connection then |
121 return nil, 'connection failed'; | |
122 end | |
123 if not test_connection() then | |
118 return nil, 'connection failed'; | 124 return nil, 'connection failed'; |
119 end | 125 end |
120 -- do prepared statement stuff | 126 -- do prepared statement stuff |
121 local stmt, err = connection:prepare(sql); | 127 local stmt, err = connection:prepare(sql); |
122 if not stmt and not test_connection() then | 128 if not stmt and not test_connection() then |