Declarative or Programmatic Transaction in Spring

后端 未结 6 1539
小蘑菇
小蘑菇 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: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

提交回复
热议问题