Added more thread protection

Added thread protection to Database class.