Committing a Transaction

Committing a Transaction

When you set the auto-commit mode to false, the operation perfomed by SQL statements are not reflected permanently in a database. You need to explicitly call the commit() method of the Connection interface to reflect the changes made by the transactions in the database. All the SQL statements that appear between two commit() methods are treated as a single transaction and are executed as asingle unit.

Example:
import java.sql.*;

public class createTrans
{
public static void main(String args[])
{
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con=DriverManager.getConnection(“jdbc:odbc:MyDataSource”, ” ”, “ ”);
PreparedStatement ps=con.preparedStatement(“INSERT into product(p_id,p_desc) VALUES (?,?)”);
Ps.setInt(1,1008);
Ps.setString(2,”Hard Disk”);
Int firstctr=ps.executeUpdate();
System.out.println(“First row inserted but not commited”);
ps=con.preparedStatement(“INSERT into product(p_id,p_desc) VALUES (?,?)”);
Ps.setInt(1,1009);
Ps.setString(2,”Hard Disk2”);
Int firstctr=ps.executeUpdate();
System.out.println(“Second row inserted but not commited”);
Con.commit();
System.out.println(“Transaction Committed”);
Ps.close();
Con.close();
}
catch(Exception e)
{
System.out.println(“Error :” +e);
}
}
}

Overridden JDBC methods
Java.sql.Connection.commit()
As defined in the JDBC API, this method commits all work that was performed on a JDBC connectionsince the previous commit() or rollback(), and releases all database locks.
If a global transaction is associated with the current thread of execution do not use this method. If the global transaction is not a container-managed transaction, that is the application manages its own transactions, and a commit is required use the JTA API to perform the commit rather than invoking commit() directly on the JDBC connection.
Java.sql.Connection.rollback()
As defined in the JDBC API, this method rolls back all work that was performed on a JDBC connection since the previous commit() or rollback(), and releases all database locks.
If a global transaction is associated with the current thread of execution do not use this method. If the global transaction is not a container-managed transaction, that is the application manages its own transactions, and a rollback is required use the JTA API to perform the rollback rather than invoking rollback() directly on the JDBC connection.
Java.sql.Connection.close()
As defined in the JDBC API, this method closes the database connection and all JDBC resourcesassociated with the connection.
If the thread is associated with a transaction this call simply notifies the JDBC pool that work on the connection is complete. The JDBC pool releases the connection back to the connection pool once the transaction has completed. JDBC connections opened by the JDBC pool cannot be closed explicitly by an application.
Java.sql.Connection.setAutoCommit(boolean)
As defined in the JDBC API, this method is used to set the auto commit mode of a transaction. The setAutoCommit() method allows Java applications to either:
• Execute and commit all SQL statements as individual transactions (when set to true). This is the default mode, or
• Explicitly invoke commit() or rollback() on the connection (when set to false).
If the thread is associated with a transaction, the JDBC pool turns off the auto-commit mode for all connections factoried in the scope of a partition’s transaction service transaction. This is because the transaction service must control transaction completion. If an application is involved with a transaction, and it attempts to set the auto commit mode to true, the java.sql.SQLException() will be raised.

Scroll to Top