Declarative or Programmatic Transaction in Spring

后端 未结 6 1534
小蘑菇
小蘑菇 2021-02-03 10:27

What type of transaction management strategy we should use in Spring? Declarative or Programmatic? Which one is better and under what situation one should use it? Can you give a

相关标签:
6条回答
  • 2021-02-03 11:06

    There are two types of Transaction management that Spring supports:

    1. Programmatic Transaction Management: Transaction is managed with the help of programming and provides extreme flexibility, but it is difficult to maintain.

    2. Declarative Transaction Management: Transaction management is separated from business code and only annotations or XML based configurations are used to manage the transactions.

    0 讨论(0)
  • 2021-02-03 11:08

    Spring offers both programmatic and declarative transactions.

    Programmatic means you have transaction management code surrounding your business code. This gives extreme flexibility, but is difficult to maintain and, well, boilerplate.

    Declarative means you separate transaction management from the business code. You can use annotations or XML based configuration.

    programmatic management is more flexible during development time but less flexible during application life
    declarative management is less flexible during development time but more flexible during application life
    

    http://docs.spring.io/spring/docs/3.0.x/reference/transaction.html

    Declarative Transaction Management allows to eliminate any dependencies on the transaction framework from the Java code. The four participants to provide the transaction support are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.

    Suggest to use Declarative Transaction Management, Alternative for HibernateTemplates either NamedJDBCTemplate or simpleJDBCTemplate

    0 讨论(0)
  • 2021-02-03 11:11

    They are not mutually exclusive.

    You can use decalrative transaction management (@Transactional) in most of cases, and fall back to programmatic transaction management (TransactionTemplate) when you face limitations of Spring AOP (see 11.5.1 Understanding the Spring Framework's declarative transaction implementation) or need to control transactions in more complex ways.

    0 讨论(0)
  • 2021-02-03 11:21

    Programmatic Transaction Management

    1. Allows us to manage transactions through programming in our source code.
    2. This means hardcoding transaction logic between our business logic.
    3. We use programming to manage transactions
    4. Flexible, but difficult to maintain with large amount of business logic. Introduces boilerplate between business logic.
    5. Preferred when relative less transaction logic is to be introduced.

    Declarative Transaction Management

    1. Allows us to manage transactions through configuration.
    2. This means separating transaction logic with business logic.
    3. We use annotations (Or XML files) to manage transactions.
    4. Easy to maintain. Boilerplate is kept away from business logic.
    5. Preferred when working with large amount of Transaction logic.
    0 讨论(0)
  • 2021-02-03 11:25

    Programmatic transaction management is usually a good idea only if you have a small number of transactional operations. For example, if you have a web application that require transactions only for certain update operations, you may not want to set up transactional proxies using Spring or any other technology. In this case, using the TransactionTemplate may be a good approach. Being able to set the transaction name explicitly is also something that can only be done using the programmatic approach to transaction management.

    On the other hand, if your application has numerous transactional operations, declarative transaction management is usually worthwhile. It keeps transaction management out of business logic, and is not difficult to configure.

    0 讨论(0)
  • 2021-02-03 11:26

    http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#tx-decl-vs-prog

    and

    http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html

    in general.

    examples:

    http://www.springbyexample.org/examples/hibernate-transaction-annotation-config.html

    New features: I suggest using DI with SessionFactory. Also take a look at 3.1 new feature: Hibernate 4 support. see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/new-in-3.1.html#d0e1385

    0 讨论(0)
提交回复
热议问题