New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Committing and rolling back transactions

From: Java Database Integration with JDBC

Video: Committing and rolling back transactions

A database transaction is one or more actions that are making changes to data in a database server or local database file. Whenever you make changes such as inserts, updates, or deletes, you're creating transactions. And in JDBC, these transactions are what we call auto-committed. That is, if you don't see anything else, the changes that you request are made and committed to the database immediately. But most database management systems give you the ability to explicitly commit and roll back transactions. It depends on what database you're using, and how you're using it.

Committing and rolling back transactions

A database transaction is one or more actions that are making changes to data in a database server or local database file. Whenever you make changes such as inserts, updates, or deletes, you're creating transactions. And in JDBC, these transactions are what we call auto-committed. That is, if you don't see anything else, the changes that you request are made and committed to the database immediately. But most database management systems give you the ability to explicitly commit and roll back transactions. It depends on what database you're using, and how you're using it.

For example, with MySQL, it depends on what backend database engine you are using. I'm going to go to phpMyAdmin to my Explore California database, and I'll choose one of my tables, the admin table, and I'll go to the Operations tab. Notice that the storage engine for my database tables is innoDB, and I have specifically selected that because innoDB supports transactions, row-level locking foreign keys, and other advanced database functionality. If I had chosen the default database type MyISAM, I would not have gotten many of those features, including transactions.

But innoDB does support transactions, and so I'll be able to demonstrate how JDBC handles transactions with this sample database. I'll go back to Eclipse where I'm working with a version of my project called Transactions. This is a version of my update application where I'm asking the user for a primary key value, and then a new password, and I'm updating the database at the server level. I'll expand my editing window and then run the application. I'll type in a number of an existing row and a new password.

I get back a message saying I have succeeded and that the connection has been closed. Then I'll run the application again and show that the new password was successfully updated, and I'll stop the application. Now, again, the reason the change was made is because my transaction is auto-committing. But here's how you can change your connection so that it doesn't auto-commit. I'll need a reference to my connection, and right now my connection is being managed by my Singleton ConnectionManager class. I'll place the cursor before the call to the update method, and I'll create a reference to my connection object with Connection conn = ConnectionManager.getInstance().getConnection().

I will make sure I have included an import for the connection interface in Java.SQL. Now I'm going to set the autocommit property to false with conn.setAutoCommit, and I'll pass in a value of false. So far, so good. Now when I go to the update method, this is where the SQL statement is being processed and executed. And previously, when the connection object's AutoCommit property was set to the default of true, the change was made and committed to the database when the executeUpdate method was called.

But now that I have set AutoCommit to false, that's no longer the case, and I have to explicitly commit the change. So I'll go back to my main class, and down here I'll call conn.commit, and then I'll do a little bit of system output, and I'll output the message Transaction committed, and I'll test my code again. Once again, I'll type in the row number and another new password. This time I'll just use pwd. I get back the message, Success, and that it was committed. Then I'll run the application again to see the data, and I'll see the change was made.

But now let's change that logic so that we're rolling back the connection. I will change from commit to rollback, and I'll change the message to indicate what I did. I'll run the code again, I'll type in the row number and new password, and I get back the message that the update method call was successful but that I have rolled back the transaction. So, now I'll run the application again, and I'll see that the change was not made. I still have the old password of pwd. So, once you have set the connection object's AutoCommit property to false, you can then explicitly commit or roll back your transactions as you need to.

The specific logic of your application will differ depending on the type of application and your reasons for rolling back transactions. But the code is pretty simple to use. Just make sure that you have set AutoCommit to false, that you're using a database management system that supports transactions, and that you're calling the commit or rollback methods in the appropriate places of your application.

Show transcript

This video is part of

Image for Java Database Integration with JDBC
Java Database Integration with JDBC

32 video lessons · 6001 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 5m 23s
    1. Welcome
      53s
    2. What you should know before starting this course
      1m 20s
    3. Using the exercise files
      3m 10s
  2. 12m 2s
    1. Testing your Java development environment
      5m 14s
    2. Importing a MySQL database
      5m 1s
    3. Creating a database user in MySQL
      1m 47s
  3. 32m 47s
    1. What is JDBC?
      4m 26s
    2. Choosing a JDBC driver
      6m 46s
    3. Connecting to a MySQL database server
      8m 7s
    4. Connecting to a HyperSQL database file
      6m 23s
    5. Executing a static SQL statement
      7m 5s
  4. 17m 42s
    1. Connecting to multiple databases
      6m 24s
    2. Handling JDBC exceptions
      7m 7s
    3. Closing database resources in Java 7
      4m 11s
  5. 47m 25s
    1. Looping through result sets
      8m 23s
    2. Moving the cursor in scrollable result sets
      5m 51s
    3. Limiting the number of fetched rows
      6m 57s
    4. Filtering data with prepared statements
      6m 58s
    5. Calling stored procedures
      5m 48s
    6. Handling multiple values from stored procedures
      5m 54s
    7. Using generic getter methods in Java SE 7
      7m 34s
  6. 45m 23s
    1. Managing data entities with JavaBean classes
      5m 0s
    2. Retrieving a single row as a JavaBean object
      6m 5s
    3. Inserting rows with prepared statements
      8m 2s
    4. Updating rows with prepared statements
      5m 4s
    5. Deleting rows with prepared statements
      4m 9s
    6. Managing data with updatable result sets
      6m 6s
    7. Using a persistent database connection
      6m 43s
    8. Committing and rolling back transactions
      4m 14s
  7. 9m 35s
    1. Getting the DatabaseMetaData object
      3m 40s
    2. Getting column and data type information
      5m 55s
  8. 50s
    1. Next steps
      50s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.