springJDBC的的事务处理


一、简单的注解方式 <tx:annoation-driven/>只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解     加在service层,那么service类就得是spring容器管理的,事务才能生效。 回滚问题:默认是遇到RuntimeException回滚,如果要根据业务回滚
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
  spring配置文件关键配置就可以了:
 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
   <property name="dataSource" ref="dataSource" />
</bean>
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource" />  
   </bean> 
<tx:annotation-driven transaction-manager="transactionManager"/>
 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
   <property name="dataSource" ref="dataSource" />
</bean>
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource" />  
   </bean> 
<tx:annotation-driven transaction-manager="transactionManager"/>
  回滚配置还有一种方式就是:只要在service类上写 @Transactional,然后在配置文件写  
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
      <props>
             <prop key="delete*">PROPAGATION_REQUIRED, -Exception</prop>  
             <prop key="add*">PROPAGATION_REQUIRED, -Exception</prop>  
             <prop key="update*">PROPAGATION_REQUIRED, -Exception</prop>  
             <prop key="save*">PROPAGATION_REQUIRED, -Exception</prop>     
             <prop key="browse*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
             <prop key="list*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
             <prop key="load*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
             <prop key="get*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
             <prop key="is*">PROPAGATION_REQUIRED,readOnly,-Exception</prop>
             <prop key="*">PROPAGATION_REQUIRED, -Exception</prop>  
      </props>
   </property>
</bean>
   未完待续。。。              

猜你喜欢

转载自602540410.iteye.com/blog/2295893