Feedback Form

Transaction Management: commit(), rollback(), and Savepoints in JDBC

Transaction Management: commit(), rollback(), and Savepoints in JDBC

Transaction Management in JDBC क्या है?

जब हम किसी database में data insert, update या delete करते हैं, तो हर operation को एक transaction कहा जाता है। Transaction का मतलब है — एक logical unit of work जिसमें कई SQL operations शामिल हो सकते हैं। अगर transaction के सभी steps successfully execute हो जाते हैं, तो data database में permanently save हो जाता है। लेकिन अगर बीच में कोई error आ जाए, तो हम उसे rollback करके data को पहले जैसी स्थिति में ला सकते हैं।

JDBC में transaction management बहुत important concept है क्योंकि यह data consistency और integrity बनाए रखता है। मान लीजिए आपके पास bank application है जहाँ money transfer हो रहा है — अगर sender के account से पैसे कट गए लेकिन receiver को credit नहीं हुआ, तो data inconsistent हो जाएगा। ऐसे में JDBC transactions help करते हैं।

Auto-Commit Mode क्या होता है?

By default, JDBC connection auto-commit mode में होता है। इसका मतलब है कि हर SQL statement automatically commit हो जाती है जैसे ही execute होती है। यानी कोई भी operation immediately database में permanently save हो जाता है।

लेकिन जब हम multiple statements को एक single transaction में manage करना चाहते हैं (जैसे money transfer), तो हमें auto-commit mode को off करना पड़ता है।

// Example: Auto-commit off करना Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password"); con.setAutoCommit(false);

अब जब auto-commit off हो जाता है, तो data तभी database में save होगा जब हम manually commit() method call करेंगे।

commit() Method क्या करता है?

commit() method का use transaction को permanently database में save करने के लिए किया जाता है। इसका मतलब है कि transaction के दौरान किए गए सभी changes अब permanent हो जाते हैं और rollback नहीं किए जा सकते।

Example समझिए — आपने दो SQL statements execute कीं: एक में आपने किसी student का marks update किया और दूसरे में उसकी attendance। अगर दोनों statements successful होती हैं, तो आप commit() call करके data को permanently save कर सकते हैं।

