Database Connection Fixes

This fixes critical issues related to database locking when using MySQL database.