Answer the question
In order to leave comments, you need to log in
Why does rollback() exist?
Please explain why there is a transaction rollback in the form of rollback()? It is desirable in the form of an example at least in words.
For example, there is the following transaction:
import psycopg2
class DB_connection:
def __enter__(self):
self.connection = psycopg2.connect(
dbname='vendor',
user='vendor',
password='vendor',
host='localhost'
)
return self.connection
def __exit__(self, exc_type, exc_val, exc_tb):
if hasattr(self, 'connection'):
self.connection.close()
with DB_connection() as db_connect:
db_cursor = db_connect.cursor()
req = "insert into vendors(vendor_name) values('nvidia')"
db_cursor.execute(req)
req = "insert into parts(part_name) values(NULL)" # throw exception
db_cursor.execute(req)
db_connect.commit()
Answer the question
In order to leave comments, you need to log in
In this case, both operations will be rolled back.This rollback is what it is. Read the documentation for the libraries you use!
Rollback is always needed because, in fact, no one can guarantee that the operation will be completed. The fact that you did all this and are sure that it will work is a so-so argument
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question