aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2012-07-30 01:54:07 +0200
committerKim Alvefur <zash@zash.se>2012-07-30 01:54:07 +0200
commit3496e5281e28c5b17c2074de2b83225213edceb5 (patch)
treec0d855232c76a6ceb25f607c3fa3d4c5705fadfd
parentdfd02c4577869306c3e9e55685b6aa1bcfb45d69 (diff)
downloadprosody-3496e5281e28c5b17c2074de2b83225213edceb5.tar.gz
prosody-3496e5281e28c5b17c2074de2b83225213edceb5.zip
mod_storage_sql: Keep connections in a shared cache table
-rw-r--r--plugins/mod_storage_sql.lua22
1 files changed, 22 insertions, 0 deletions
diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua
index 6d19eee6..c5a711bc 100644
--- a/plugins/mod_storage_sql.lua
+++ b/plugins/mod_storage_sql.lua
@@ -27,12 +27,28 @@ local next = next;
local setmetatable = setmetatable;
local xpcall = xpcall;
local json = require "util.json";
+local build_url = require"socket.url".build;
local DBI;
local connection;
local host,user,store = module.host;
local params = module:get_option("sql");
+local dburi;
+local connections = module:shared "/*/sql/connection-cache";
+
+local function db2uri(params)
+ return build_url{
+ scheme = params.driver,
+ user = params.username,
+ password = params.password,
+ host = params.host,
+ port = params.port,
+ path = params.database,
+ };
+end
+
+
local resolve_relative_path = require "core.configmanager".resolve_relative_path;
local function test_connection()
@@ -42,6 +58,7 @@ local function test_connection()
else
module:log("debug", "Database connection closed");
connection = nil;
+ connections[dburi] = nil;
end
end
local function connect()
@@ -60,6 +77,8 @@ local function connect()
module:log("debug", "Successfully connected to database");
dbh:autocommit(false); -- don't commit automatically
connection = dbh;
+
+ connections[dburi] = dbh;
return connection;
end
end
@@ -146,6 +165,9 @@ do -- process options to get a db connection
end
assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
+
+ dburi = db2uri(params);
+ connection = connections[dburi];
assert(connect());