Database: implemented lazy database connection [Closes #1029]