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 
} 

What is a DDOS? Explaining it to your grandmother

After Friday’s internet attack, I had to explain what a DDOS (Distributed Denial of Service) attack was to my mother. She’s not so good with computers which is why her computer is a Chromebook. Here’s what I came up with:

Imagine I ring your doorbell and then run around the corner. You answer the door, but nobody is there. I do this every hour for six hours. Annoying right? Now imagine I recruit 100 people to do the same thing. Now your doorbell is ringing every 30 seconds. Eek! That’s a DDOS.

What do you think? Good analogy?

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.