Using Transactions in JDBC

By: Ramlak Emailed: 1594 times Printed: 2041 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

By default, JDBC classes operate in auto-commit mode. This means that each SQL statement executed is considered a separate transaction ( a singleton transaction) and a commit is made at the completion of the statement. In order to group a set of transactions together, this autocommit mode must be disabled using the connection class setAutoCommit method and passing the method boolean false value.

With autocommit disabled, there is always an implicit transaction in place. To commit a series of previously executed SQL statements to the database, an explicit commit can be made by calling the Connection method commit. Alternatively, a rollback can be made by calling the Connection method rollback. This rolls back the current transaction and restores the database to the state bit was in before the start of the current transaction. Failure to commit a transaction before closing the corresponding Connection object will lead to an automatic rollback of the database updates; all work will be lost. Developers should be sure that all work is committed to the database before closing the Connection.

Various database-dependant isolation levels can be set. There are methods in the DatabaseMetaData class to learn the existing defaults in place in the current session and methods in the Connection class to change the current isolation level.

JDBC Isolation Mode Description
TRANSACTION_NONE Transactions are not supported. Not all databases support this mode; most require some level of transactions to be in place.
TRANSACTION_READ_COMMITTED Only reads on the current row are repeatable.
TRANSACTION_READ_UNCOMMITTED Rows being used by a tranaction can be read even if the rows have not been committed.
TRANSACTION_REPEATABLE_READ Reads on all rows of a result are repeatable.
TRANSACTION_SERIALIZABLE Reads on all rows of a transaction are repeatable in the order in which they were executed.

JDBC Home | All JDBC Tutorials | Latest JDBC Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Thanks for this tutorial . This provides me lots of help for understating basics....

View Tutorial          By: Pranit at 2011-07-25 09:12:29

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ramlak
While Loop in VB.net
For Each…Next Loop in VB.net
For Loop in VB.net
Do Loop in VB.net
Setting Up SSL on Tomcat
Unicode and UTF-8 in C
Sample program to demonstrate the use of ActionListener
java.io.IOException: HTTPS hostname wrong: should be
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
NotifyUtil::java.net.ConnectException: Connection refused: connect
'LINK.EXE' is not recognized as an internal or ext
Using Transactions in JDBC
What is the ACID principal?
How connection pooling works in Java and JDBC
A simple JDBC application sample code

More Tutorials in JDBC
TEXT datatype SPLIT in MSSQL - to solve the 8000 limit set by varchar
Import TEXT to TABLE in MSSQL
What is Referential Integrity in databases?
Handling CSV in Stored Procedures
setSavepoint and releaseSavepoint Example in Java
Calling a Stored Procedure from JDBC in Java
java.lang.NoClassDefFoundError and java.lang.NoSuchMethodError
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components
SELECT Statements
WHERE Clauses in SQL
Joins example in SQL
Common SQL Commands
Result Sets, Cursors and Transactions in SQL
Stored Procedures example in SQL

More Latest News
Most Viewed Articles (in JDBC )
A simple JDBC application sample code
How connection pooling works in Java and JDBC
JDBC Components
Using JDBC to connect to MySQL from Java Program
Using the DriverManager Class vs Using a DataSource Object for a connection
Using JDBC to extract data from a database and output to an XML document
PreparedStatement Example in Java
WHERE Clauses in SQL
Stored Procedures example in SQL
JDBC Architecture
What is the ACID principal?
Common SQL Commands
setSavepoint and releaseSavepoint Example in Java
Handling CSV in Stored Procedures
What is Referential Integrity in databases?
Most Emailed Articles (in JDBC)
The Structure of JDBC
A simple JDBC application sample code
Using Transactions in JDBC
JDBC Components
Using JDBC to extract data from a database and output to an XML document
What is the ACID principal?
Using JDBC to connect to MySQL from Java Program
How connection pooling works in Java and JDBC
JDBC and Tomcat context settings
PreparedStatement Example in Java
Data Access Technologies in Java
Result Sets, Cursors and Transactions in SQL
Handling CSV in Stored Procedures
Creating Database Connection Pool in Tomcat 5.0 and Tomcat 5.5 for MySQL and Java
JDBC Basics and JDBC Components