基于Spring4
1.事务概念
首先理解下事务的概念----全有或全无的操作被称为事务(transaction)
吐槽下这个词语,实在是不友好,看来还是不能望文生义啊。
一般来说典型的事务有转账、买票等。
然后是事务的四个特性:原子性(Atomic)、一致性(Consistent)、隔离性(Isolated),持久性(Durable)
2.选择事务管理器
2.1 JDBC事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
其中 ref 引用数据源
jdbc的DataSourceTransactionManager调用javax.sql.Connection来管理事务。使用commit()提交事务,使用rollback()回滚
后续大概还有hibernate 和jpa
3.声明式事务
Spring提供两种方式的事务支持:编码式和声明式
编码式:细粒度控制,精确,耦合度高
声明式:已用,耦合度低,基于AOP
定义事务:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation=""/>
</tx:attributes>
</tx:advice>
通知事务:
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression=" execution(* service..*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
Spring另一种配置事务的方法是使用注解