Spring笔记7---事务集成
一.Spring的事务管理策略。
?1. 事务定义:TransactionDefinition接口。
?
?? 首先,看以下代码:
?
?
<bean id="clinic" p:transactionManager-ref="transactionManager" p:targer-ref="clinicTarget"><property name="transactionAttributes"><props><prop key="*">PROPAGATION_REQUIRED,-BusinessExcepRollback,timeout_120,ISOLATION_READ_COMMITED,+BusinessExcepCommit</prop></props></property></bean>?
?说明:所有的业务方法(*)都要受事务保护(PROPAGATION_REQUIRED);一旦应用抛出了BusinessExcepCommit异常,则Spring收管事务必须提交;一旦应用抛出了BusinessExcepRollback异常,则事务必须回滚;事务超时时间120秒;事务采取的隔离级别是READ_COMMITTED。
(-号表示回滚,+号表示提交)
?
TransactionDefinition的事务传播策略:
?
?
int PROPAGATION_REQUIRED=0 int PROPAGATION_SUPPORTS=1 int PROPAGATION_MANDATORY=2 int PROPAGATION_REQUIRES_NEW=3 int PROPAGATION_NOT_SUPPORTED=4 int PROPAGATION_NEVER=5 int PROPAGATION_NESTED=6
?
2.各种PlatformTrasactionManager实现
?
例如hibernate的TrasactionManager的配置
?
?
<bean id="transactionManager" ref="sessionFactory"/></bean>?
二. 使用@Transactional注解和<tx:annotation-driven/>元素
?
首先,要启用@Transactiona注解必须在配置文件中引入<tx:annotation-driven/>元素
?
1. @Transactional只是Spring提供的非标准注解。
?
2. @Transactional注解可直接应用到接口上,该接口的实现方法也能间接应用。但只有在Spring的应用中支持继承和接口实现,建议尽量将其注解应用到类上,从而兼容Java EE平台注解的语义。
?
3. 除了可启用@Transactiona注解外,还可以借用EJB3.0中引入的@TransactionAttribute注解。
?
4. <tx:advice>元素
?
?
?
<aop:config><aop:advisor advice-ref="txadvice" pointcut="execution(**..clinic.*(..))"/></aop:config><tx:advice><tx:attributes><tx:method name="store*" propagation="REQUIRED"/><tx:method name="refresh*" propagation="REQUIRES_NEW" read-only="true"/></tx:attributes></tx:advice>??
?? ?该元素充当了拦截器的作用,以上面代码为例,会拦截以store、refresh开头的业务方法。开发者需定义若干<tx:method/>子元素,以捕捉具体连接点和指定相应的事务属性.
?? ?<tx:method/>属性包括:
?
?
?
name 方法名propagation 传播策略isolation 隔离级别timeout 事务超时read-only 只读事务rollback-for 触发回滚异常no-rollback-for 触发提交的异常??
?
?
?
?
?
?
?
?
?
?
?
?
?
?