12.声明式事物
回顾事物
把一组业务当成一个业务来做,要么都成功,要么都失败
在事物项目开发中,十分重要,涉及到数据的一些处理问题,不能马虎
确保完整性和一致性
事物ACID原则:
- 原子性(Atomicity):一个事务是一个不可分割的工作单位,事务中包括的动作要么都做要么都不做。
- 一致性(Consistency):事务必须保证数据库从一个一致性状态变到另一个一致性状态,一致性和原子性是密切相关的。
- 隔离性(Isolation):一个事务的执行不能被其它事务干扰,即一个事务内部的操作及使用的数据对并发的其它事务是隔离的,并发执行的各个事务之间不能互相打扰。
- 持久性(Durability):持久性也称为永久性,指一个事务一旦提交,它对数据库中数据的改变就是永久性的,后面的其它操作和故障都不应该对其有任何影响。
log4j.properties
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=D:\\log.out
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
Spring 事物管理
声明式事物:AOP
编程式事物:需要在代码种,进行事物管理
需要事物的原因:
如果不配置事物,数据可能回存在不一致的情况
如果不在SPRING中配置事物声明,我们就需要在代码中手动配置事物
事物在项目开发十分重要,设计数据库一致性和完整性问题,不容马虎
配置
<!--配置声明式事物-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
<!--结合aop配置事物置入-->
<!--配置事物类通知-->
<tx:advice id="interceptor" transaction-manager="transactionManager">
<!--给方法配置事物-->
<tx:attributes>
<tx:method name="Insert" propagation="REQUIRED"/>
<tx:method name="Delete" propagation="REQUIRED"/>
<tx:method name="Uptate" propagation="REQUIRED"/>
<tx:method name="Select" propagation="REQUIRED"/>
<!--一般配置*号即可-->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事物切入-->
<aop:config>
<!--*.*(..) 代表里面的所有的类和所有的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.yuan.mapper.impl.*.*(..))"/>
<aop:advisor advice-ref="interceptor" pointcut-ref="pointcut"/>
</aop:config>