Fix #611, add conditional for DB session usage.