Added transaction around access of DB. Since beginTx waits until db is ready to use this fixes some NPE's that would otherwise occur if db is accessed in PENDING mode.