50% off All Oracle and Java Exams!

Making a New Year’s resolution to get Java certified? For this month only (expires 12/31/2019), Oracle is offering 50% off all digital training and exam vouchers. This means if you want to get Java (or any other Oracle certification), you can do it for half the price! Better yet, the vouchers are good for 6 months from date of purchase. Why wait till the prices go up?

Need help? Our new book for the Java 11 1Z0-815 exam just went on sale last month! Jeanne and I offer a wide variety of books for Java 8 certifications including the Java 8 OCA 1Z0-808 exam or the Java 8 OCP 1Z0-809 exam, or if you’re taking both we offer a Java 8 Certification Kit and a Practice Questions book.

The first step to getting certified is making the decision to get certified! Then, you purchase a study guide and draw up a study plan. For example, you could decide to read one chapter a week. Finally, you start taking practice exams to determine what topics you need to study more. And since the vouchers expire in 6 months, you have a nice incentive to not give up!

OCA/OCP 8 Programmer Practice Tests AVAILABLE NOW!

Jeanne and I are really excited to announce that our new book OCA / OCP Practice Tests: Exam 1Z0-808 and Exam 1Z0-809 is now shipping. In fact, we just received our copies today! The book is available in both print and digital formats from Amazon and other major book retailers. In addition, purchasing this book grants the holder access to Sybex’s interactive test bank!

This book serves as worthwhile companion to our previous two books, now available as set, The OCA / OCP Certification Kit. With this book you are really getting two books in one, as Jeanne and I packed over 1000+ questions into this text, covering both the OCA and OCP exams. To better prepare you for test day, we also threw in two simulated OCA/OCP exams. Whether your taking the OCA exam now and thinking about the OCP exam down the road, or taking the OCP exam and need a refresher on the cumulative material, this book has got you covered!

This book covers 100% of all exam objectives in these practice tests, which means you’ll be ready for:

  • Java basics, class design, and data types
  • Using decision and loop constructs
  • Building and using arrays
  • Methods, encapsulation, and inheritance
  • Mastering Java Streams
  • Working with the Date/Time API and Localization
  • Understanding design patterns and principles
  • Writing Lambda expressions and functional interfaces
  • Exceptions and assertions
  • Java File I/O (NIO.2)
  • Concurrency and localization
  • JDBC and database applications

Jeanne and I put a lot of time, energy, and planning into this new book and we really hope our reader enjoy it!

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 
}