Every time you BEGIN a transaction you need to follow it with either a COMMIT or a ROLLBACK, no exceptions.
You should not have a second BEGIN before the previous one was committed or rolled back, it won't do anything. A COMMIT or ROLLBACK ends the transaction, they don't nest.
Make sure your code doesn't leave transactions open when it finishes, begin a second without committing the first, or tries to commit/rollback more than once.