By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: SQL Transactions – How to Implement COMMIT, ROLLBACK, and Savepoints With Python | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > SQL Transactions – How to Implement COMMIT, ROLLBACK, and Savepoints With Python | HackerNoon
Computing

SQL Transactions – How to Implement COMMIT, ROLLBACK, and Savepoints With Python | HackerNoon

News Room
Last updated: 2025/05/02 at 12:10 PM
News Room Published 2 May 2025
Share
SHARE

In modern database development, tools like DataGrip make transaction handling easier by providing graphical interfaces to commit, rollback, or set savepoints.

In this article, we’ll define COMMIT, ROLLBACK, and SAVEPOINT in SQL and demonstrate how to implement these transaction controls when working with Oracle, MySQL, or PostgreSQL in Python.

Key Transaction Concepts

1. COMMIT

• Definition: Finalizes a transaction, making all changes permanent in the database.

• Use Case: Use COMMIT when all operations in a transaction are successful, ensuring the database reflects the changes.

2. ROLLBACK

• Definition: Reverts all changes made during a transaction, restoring the database to its previous state.

• Use Case: Use ROLLBACK to handle errors or undo a failed transaction.

3. SAVEPOINT

• Definition: Sets a named checkpoint within a transaction, allowing partial rollbacks to that point without undoing the entire transaction.

• Use Case: Use SAVEPOINT to manage complex transactions with multiple steps, rolling back selectively when needed.

Python and Database Transactions

When working with databases programmatically, Python’s database libraries (e.g., cx_Oracle, mysql-connector-python, psycopg2) provide methods to control transactions explicitly.

Common Steps for Transactions in Python

  1. Start a Transaction: Automatically starts with the connection unless autocommit is enabled.
  2. Execute SQL Statements: Perform the necessary database operations.
  3. Commit or Rollback: Use commit() to finalize changes or rollback() to revert them.
  4. Use Savepoints: For finer control, define and rollback to savepoints if supported.

Example: Python Transactions With Oracle Databases

Setup Connection

import cx_Oracle

# Connect to Oracle Database
connection = cx_Oracle.connect("user/password@localhost/XEPDB1")
cursor = connection.cursor()

Using COMMIT and ROLLBACK

try:
    # Start Transaction
    cursor.execute("UPDATE Accounts SET Balance = Balance - 100 WHERE Name = 'Alice'")
    cursor.execute("UPDATE Accounts SET Balance = Balance + 100 WHERE Name = 'Bob'")
    
    # Commit the transaction
    connection.commit()
    print("Transaction committed successfully!")
except Exception as e:
    # Rollback in case of error
    connection.rollback()
    print(f"Transaction failed. Rolled back changes. Error: {e}")

Using SAVEPOINT

try:
    # Start Transaction
    cursor.execute("UPDATE Accounts SET Balance = Balance - 200 WHERE Name = 'Alice'")
    connection.commit()
    
    # Savepoint
    cursor.execute("SAVEPOINT Savepoint_After_Alice")

    # Add 200 to Bob (intentional error to demonstrate rollback)
    cursor.execute("UPDATE Accounts SET Balance = Balance + 200 WHERE Name = 'Unknown'")

    # Commit if successful
    connection.commit()
except Exception as e:
    # Rollback to savepoint
    cursor.execute("ROLLBACK TO Savepoint_After_Alice")
    connection.commit()
    print(f"Rolled back to savepoint. Error: {e}")

Summary

Mastering these concepts allows you to manage database transactions efficiently in real-world applications!


Thank you for taking the time to explore data-related insights with me. I appreciate your engagement. If you find this information helpful, I invite you to follow me or connect with me on LinkedIn. Happy exploring!👋

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How a U.S. Tax Loophole Supercharged China’s Exports
Next Article TikTok fined €530m by Irish regulator for failing to guarantee China would not access user data
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

The Kill Switch: A Coder’s Act of Revenge | HackerNoon
Computing
Screen Recordings Made Easy, Thanks to This On-Sale AI Tool
News
20% of NIO’s battery swap stations approach breakeven point · TechNode
Computing
Get the record-low price on the Nebula Capsule 3 projector before Prime Day
News

You Might also Like

Computing

The Kill Switch: A Coder’s Act of Revenge | HackerNoon

7 Min Read
Computing

20% of NIO’s battery swap stations approach breakeven point · TechNode

2 Min Read
Computing

Authentication Sucks—So This Developer Built a Better Starting Point | HackerNoon

4 Min Read
Computing

Xiaomi smartphone to debut Qualcomm’s Snapdragon 7s Gen 3 next month · TechNode

1 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?