Mercurial > prosody-hg
comparison plugins/mod_storage_sql.lua @ 5054:97385c45e670
mod_storage_sql: Keep connections in a shared cache table
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 30 Jul 2012 01:54:07 +0200 |
| parents | 685403a6fee1 |
| children | d466d2088a61 |
comparison
equal
deleted
inserted
replaced
| 5053:375ab71e4b37 | 5054:97385c45e670 |
|---|---|
| 25 local pairs = pairs; | 25 local pairs = pairs; |
| 26 local next = next; | 26 local next = next; |
| 27 local setmetatable = setmetatable; | 27 local setmetatable = setmetatable; |
| 28 local xpcall = xpcall; | 28 local xpcall = xpcall; |
| 29 local json = require "util.json"; | 29 local json = require "util.json"; |
| 30 local build_url = require"socket.url".build; | |
| 30 | 31 |
| 31 local DBI; | 32 local DBI; |
| 32 local connection; | 33 local connection; |
| 33 local host,user,store = module.host; | 34 local host,user,store = module.host; |
| 34 local params = module:get_option("sql"); | 35 local params = module:get_option("sql"); |
| 36 | |
| 37 local dburi; | |
| 38 local connections = module:shared "/*/sql/connection-cache"; | |
| 39 | |
| 40 local function db2uri(params) | |
| 41 return build_url{ | |
| 42 scheme = params.driver, | |
| 43 user = params.username, | |
| 44 password = params.password, | |
| 45 host = params.host, | |
| 46 port = params.port, | |
| 47 path = params.database, | |
| 48 }; | |
| 49 end | |
| 50 | |
| 35 | 51 |
| 36 local resolve_relative_path = require "core.configmanager".resolve_relative_path; | 52 local resolve_relative_path = require "core.configmanager".resolve_relative_path; |
| 37 | 53 |
| 38 local function test_connection() | 54 local function test_connection() |
| 39 if not connection then return nil; end | 55 if not connection then return nil; end |
| 40 if connection:ping() then | 56 if connection:ping() then |
| 41 return true; | 57 return true; |
| 42 else | 58 else |
| 43 module:log("debug", "Database connection closed"); | 59 module:log("debug", "Database connection closed"); |
| 44 connection = nil; | 60 connection = nil; |
| 61 connections[dburi] = nil; | |
| 45 end | 62 end |
| 46 end | 63 end |
| 47 local function connect() | 64 local function connect() |
| 48 if not test_connection() then | 65 if not test_connection() then |
| 49 prosody.unlock_globals(); | 66 prosody.unlock_globals(); |
| 58 return nil, err; | 75 return nil, err; |
| 59 end | 76 end |
| 60 module:log("debug", "Successfully connected to database"); | 77 module:log("debug", "Successfully connected to database"); |
| 61 dbh:autocommit(false); -- don't commit automatically | 78 dbh:autocommit(false); -- don't commit automatically |
| 62 connection = dbh; | 79 connection = dbh; |
| 80 | |
| 81 connections[dburi] = dbh; | |
| 63 return connection; | 82 return connection; |
| 64 end | 83 end |
| 65 end | 84 end |
| 66 | 85 |
| 67 local function create_table() | 86 local function create_table() |
| 144 if params.driver == "SQLite3" then | 163 if params.driver == "SQLite3" then |
| 145 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); | 164 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); |
| 146 end | 165 end |
| 147 | 166 |
| 148 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); | 167 assert(params.driver and params.database, "Both the SQL driver and the database need to be specified"); |
| 168 | |
| 169 dburi = db2uri(params); | |
| 170 connection = connections[dburi]; | |
| 149 | 171 |
| 150 assert(connect()); | 172 assert(connect()); |
| 151 | 173 |
| 152 -- Automatically create table, ignore failure (table probably already exists) | 174 -- Automatically create table, ignore failure (table probably already exists) |
| 153 create_table(); | 175 create_table(); |
