A Xavante or FastCGI instance running an Orbit application can be very long-lived. MySQL has an annoying “feature” that makes it close a connection with a client in case the connection has been idle for a long time, and libmysqlclient
has an even more annoying bug where it won’t reopen a dropped connection automatically. Even worse, the error code for a closed connection varies depending on the specific implementation of the server. This used to be a problem for long-lived Orbit applications that kept connections open so they wouldn’t have to be reopened on every request.
Orbit 2.1.0 has a solution to this problem in the form of connections that have timeouts. To create a timed connection you use the orbit.recycle
function, passing a connection maker and a timeout in seconds. The function returns a connection proxy that automatically reopens the connection when it becomes stale. For example, if you want a connection that reopens every hour you can do the following, where todo
is your Orbit application:
local env = luasql.sqlite3() | |
local db = todo.real_path .. "/todo.db" | |
local timeout = 3600 -- one hour | |
todo.mapper.conn = recycle(function () | |
return env:connect(db) | |
end, timeout) |