使用事务保证了数据的一致性,避免了脏读。
事务采用的是ACID原则:
A原子性:
C一致性:
I隔离性:多个业务操作同一资源时,防止数据破坏。
D持久性:事务一旦提交,无论系统发生什么问题,结果都不会再被影响,被持久化写到存储器中。
Spring中事务管理:
声明式事务:配合aop
- 实现事务管理,
<!--实现事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="Datasource" />
</bean>
- 结合aop实现事务织入
<!--结合aop实现事务织入-->
<!--配置事务通知-->
<tx:advice transaction-manager="transactionManager" id="transactionInterceptor">
<!--配置事务的特性,哪些方法使用事务-->
<tx:attributes>
<tx:method name="adduser" propagation="REQUIRED"/>
<tx:method name="delete" propagation="REQUIRED"/>
<tx:method name="getUser" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
- 配置aop切入
<aop:config>
<aop:pointcut id="txadvice" expression="execution(* com.mapper.*.*(..))"/>
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="txadvice"/>
</aop:config>
编程式事务:代码中进行事务管理。