As a definition, the word transaction represents a unit of work or a set of operations that are needed to be done together and completed successfully together. In case any of the transaction operations fail, then all the transactions should be failed and all the transaction changes should be removed. In a very short statement, transaction can be considered as an atomic large operation.
From a database perspective, a transaction is a set of SQL statements – Update/Delete/Insert – that are needed to be applied to the database successfully, and in case of the failure of any of the SQL queries included in the transaction, all the previous queriy changes should be removed or in the database transaction language, it should rollback.
Transactions have 4 properties called ACID properties:
- Atomicity: The transaction operations are indivisible.
- Consistency: The database data should be consistent whether the transaction succeeded or rollback.
- Isolation: transactions are isolated from each other and no transaction can affect another concurrent running transaction.
- Durability: Once the transaction succeeded, or in other word, committed, all the changes are permanent and visible to other transactions.
Suppose you have a bank application and you want to implement the operation of money transfer between customers, the operation should be as follows:
- Deduct the amount from customer #1
- Add the amount to customer #2
- Log the transaction details in the database
Now, suppose that the database has a constraint about the max number of transfer transactions that can be done by any customer in the same day.
Your operation will have a big problem if the operation number 2 failed due to the above constraint. The problem is that the money is already deducted from customer #1 and will never go back to him again.
Such operations are called atomic operation as it should be done successfully as a whole, or rollback as a whole (i.e. in case the adding of the amount to the customer #2 failed, rollback the money deduction that took place in the statement #1)
The JEE provides a set of interfaces that can be used to manage enterprise application transactions, the interfaces hide the vendor specific interfaces and provide a generic interfaces that can be used with all JEE compatible database engines.
2 types of transaction management techniques are supported in the JEE i.e. the container-managed transactions and the bean-managed transactions.