How to start a transaction in JDBC?

How to start a transaction in JDBC?

To start a transaction in JDBC (Java Database Connectivity), you typically follow a set of steps to ensure that a series of database operations are treated as a single unit of work. JDBC provides methods to manage transactions using the Connection object. Here's how you can start a transaction in JDBC:

  1. Establish a Database Connection: First, create a database connection using the DriverManager or a connection pool if you are using one. Open the connection to the database.

    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password"); 
  2. Disable Auto-Commit: By default, JDBC starts in auto-commit mode, where each SQL statement is treated as a separate transaction and is automatically committed. To start a transaction, you must disable auto-commit mode for the connection.

    connection.setAutoCommit(false); 
  3. Perform Database Operations: Perform the desired database operations (e.g., INSERT, UPDATE, DELETE) within the transaction. These operations will be part of the same transaction until you explicitly commit or roll back.

    Statement statement = connection.createStatement(); statement.executeUpdate("INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2')"); statement.executeUpdate("UPDATE my_table SET column1 = 'new_value' WHERE column2 = 'value2'"); // Additional database operations... 
  4. Commit the Transaction: If all the database operations within the transaction are successful and you want to make the changes permanent, you can commit the transaction.

    connection.commit(); 
  5. Roll Back the Transaction (Optional): If an error occurs during the transaction or you want to undo the changes made within the transaction, you can roll back the transaction.

    connection.rollback(); 
  6. Close the Connection: Finally, close the database connection to release resources.

    connection.close(); 

Here's a complete example that demonstrates starting a transaction, performing database operations, and committing the transaction:

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JDBCTransactionExample { public static void main(String[] args) { String jdbcUrl = "jdbc:mysql://localhost:3306/mydb"; String username = "yourUsername"; String password = "yourPassword"; try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) { connection.setAutoCommit(false); // Start a transaction Statement statement = connection.createStatement(); statement.executeUpdate("INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2')"); statement.executeUpdate("UPDATE my_table SET column1 = 'new_value' WHERE column2 = 'value2'"); connection.commit(); // Commit the transaction } catch (SQLException e) { e.printStackTrace(); // Handle exceptions and roll back the transaction if necessary } } } 

By following these steps, you can start and manage a transaction in JDBC, ensuring that a series of database operations are treated as a single unit of work and can be committed or rolled back as needed.


More Tags

hazelcast microsoft-metro runonce android-handler superscript space-complexity java-5 cqlsh event-driven python-import

More Java Questions

More Stoichiometry Calculators

More Gardening and crops Calculators

More General chemistry Calculators

More Chemistry Calculators