Don't hold database sessions open for long periods during mailsync.

This was causing us to create a ridiculous number of database
connections which sat around idle most of the time because the mailsync
code was waiting on network I/O (or simply sleeping).