text: import contextlib
  
  
  class Transaction:
      def __init__(self, conn):
          self.conn = conn
          self.xid = conn._start_transaction()
  
      def commit(self):
          self.conn._commit_transaction(self.xid)
  
      def rollback(self):
          self.conn._rollback_transaction(self.xid)
  
  
  @contextlib.contextmanager
  def start_transaction(connection):
      tx = Transaction(connection)
  
      try:
          yield tx
      except Exception:
          tx.rollback()
          raise
  
      tx.commit()
  