I see two things, neither of which are probably causing your performance problems.
1. prepare_cached is unnecessary here, as it only gets called two times with two explicitly different statements
2. Why set AutoCommit to 1 and use begin_work/commit everywhere? Why not turn it off and simply commit when you want?
Again, given the time that I have been able to devote to looking it over, it looks good to me, and I cannot explain it away.
--------------
It's sad that a family can be torn apart by such a such a simple thing as a pack of wild dogs