aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_storage_sql.lua
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
commit9be3d774a9bd516abed3b081202e58f761570f2d (patch)
treec0d855232c76a6ceb25f607c3fa3d4c5705fadfd /plugins/mod_storage_sql.lua
parenta515ffa5e3ebd1503d1a609186c87fafebde5d37 (diff)
downloadprosody-9be3d774a9bd516abed3b081202e58f761570f2d.tar.gz
prosody-9be3d774a9bd516abed3b081202e58f761570f2d.zip
mod_storage_sql: Keep connections in a shared cache table
Diffstat (limited to 'plugins/mod_storage_sql.lua')
-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());