Conditional thread safe database execute