I've never seen the database auto-disconnect, you maintain the connection so you decide when it disconnects. Again you can't make a connection before the query, because the connection is threaded. You'll have to store the queries somewhere, connect, and then fire them afterwards (hoping they're not invalid anymore).