Reconnect to database on errors (fix #675)