Refactor api to explicitly close mysql connections

@WayneKoorts @markstuart 
On @andrewm-mediasuite's advice, I have refactored a bit to explicitly open use and then close mysql connections. This is because due to nodes long running process nature and mysql's want to close inactive connections after a while (which crashes the node process) the app was crashing on staging every couple mins.
Please give it a once over.