transaction is not working in mysql database

Your first code does not raise an exception and so the transaction is not rolled back. Note the exclamation mark after create!