What is the result of the employees table has no records before the code executed?

Given the code fragment:

try {

conn.setAutoCommit(false);

stmt.executeUpdate(“insert into employees values(1,’Sam’)”);

Savepoint save1 = conn.setSavepoint(“point1”);

stmt.executeUpdate(“insert into employees values(2,’Jane’)”);

conn.rollback();

stmt.executeUpdate(“insert into employees values(3,’John’)”);

conn.setAutoCommit(true);

stmt.executeUpdate(“insert into employees values(4,’Jack’)”);

ResultSet rs = stmt.executeQuery(“select * from employees”);

while (rs.next()) {

System.out.println(rs.getString(1) + ” ” + rs.getString(2));

}

} catch(Exception e) {

System.out.print(e.getMessage());

}

What is the result of the employees table has no records before the code executed?

Given the code fragment:

try {

conn.setAutoCommit(false);

stmt.executeUpdate(“insert into employees values(1,’Sam’)”);

Savepoint save1 = conn.setSavepoint(“point1”);

stmt.executeUpdate(“insert into employees values(2,’Jane’)”);

conn.rollback();

stmt.executeUpdate(“insert into employees values(3,’John’)”);

conn.setAutoCommit(true);

stmt.executeUpdate(“insert into employees values(4,’Jack’)”);

ResultSet rs = stmt.executeQuery(“select * from employees”);

while (rs.next()) {

System.out.println(rs.getString(1) + ” ” + rs.getString(2));

}

} catch(Exception e) {

System.out.print(e.getMessage());

}

What is the result of the employees table has no records before the code executed?

A.
1 Sam

B.
4 Jack

C.
3 John
4 Jack

D.
1 Sam
3 John
4 Jack

Explanation:
Autocommit is set to false. The two following statements will be within the same transaction.
stmt.executeUpdate(“insert into employees values(1,’Sam’)”); stmt.executeUpdate(“insert into employees values(2,’Jane’)”); These two statements are rolled back through (the savepoint is ignored the savepoint must be specified in the rollback if you want to rollback to the savepoint):
conn.rollback();
The next two insert statements are executed fine. Their result will be in the output.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


Charlie

Charlie

C is correct. setAutoCommit(true) auto-commits ‘3, John’.
If setAutoCommit() is called during a transaction and the auto-commit mode is changed, the transaction is committed. If setAutoCommit is called and the auto-commit mode is not changed, the call is a no-op.