OCP 8 – working with autocommit vs savepoints

You might want to know more about JDBC than is covered by Chapter 10 of our book, OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide: Exam 1Z0-809. This blog post teaches you about autocommit and savepoints and assumes you have completed reading chapter 10 of our OCP 8 book and are looking to learn more. (Note: we’ve heard rumors this topic is in fact on the exam. It wasn’t when we took the beta but was on the Java 7 exam. So either the questions are left over or it was added back.)

In the book, you saw how to run one statement at a time and have the database automatically see the changes right away. This is called automatic commit or auto-commit for short. A commit updates the database. JDBC provides options for more control by running your code in transactions. A transaction allows a group of statements to go together.

Rollback/Commit

By default, auto-commit is set to true which means each statement is committed in the database as soon as it is run. You can set it to false so statements and prepared statements do not automatically commit data.

The following example gives that control. If anything goes wrong update either of the rows, the entire transaction is rolled back and the database doesn’t change either row. If both are successful, the database sees both.

conn.setAutoCommit(false);
String sql = "update species name = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
try {
  ps.setString(1, "Elephant");
  ps.setInt(2, 1);
  ps.executeUpdate();
  ps.setString(1, "Monkey");
  ps.setInt(2, 4);
  ps.executeUpdate();
  
  conn.commit();      // send data to database
} catch (SQLException e) {
  conn.rollback();    // don't update database
}

Let’s rewrite this example to focus more on the commit/rollback code:

conn.setAutoCommit(false);
String sql = "update species name = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
try {
  updateRecord(1);
  updateRecord(2);
  conn.commit(); // send data to database 
} catch (SQLException e) {
  conn.rollback(); // don't update database 
} 

Setting a Rollback point

JDBC has a type called a Savepoint which is like putting a bookmark in a book so you can easily get back there.

conn.setAutoCommit(false);
String sql = "update species name = ? where id = ?";
PreparedStatement ps = conn.prepareStatement(sql);
try {
  updateRecord(1);
  Savepoint sp = conn.setSavepoint();
  updateRecord(2);
  updateRecord(3);
  // oops - let's go back like this didn't happen
  conn.rollback(sp);
  // proceed from there
  updateRecord(4);
  conn.commit(); // send data to database 
} catch (SQLException e) { 
  conn.rollback(); // don't update database 
} 

Announcing: Our NEW Java OCA / OCP 8 Practice Test Book!

pt-cover

Jeanne and I are thrilled and excited to announce that we are nearly done writing a brand new book for the OCA / OCP Java 8 Programmer exams!

The new book, OCA / OCP Java SE 8 Programmer Practice Tests, includes over 1000+ hand-crafted and peer reviewed questions. We’ve also created a new permanent Practice Tests OCA / OCP 8 book page on the blog to keep track of updates and news about the new book.

Jeanne and I wanted to say how thankful we are to our all of our readers of our first two books. Without your invaluable feedback and positive reviews, we never would have been offered this opportunity to expand our OCA and OCP subject matter. We promise to make this next book our best yet!

We are expecting to ship the book in March 2017. Stay tuned for additional news about the book!

FIRST robotics 2017 background check and social security number

This year, when you register to volunteer with FIRST Robotics in VIMS, you get prompted to register in Verified Volunteers so they can do a background check. In particular to determine you don’t have a criminal history and aren’t a sex offender.  For the most part, this is straightforward. You give some minimal information about yourself. And FIRST pays the cost of the background check.US FIRST - V2

 

Wait? They want my Social Security Number?

The only thing in the background check that could be considered sensitive is your Social Security Number. FIRST doesn’t get this information if you choose to provide it. But Verified Volunteers does.

I have a “real” background check for my job. So I don’t have a problem with the concept of a background check. I’m not a big fan of providing unnecessary information though. Especially given the number of data breaches lately. Luckily, FIRST says you don’t have to provide it in their volunteer screening guide:

first-ssn

 

Wait, does this work?

Yes. I chose to check the “No SSN” button. I was screened within 48 hours.

How much does it cost?

As a volunteer, it costs nothing. It costs FIRST money though. $8 for the national screening. And since I am in New York State, they also paid $65 for a state one. This adds up fast. Which means FIRST is spending many thousands of dollars on background checks.

There is a thread on chief delphi about this. It’s hard to find the relevant info without reading the whole thread so putting up this blog post for easy reference.