Spring Transaction
Transaction
comprises of a unit of work against a database. Transaction provides “all or
noting” preposition unit of work performed entirely committed or rollback.
Unit of work: consider to transfer account
balance from account A to account B we have to debit account A and credit
account B and this is a unit of work performed to do transfer.
Acronym ACID describes the nature or
properties for transaction.
- Atomic: All of the operations in a transaction happen or none of them happen.
- Consistent: After a successful or failed transaction system is left in a consistent state.
- Isolation: it is about concurrent read write if two or more transaction progressing at time.
- Durable: after a successful transaction data is persisted permanently.
Transaction
attributes:
- Propagation
1.
PROPAGATION_REQUIRED:
transaction already exists then the code will use it otherwise a new
transaction is created. This is the default.
2.
PROPAGATION_SUPPORTS:
transaction exists then the code will use it, but the code does not require a
new one.
3.
PROPAGATION_MANDATORY:
Participates in an existing transaction, however if no transaction context is
present then it throws a TransactionRequiredException.
4.
PROPAGATION_REQUIRES_NEW:
new transaction and if an existing transaction is present then it is suspended.
When the new transaction is complete then the original transaction resumes.
5.
PROPAGATION_NOT_SUPPORTED:
operations needs to be performed non-transitionally.
6.
PROPAGATION_NEVER: code
cannot be invoked within a transaction. if an existing transaction is present
then an exception will be thrown.
7.
PROPAGATION_NESTED: code
is executed within a nested transaction if existing transaction is present; if
no transaction is present then a new transaction is created.
- Isolation
1.
ISOLATION_DEFAULT:
Use the isolation level of the underlying database. This is the default.
2.
ISOLATION_READ_UNCOMMITTED: dirty reads, phantom reads and non-repeatable
reads.
3.
ISOLATION_READ_COMMITTED:
prevents dirty reads but allows phantom reads and non-repeatable reads.
4.
ISOLATION_REPEATABLE_READ:
prevents dirty reads and non-repeatable reads but allows phantom reads.
5.
ISOLATION_SERIALIZABLE:
prevents dirty reads, non repeatable reads and phantom reads.
Dirty Reads- Dirty reads occur when transaction B reads data that has
been modified by transaction A but not committed. The problem occurs when
transaction A rollbacks the transaction, in which case the data read by
transaction B will be invalid.
Non Repeatable Reads- Non-repeatable reads happen when a transaction fires the
same query multiple times but receives different data each time for the same
query.
Phantom Reads - Phantom reads occur when the collection of rows returned
is different when a same query is executed multiple times in a transaction.
- Read only: The read only attribute specifies that the transaction is only going to read data from a database. by default false.
- Rollback rule: specify that transactions roll back on certain exceptions and do not rollback on other exceptions.By default RuntimeException.
- Timeout: Timeout specifies the maximum time allowed for a transaction to run then rolled back.
Spring
Transaction supports:
- It offers both programmatic and declarative transaction.
- Declarative transaction can be used in stand-alone application unlike EJB.
- Support wide range of transactional manager,.
- Spring transactions are good at application using single database. I multiple databases access required have to use XA transaction of JTA.
Spring
transaction managers: soring does not manage transaction directly rather
delegate the responsibility to configured platform specific transaction
manager.
JDBC
|
DataSourceTransactionManager
|
Hibernate
|
HibernateTransactionManager
|
JDO
|
JdoTransactionManager
|
JTA
|
JtaTransactionManager
|
JPA
|
JpaTransactionManager
|
x
No comments:
Post a Comment