- spring中事务分为编程式事务和声明式事务(多)
- 事务的ACID
- 原子性:事务中涉及的操作要么全部成功,要么全部失败
- 一致性:事务提交前后需保持一直
- 隔离性:各个事务之间的操作是隔离开的,各自独立完成,互不影响
- 持久性:事务的操作一旦提交,对于数据库的操作就是永久性的
- 声明式事务的配置文件基本类似
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test1?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transcationManager">
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete"/>
<tx:method name="delete"/>
<tx:method name="query" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txCutPoint" expression="execution(* com.feng.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txCutPoint"/>
</aop:config>