Calling a rollback operation undoes all the effects or modifications that have been done by a Transaction(Ti) and terminate the Ti and all the variables get their previous values stored. Rollback is mainly called when you get one or more than one SQL exception in the statements of Transaction(Ti), then the Ti get aborted and start over from the beginning. This is the only way to know what has been committed and what hasn’t been committed.
A SQL exception just signals out that something is wrong in your statement that you have written but doesn’t mention what and where wrong has been done. So the only option left to you is calling a rollback method.
Procedure: It primarily deals with two steps. First, create a database and then dealing with transactions.
- Create a database
- Execute the transactions for rollback
- Import the database
- Load and register drivers if necessary
- Create a new connection
- Create a statement for commit/rollback
- Execute the query for commit/rollback
- Process the results
- Close the connection else previous processing may lose if any.
Step 1: We can also rollback the modifications in the database up to a particular flag or save point by just passing the needed Save points name as a parameter into this below method −
// Set the Flag or Save point con.rollback("MysavePoint");
Step 2. To roll back a transaction: Load the JDBC driver, by using the API method forName(String className) of the Class. In this example, we are using the Oracle
- Register the required driver using the registerDriver( ) method
// To register the needed Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver());
- Get the connection information using the getConnection() API method of the DriverManager:
// For getting the connection String url = "jdbc:mysql://localhost/mydatabase/icpc"; Connection conn = DriverManager.getConnection(string url, String user, String password);
- Disable the auto-commit using the API method of off connection setAutoCommit(boolean auto-commit) method as:
// Set the auto commit false. This will execute all // SQL statements as individual transactions con.setAutoCommit(false);
- Now, set the save point using the setSavepoint() or, commit the transaction using the API method commit( ) of connection as shown below−
Savepoint savePoint = con.setSavepoint("MysavePoint"); Con.commit();
- If any SQL exception is found then, in that case, invoke rollback( ) API method for the whole transaction to till the previously set savepoint:
con.rollback() Or, con. rollback(my_Savepoint);
Implementation: Java program to demonstrate both rollback() and commit() program is as follows
Output: There are two sample outputs images covering both cases: commit and rollback or simply direct rollback as illustrated in the below outputs.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.