Transaction management in spring (1)

Transaction management in spring (1)


        As an enterprise-level application framework, Spring defines an abstraction layer on top of different transaction management APIs. Application developers can use Spring's transaction management mechanism without knowing the underlying transaction management API.

 

Programmatic Transaction Management

Transaction management using native JDBC API

1. Get the database connection Connection object

2. Cancel the automatic commit of the transaction

3. Perform an action

4. Manually commit the transaction when the operation is completed normally

5. Rollback transaction when execution fails

6. Close related resources

 

Spring supports both programmatic and declarative transaction management.

Programmatic transaction management: Embed transaction management code into business methods to control the commit and rollback of transactions. When managing transactions programmatically, additional transaction management code must be included in each transaction operation.

Declarative transaction management: In most cases, it is better to use than programmatic transaction management. It separates transaction management code from business methods and implements transaction management in a declarative manner. As a cross-cutting concern, transaction management can Modularity via AOP approach. Spring supports declarative transaction management via the Spring AOP framework.

 

Spring abstracts a complete set of transaction mechanisms from different transaction management APIs. Developers can take advantage of these transaction mechanisms without knowing the underlying transaction API. With these transaction mechanisms, transaction management code can be independent of a specific transaction technology .

Spring's core transaction management abstraction is the interface PlatformTransactionManager which encapsulates a set of technology-independent methods for transaction management. A transaction manager is required regardless of which Spring transaction management strategy (programmatic or declarative) is used.

 

Different implementations of transaction managers in Spring

DataSourceTransactionManager: Only one data source needs to be processed in the application, and it is accessed through JDBC

JtaTransactionManager: Transaction management with JTA (Java Transaction API) on a JavaEE application server

HibernateTransactionManager: Accessing the database with the Hibernate framework

……

The transaction manager is declared in the Spring IOC container as a normal bean

 

 

Manage transactions declaratively with the @Transactional annotation

In addition to declaring transactions in bean configuration files with pointcuts, advice and enhancers, Spring also allows simply annotating transactional methods with the @Transactional annotation.

In order to define a method to support transaction processing, you can add the @Transactional annotation to the method. According to Spring AOP's proxy-based mechanism, only public methods can be annotated.

The @Transactional annotation can be added at the method or class level. When this annotation is applied to a class, all public methods in the class will be defined as transactional.

Just enable the <tx:annotation-driven> element in the bean configuration file and specify a transaction manager for it.

 

If the name of the transaction handler is transactionManager, the transaction-manager attribute can be omitted from the <tx:annotation-driven> element. This element will automatically detect a transaction handler of that name.

 

<!-- Configure Transaction Manager-->

    <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <!-- Set data source-->

        <propertyname="dataSource"ref="dataSource"></property>

    </bean>

   

    <!--启用事务的注解支持 -->

    <!--如果事务管理器的id属性值是transactionManager,transaction-manager属性就可以省略 -->

    <tx:annotation-driven/>

 

 

 

 

用事务通知声明式地管理事务(xml)

<!-- 配置事务管理器 -->

    <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <propertyname="dataSource"ref="dataSource"></property>

    </bean>

   

    <!--配置事务 -->

    <tx:adviceid="txAdvice">

        <!--配置添加事务的方法 -->

        <tx:attributes>

              <!--给具体的方法添加事务 -->

            <tx:methodname="test"propagation="REQUIRED"/>

            <!--给查询的方法添加事务 -->

            <tx:methodname="get*"read-only="true"/>

            <!--给所有的方法添加事务 -->

            <tx:methodname="*"/>

        </tx:attributes>

    </tx:advice>

    <!--配置中AOP -->

    <aop:config>

        <!--配置切入点表达式 -->

        <aop:pointcutexpression="execution(* com.atguigu.service.*.*(..))"

                id="pointCut"/>

        <!--将添加的方法与切入点表达式关联 起来 -->

        <aop:advisoradvice-ref="txAdvice"pointcut-ref="pointCut"/>

    </aop:config>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325955459&siteId=291194637