# HG changeset patch # User Kim Alvefur # Date 1456062799 -3600 # Node ID fb37aece32528c484b40420b76a6010718ef3ccd # Parent d00d8cfcc9a8061b59ffd6e6b3446609f2236351 mod_storage_sql: Share SQL connections with same parameters across VirtualHosts (fixes #576) diff -r d00d8cfcc9a8 -r fb37aece3252 plugins/mod_storage_sql.lua --- a/plugins/mod_storage_sql.lua Fri Feb 19 19:22:18 2016 +0100 +++ b/plugins/mod_storage_sql.lua Sun Feb 21 14:53:19 2016 +0100 @@ -449,19 +449,25 @@ function module.load() if prosody.prosodyctl then return; end + local engines = module:shared("/*/sql/connections"); local params = normalize_params(module:get_option("sql", default_params)); - engine = sql:create_engine(params, function (engine) - if module:get_option("sql_manage_tables", true) then - -- Automatically create table, ignore failure (table probably already exists) - -- FIXME: we should check in information_schema, etc. - create_table(); - -- Check whether the table needs upgrading - if upgrade_table(params, false) then - module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name); - return false, "database upgrade needed"; + engine = engines[sql.db2uri(params)]; + if not engine then + module:log("info", "Creating new engine"); + engine = sql:create_engine(params, function (engine) + if module:get_option("sql_manage_tables", true) then + -- Automatically create table, ignore failure (table probably already exists) + -- FIXME: we should check in information_schema, etc. + create_table(); + -- Check whether the table needs upgrading + if upgrade_table(params, false) then + module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name); + return false, "database upgrade needed"; + end end - end - end); + end); + engines[sql.db2uri(params)] = engine; + end module:provides("storage", driver); end