ACID properties with examples

Category : Java | Sub Category : Java Interview questions | By Prasad Bonam Last updated: 2023-07-17 08:51:24 Viewed : 341


ACID properties with example:

Certainly! Lets explore the ACID properties with examples in the context of a Java application using a database transaction.

  1. Atomicity:
java
try { // Begin a database transaction conn.setAutoCommit(false); // Perform multiple database operations // ... // Commit the transaction conn.commit(); } catch (SQLException ex) { // Handle any exceptions ex.printStackTrace(); // Rollback the transaction in case of failure conn.rollback(); }

In this example, a database transaction is started by disabling auto-commit mode (conn.setAutoCommit(false)). Multiple database operations are performed within the transaction. If any exception occurs during the transaction, the catch block is executed, and the transaction is rolled back (conn.rollback()) to ensure atomicity. If no exceptions occur, the transaction is committed (conn.commit()).

  1. Consistency:
java
public void updateBalance(int accountId, BigDecimal amount) { try { // Begin a database transaction conn.setAutoCommit(false); // Retrieve the current balance BigDecimal currentBalance = getBalance(accountId); // Perform the balance update BigDecimal updatedBalance = currentBalance.add(amount); // Update the balance in the database updateBalanceInDatabase(accountId, updatedBalance); // Commit the transaction conn.commit(); } catch (SQLException ex) { // Handle any exceptions ex.printStackTrace(); // Rollback the transaction in case of failure conn.rollback(); } }

In this example, the updateBalance method updates the balance of an account in the database within a transaction. Before making the update, the current balance is retrieved, and the desired amount is added to it. The updated balance is then stored in the database. If any exception occurs during the transaction, the catch block is executed, and the transaction is rolled back to maintain consistency.

  1. Isolation:
java
public void transferFunds(int fromAccountId, int toAccountId, BigDecimal amount) { try { // Begin a database transaction conn.setAutoCommit(false); // Set transaction isolation level conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); // Perform the funds transfer withdrawFromAccount(fromAccountId, amount); depositToAccount(toAccountId, amount); // Commit the transaction conn.commit(); } catch (SQLException ex) { // Handle any exceptions ex.printStackTrace(); // Rollback the transaction in case of failure conn.rollback(); } }

In this example, the transferFunds method performs a funds transfer between two accounts. To ensure isolation, the transaction isolation level is explicitly set to TRANSACTION_SERIALIZABLE, the highest level of isolation. This prevents concurrent transactions from modifying the involved accounts while the transfer is in progress, ensuring consistency and isolation.

  1. Durability:

Durability is an inherent property of the database system and is typically handled by the database itself. Once a transaction is committed, the changes made by the transaction are persisted and can survive system failures. Java code alone cannot demonstrate durability as it is managed by the underlying database systems mechanisms, such as write-ahead logging and transaction logs.

The examples above illustrate how Java code can enforce atomicity, consistency, and isolation within a database transaction. Durability, on the other hand, is a responsibility of the database system to ensure the permanent storage and recovery of committed data.

Please note that the examples provided are simplified and may not cover all aspects of implementing ACID properties in a production-level application.


Search
Related Articles

Leave a Comment: