Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to design a daemon with a MySQL DB connection
    text
    copied!<p>Say you were writing a daemon that services a job queue. Various other software writes jobs for the daemon into the queue. The daemon polls the queue every few seconds for pending jobs. Assume the queue is implemented as a table in a MySQL database and that the daemon is a simple loop:</p> <ol> <li>get all due jobs from the queue</li> <li>do the jobs</li> <li>sleep for N seconds</li> <li>goto 1</li> </ol> <p>The daemon must survive interrupted service from the MySQL DB server and disruption to DB connections.</p> <p>Would you design the daemon to connect to the DB server once per cycle? i.e. connect before 1. and disconnect between 2 an 3?</p> <p>Or would you have daemon keep a connection open? In which case it needs also to a) detect when the server or connection is not working, b) disconnect and reconnect, and c) do so without accumulating DB connections, dud connection descriptors or other dead resources.</p> <p>If you have a preference, why?</p> <p>Pros and cons? </p> <p>Factors that enter into the design?</p> <p>Any other approaches?</p> <p>The answer here: <a href="https://stackoverflow.com/questions/4063230">mysql connection from daemon written in php</a> doesn't say why it's better to keep the connection open. I read elsewhere that the per-connection overhead in MySQL is very light. So it isn't obvious why permanently consuming one server connection is better than connecting/disconnecting every few seconds.</p> <p>In my case the daemon is written in PHP.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload