I was setting con.setAutoCommit(false);
as soon as I create connection so that nothing goes in DB uncommitted. But it turns out if you close the connection all transaction will be committed, no matter what your setAutoCommit() status is.
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.7.5:xxxx:xxx", "xxx", "xxx");
con.setAutoCommit(false);
st = con.createStatement();
String sql = "INSERT INTO emp (eid, name, dob, address) VALUES (?, ?, ?, ?)";
PreparedStatement statement = con.prepareStatement(sql);
statement.setInt(1, 8 );
statement.setString(2, "kkk" );
statement.setDate(3, date );
statement.setString(4, "pppp" );
pst = con.prepareStatement(sql);
int rowsInserted = statement.executeUpdate();
//con.commit ();
System.out.println ("rowsInserted "+rowsInserted);
con.close ();
Even after commenting con.commit ();
row is being inserted when connection closes so I was wondering what is the use of con.commit ();
?
Another answer says it's vendor specific:
If a Connection is closed without an explicit commit or a rollback; JDBC does not mandate anything in particular here and hence the behaviour is dependent on the database vendor. In case of Oracle, an implict commit is issued.
It does not make sense.Thanks.