Wednesday, 24 October 2018

Spring Transaction Management

  • Declarative Transaction Management

    • Declarative transaction management is the most common Spring implementation as it has the least impact on application code.
    • The XML declarative approach configures the transaction attributes in a Spring bean configuration file. Declarative transaction management in Spring has the advantage of being less invasive. 
    •  There is no need for changing application code when using declarative transactions.       All you have to do is to modify the application context.
  • Methods
    •  XML based
    • Annotations based
  • Steps
    • Step 1: Define a transaction manager in spring.xml
      • <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="txManager"/>
    • Step 2: Turn on support for transaction annotations in spring.xml
      • <tx:annotation-driven transaction-manager="txManager"/>
    •  Step 3: Add the @Transactional annotation to the createEmployee Method      @Transactional
    • public int createEmployee(Employee employee) {
..

  • Annotation attributes
    • Transaction readOnly
      • If you don’t explicitly set readOnly attribute to true, you will have read/write transactions.Its always better to explicitly specify the readOnly attribute, as we have noticed some massive performance improvements with Hibernate because of this.
    • Transaction propagation
      • Transaction propagation is REQUIRED by default, which means that the same transaction will propagate from a transactional caller to transactional callee. It will create a new transaction or reuse the one if available. For example, if a read-only transaction calls a read-write transaction method, the whole transaction will be read-only.
      • Depending on the transaction propagation attribute (like for REQUIRES_NEW), sometimes the existing transaction is suspended/paused at some point, a new one is always started and eventually committed, and after that the first transaction is resumed.
    • Isolation Level
      • Isolation level defines a contract between transactions.
        • Read Uncommitted – Allows dirty reads, when a transaction is not yet committed by a thread and another thread is reading the dirty data.
        • Read Committed – Does not allow dirty reads. Only lets a thread to read values which have already been committed by other running transactions in another threads.
        • Repeatable Read – If the same data is read twice in the same transaction, it will always be the same. This level guarantees that any data once read cannot change.
        • Serializable – Transactions occur with locking at all levels (read, range and write locking), because of which they are executed in a fixed sequence. It doesn’t allow concurrent transactions and leads to a performance hit.

  •  Programmatic Transaction Management
    • Spring provides Support for programmatic transaction management by using TransactionTemplate or PlatformTransactionManager implementation.



No comments:

Post a Comment

Search This Blog

Contact us

Name

Email *

Message *