Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member