1、事务的属性:ACID
Atomic,Consistent,(事务结束后的状态一致),Isolated,Durable(持久化)
2、JDBC事务配置
1 2 3 4 |
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"ref="dataSource"/> </bean> |
3、编程式事务
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public void saveSpittle(final Spittle spittle){ txTemplate.execute(new TransactionCallback<Void>(){ public Void doInTransaction(TransactionStatus txStatus){ try{ spitterDao.saveSpittle(spittle); } catch(RuntimeException e){ txStatus.setRollbackOnly(); throw e; } return null; } }); } |
txTemplate 是TransactionTemplate 的实例,通过DI注入,
4、声明式事务的属性
各个隔离级别的含义:
Spring对应的定义:
5、声明式事务的配置
1 2 3 4 5 6 7 8 9 10 |
<tx:advice id="txAdvice"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut="execution(**..SpitterService.*(..))" advice-ref="txAdvice"/> </aop:config> |
6、注解驱动的声明式事务配置
<tx:annotation-driven transaction-manager="txManager"/> 如果 transaction manager bean的id=transactionManager可以省略这个属性。
@Transactional
Posted in: spring practise
Comments are closed.