0% found this document useful (0 votes)
5 views

Unit-5 SQL 4.1 TRANSACTIONS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Unit-5 SQL 4.1 TRANSACTIONS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CHAPTER-5

INTERFACE PYTHON WITH SQL


MYSQL TRANSACTIONS
MYSQL TRANSACTIONS
MYSQL TRANSACTIONS
NOTE: The behaviour of the transactions is controlled by using Autocommit session variable.
By default, whenever you execute a SQL statement, MySQL immediately writes the result of the
statement to the database. Another way to think about this is that the MySQL executes each
statement as a separate transaction which implicitly starts when you execute the statement and
ends as soon as the statement is done executing. This behaviour is called the *autocommit* mode.

To view the current autocommit setting, type the following in the MySQL shell:

As you can see, the @@autocommit system variable is set to 1 which means that the
autocommit mode is enabled.
If you disable the autocommit mode then the SQL statements that follow will be part of a
transaction, and you will have to commit them manually using the COMMIT statement before the
MySQL session ends, otherwise, the changes made by the statements will be rolled back.
MYSQL TRANSACTIONS
CLOSING A CONNECTION

Since the database can only keep a limited number of


connections open at a time, we must close the connection.
cursor.close() OR connection.close()
PRACTICAL IMPLEMENTATION:18
MYSQL TRANSACTIONS

Write all the queries in


set auto-commit try onlyi.e., we need to commit the transaction only when both the
to false,
transactions complete successfully.

After successful execution of both the queries, we committed our


changes to the database
Refer next page fo
about format() in p
In case of an exception or failure of one of the queries, we can revert our changes using
a connection.rollback()

Closing the connection of


database

OUTPUT
We placed all our code in the “try-except” block to
:
catch the database exceptions that may occur
during the process.
PRACTICAL IMPLEMENTATION:18
PRACTICAL IMPLEMENTATION:18

You might also like