While writing java JDBC code to call a stored procedure I am using con.setAutoCommit(false);
My question is what is the difference in the below approaches:
Approach-1:
con = DBConnection.getConnection();
con.setAutoCommit(false);
stmt= con.prepareCall("{call updateEmp(?,?,?,?,?,?)}");
stmt.setInt(1, id);
stmt.setString(2, name);
stmt.setString(3, role);
stmt.registerOutParameter(6, java.sql.Types.VARCHAR);
stmt.executeUpdate();
con.commit();
//read the OUT parameter AFTER commit
String result = stmt.getString(6);
or Approach-2:
// Read the OUT parameter BEFORE commit
String result = stmt.getString(6);
con.commit();
It seems to me that in cases when out parameter is a simple type, it's to greater extent a matter of style. However, out parameter can be, for instance, a cursor. In case of autocommit, a commit operation occurs only when all of the result sets that are returned by cursor type output parameters or by the stored procedure are closed. If commit issued before cursor completely fetched , data consistency is questionable. To avoid such ambiguity , I'd suggest committing/rolling back transaction after all out parameters are read.