Software / code / prosody
Comparison
util/sql.lua @ 8290:1ebe590c8849
Merge 0.10->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 04 Oct 2017 12:10:55 +0200 |
| parent | 8288:e9ac2d93de18 |
| child | 8378:6a098961bc00 |
comparison
equal
deleted
inserted
replaced
| 8285:433b2a41351f | 8290:1ebe590c8849 |
|---|---|
| 215 engine.delete = engine.execute_update; | 215 engine.delete = engine.execute_update; |
| 216 engine.update = engine.execute_update; | 216 engine.update = engine.execute_update; |
| 217 end | 217 end |
| 218 end | 218 end |
| 219 local function handleerr(err) | 219 local function handleerr(err) |
| 220 log("error", "Error in SQL transaction: %s", debug_traceback(err, 3)); | 220 local trace = debug_traceback(err, 3); |
| 221 return err; | 221 log("debug", "Error in SQL transaction: %s", trace); |
| 222 return { err = err, traceback = trace }; | |
| 222 end | 223 end |
| 223 function engine:_transaction(func, ...) | 224 function engine:_transaction(func, ...) |
| 224 if not self.conn then | 225 if not self.conn then |
| 225 local ok, err = self:connect(); | 226 local ok, err = self:connect(); |
| 226 if not ok then return ok, err; end | 227 if not ok then return ok, err; end |
| 236 log("debug", "SQL transaction success [%s]", tostring(func)); | 237 log("debug", "SQL transaction success [%s]", tostring(func)); |
| 237 local ok, err = self.conn:commit(); | 238 local ok, err = self.conn:commit(); |
| 238 if not ok then return ok, err; end -- commit failed | 239 if not ok then return ok, err; end -- commit failed |
| 239 return success, a, b, c; | 240 return success, a, b, c; |
| 240 else | 241 else |
| 241 log("debug", "SQL transaction failure [%s]: %s", tostring(func), a); | 242 log("debug", "SQL transaction failure [%s]: %s", tostring(func), a.err); |
| 242 if self.conn then self.conn:rollback(); end | 243 if self.conn then self.conn:rollback(); end |
| 243 return success, a; | 244 return success, a.err; |
| 244 end | 245 end |
| 245 end | 246 end |
| 246 function engine:transaction(...) | 247 function engine:transaction(...) |
| 247 local ok, ret = self:_transaction(...); | 248 local ok, ret = self:_transaction(...); |
| 248 if not ok then | 249 if not ok then |
| 249 local conn = self.conn; | 250 local conn = self.conn; |
| 250 if not conn or not conn:ping() then | 251 if not conn or not conn:ping() then |
| 252 log("debug", "Database connection was closed. Will reconnect and retry."); | |
| 251 self.conn = nil; | 253 self.conn = nil; |
| 254 log("debug", "Retrying SQL transaction [%s]", tostring((...))); | |
| 252 ok, ret = self:_transaction(...); | 255 ok, ret = self:_transaction(...); |
| 256 log("debug", "SQL transaction retry %s", ok and "succeeded" or "failed"); | |
| 257 else | |
| 258 log("debug", "SQL connection is up, so not retrying"); | |
| 259 end | |
| 260 if not ok then | |
| 261 log("error", "Error in SQL transaction: %s", ret); | |
| 253 end | 262 end |
| 254 end | 263 end |
| 255 return ok, ret; | 264 return ok, ret; |
| 256 end | 265 end |
| 257 function engine:_create_index(index) | 266 function engine:_create_index(index) |