use select_for_update to avoid deadlock or foreign key error around replace
cleanup
fix SQL
remove begin_work,commit,rollback, we are already in transaction
Merge pull request #22 from kazeburo/select_for_update

Select for update