servlets struts hibernate spring maven java
 

JDBC - Transactions


If your JDBC Connection is in auto-commit mode, which it is as a matter of course, at that point each SQL explanation is focused on the database upon its completion.

That might be fine for straightforward applications, yet there are three reasons why you might need to kill the auto-submit and deal with your very own exchanges −

  • To increment performance.

  • To keep up the uprightness of business processes.

  • To utilize appropriated transactions.

Transactions empower you to control if, and when, changes are connected to the database. It treats a solitary SQL articulation or a gathering of SQL proclamations as one intelligent unit, and if any announcement comes up short, the entire exchange fails.

To empower manual-exchange support rather than the auto-commit mode that the JDBC driver utilizes as a matter of course, utilize the Connection item's setAutoCommit() technique. On the off chance that you pass a boolean false to setAutoCommit( ), you turn off auto-submit. You can pass a boolean consistent with walk out on again.

For model, on the off chance that you have a Connection object named conn, code the accompanying to kill auto-submit −

conn.setAutoCommit(false);

Commit & Rollback

Once you are finished with your progressions and you need to submit the progressions at that point call commit() technique on association object as pursues −

conn.commit( );

Otherwise, to move back updates to the database made utilizing the Connection named conn, utilize the accompanying code −

conn.rollback( );

The following precedent delineates the utilization of a submit and rollback object −

try{ 

//Assume a substantial association object conn 

conn.setAutoCommit(false); 

Statement stmt = conn.createStatement(); 

String SQL = "INSERT INTO Employees " + 

"VALUES (106, 20, 'Rita', 'Tez')"; 

stmt.executeUpdate(SQL); 

//Submit a deformed SQL explanation that breaks 

String SQL = "INSERTED IN Employees " + 

"VALUES (107, 22, 'Sita', 'Singh')"; 

stmt.executeUpdate(SQL); 

//If there is no error. 

conn.commit(); 

}catch(SQLException se){ 

//If there is any error. 

conn.rollback(); 

}

Using Savepoints

The new JDBC 3.0 Savepoint interface gives you the extra value-based control. Most present day DBMS, support savepoints inside their surroundings, for example, Oracle's PL/SQL.

When you set a savepoint you characterize a coherent rollback point inside an exchange. On the off chance that a mistake happens past a savepoint, you can utilize the rollback technique to fix either every one of the progressions or just the progressions made after the savepoint.

The Connection object has two new strategies that assistance you oversee savepoints −

  • setSavepoint(String savepointName): Defines another savepoint. It additionally restores a Savepoint object.

  • releaseSavepoint(Savepoint savepointName): Deletes a savepoint. Notice that it requires a Savepoint object as a parameter. This item is generally a savepoint produced by the setSavepoint() method.

There is one rollback (String savepointName) technique, which moves back work to the predefined savepoint.

The following model shows the utilization of a Savepoint object −

try{ 

//Assume a substantial association object conn 

conn.setAutoCommit(false); 

Statement stmt = conn.createStatement(); 

//set a Savepoint 

Savepoint savepoint1 = conn.setSavepoint("Savepoint1"); 

String SQL = "INSERT INTO Employees " + 

"VALUES (106, 20, 'Rita', 'Tez')"; 

stmt.executeUpdate(SQL); 

//Submit a distorted SQL articulation that breaks 

String SQL = "INSERTED IN Employees " + 

"VALUES (107, 22, 'Sita', 'Tez')"; 

stmt.executeUpdate(SQL); 

//If there is no blunder, submit the changes. 

conn.commit(); 

}catch(SQLException se){ 

//If there is any error. 

conn.rollback(savepoint1); 

}





© Javacodegeeks © 2019 - 2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3