try { con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.executeUpdate("UPDATE students SET marks = 90 WHERE id = 1"); stmt.executeUpdate("UPDATE attendance SET present_days = 40 WHERE student_id = 1"); con.commit(); // All changes saved permanently System.out.println("Transaction committed successfully!"); } catch (SQLException e) { con.rollback(); System.out.println("Transaction failed, rolled back!"); }

ऊपर दिए गए example में अगर दोनों queries successfully चल जाती हैं, तो commit() database में changes save कर देता है। अगर बीच में कोई error आती है, तो rollback() call होने से सारे changes cancel हो जाते हैं।

rollback() Method क्या करता है?

rollback() method का use transaction को cancel करने के लिए किया जाता है। इसका मतलब है कि transaction में किए गए सभी temporary changes revert हो जाते हैं और database अपनी previous state में चला जाता है।

rollback() तब बहुत useful होता है जब किसी transaction के बीच में error आ जाए — जैसे second SQL statement fail हो जाए। इस case में हम rollback() करके data को पहले जैसा रख सकते हैं।

try { con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.executeUpdate("INSERT INTO accounts VALUES (1, 'Amit', 5000)"); stmt.executeUpdate("UPDATE accounts SET balance = balance - 2000 WHERE id = 2"); con.commit(); System.out.println("Transaction completed!"); } catch (SQLException e) { con.rollback(); System.out.println("Error occurred! Transaction rolled back."); }

ऊपर के code में अगर second query fail हो जाती है, तो rollback() पूरे transaction को cancel कर देगा, जिससे database safe और consistent रहेगा।

Savepoint क्या होता है?

कभी-कभी हमें transaction के अंदर किसी specific point पर वापस जाना होता है, लेकिन पूरे transaction को rollback नहीं करना होता। ऐसे में हम Savepoint use करते हैं।

Savepoint एक ऐसा marker होता है जहाँ तक हम rollback कर सकते हैं बिना पूरे transaction को cancel किए। यानी partial rollback करने का तरीका।

try { con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.executeUpdate("INSERT INTO orders VALUES (101, 'Laptop', 50000)"); Savepoint sp1 = con.setSavepoint("FirstSavepoint"); stmt.executeUpdate("INSERT INTO orders VALUES (102, 'Mouse', 1000)"); Savepoint sp2 = con.setSavepoint("SecondSavepoint"); stmt.executeUpdate("INSERT INTO orders VALUES (103, 'Keyboard', 2000)"); con.rollback(sp2); // Rollback to Second Savepoint con.commit(); System.out.println("Rolled back to savepoint and committed remaining."); } catch (SQLException e) { e.printStackTrace(); }

ऊपर दिए गए example में, अगर हम rollback(sp2) करते हैं, तो सिर्फ “Keyboard” वाली entry cancel होगी, बाकी की entries save रहेंगी।

Transaction Management के Types

JDBC में transaction को manage करने के दो तरीके होते हैं:

  • Programmatic Transaction Management: Developer manually control करता है कि कब commit या rollback करना है।
  • Declarative Transaction Management: Framework (जैसे Spring) automatically handle करता है transactions को annotations या configurations के through।

Programmatic Transaction Example

try { con.setAutoCommit(false); PreparedStatement ps1 = con.prepareStatement("UPDATE employees SET salary = salary + 5000 WHERE id = ?"); ps1.setInt(1, 101); ps1.executeUpdate(); PreparedStatement ps2 = con.prepareStatement("UPDATE payroll SET total = total + 5000 WHERE emp_id = ?"); ps2.setInt(1, 101); ps2.executeUpdate(); con.commit(); System.out.println("Salary updated successfully!"); } catch (SQLException e) { con.rollback(); System.out.println("Error! Changes reverted."); }

यह approach हर small-level JDBC project में सबसे common है क्योंकि इसमें आपको full control मिलता है कि transaction कब commit या rollback करना है।

Transaction Management के Advantages

  • Data Consistency: सभी related operations या तो पूरी तरह successful होंगे या बिल्कुल नहीं।
  • Data Integrity: गलत data कभी database में नहीं जाएगा।
  • Error Handling: किसी भी failure के बाद rollback से data सुरक्षित रहता है।
  • Atomicity: Transaction एक single unit की तरह काम करता है।
  • Reliability: Application अधिक भरोसेमंद बनती है।

ACID Properties in JDBC Transaction

Transaction management को समझने के लिए ACID properties बहुत important हैं। ACID का मतलब है:

Property Meaning
Atomicity Transaction या तो पूरी तरह execute होगा या बिल्कुल नहीं।
Consistency Transaction के बाद database valid state में रहेगा।
Isolation Parallel transactions एक-दूसरे को affect नहीं करेंगी।
Durability Once committed, data permanently database में save रहेगा।

Transaction Management के Best Practices

  • Always auto-commit mode को false करें जब multiple statements हों।
  • हर transaction को try-catch block में रखें।
  • Exception आने पर तुरंत rollback करें।
  • Transaction boundaries को clearly define करें।
  • जहाँ जरूरी हो वहाँ Savepoints का use करें।
  • Resource leak से बचने के लिए finally block में connection close करें।

Real-World Example: Banking System

मान लीजिए, आपको एक banking application में money transfer करना है — जहाँ एक account से पैसा debit होता है और दूसरे में credit।

try { con.setAutoCommit(false); PreparedStatement debit = con.prepareStatement("UPDATE account SET balance = balance - ? WHERE acc_no = ?"); debit.setDouble(1, 5000); debit.setInt(2, 101); debit.executeUpdate(); PreparedStatement credit = con.prepareStatement("UPDATE account SET balance = balance + ? WHERE acc_no = ?"); credit.setDouble(1, 5000); credit.setInt(2, 102); credit.executeUpdate(); con.commit(); System.out.println("Transaction Successful!"); } catch (SQLException e) { con.rollback(); System.out.println("Transaction Failed! Rolled back to previous state."); }

अगर बीच में कोई query fail होती है (जैसे receiver का account नहीं मिला), तो rollback() दोनों updates को cancel कर देगा। इससे money loss नहीं होगा और data consistent रहेगा।

Important Points to Remember

  • commit() से पहले auto-commit disable होना चाहिए।
  • rollback() और savepoint() same connection object पर काम करते हैं।
  • Savepoint को release करने के लिए con.releaseSavepoint(sp) use करें।
  • Always close connection, statement, और resultset objects properly।

Exam-Oriented Notes

  • Transaction = group of operations जो एक logical unit बनाते हैं।
  • commit() → changes permanently save करता है।
  • rollback() → transaction cancel करके previous state restore करता है।
  • Savepoint → transaction के अंदर specific point जहाँ तक rollback किया जा सकता है।
  • Auto-commit mode default में true होता है।
  • ACID properties ensure करती हैं कि data consistent और reliable रहे।
  • Transaction management real-life systems (banking, ecommerce) में बहुत जरूरी है।
  • Programmatic transaction control JDBC में सबसे ज्यादा use होता है।