侵入代码式 的事务 管理

在spring aop 事务管理中发现,咱们是在service层实现的事务管理。 
如今有以下场景,你们讨论下看如何实现? 
ControllerA、ControllerB、ControllerC….共同依赖ServiceA、ServiceB,上述Controller的save操做须要把数据同步ServiceA和ServiceB。 
因为每一个Controller保存ServiceB的extraData字段是经过Json组装的,因此每一个Controller具备独特性。若是在Service层实现事务管理,ServiceA将会变的异常庞大,须要判断是哪一个Controller过来的数据,而后组装ServiceB的extraData字段。 
另外一种思路,咱们是否能够把每一个Controller组装ServiceB的extraData字段过程放在各自的Controller,而后在Controller实现事务管理呢? 
通过测试,在Controller层加事务,在spring.xml的aop:config添加对Controller的声明式事务拦截,结果未生效。在Controller的class加上@Transactional也未生效。最后采起的编程式事务实现的。 
咱们在Spring.xml配置sessionFactory和transactionManager,若是已经配置声明式事务,这步能够忽略。java

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="c3p0DataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">#{ nplat['db.dialect'] }</prop> <prop key="hibernate.hbm2ddl.auto">none</prop> <prop key="hibernate.connection.release_mode">after_transaction</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.max_fetch_depth">3</prop><!-- 抓取的级联深度 --> <prop key="hibernate.jdbc.fetch_size">50</prop><!-- 批量抓取的数量.mysql不支持 --> <prop key="hibernate.jdbc.batch_size">30</prop><!-- 批量写入的数量 --> <prop key="javax.persistence.validation.mode">none</prop><!-- HiberV3.5以上需配置该项 --> <!-- <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 强制Hibernate以更人性化的格式将数据存入二级缓存 <prop key="hibernate.cache.use_structured_entries">true</prop> --> </props> </property> <property name="packagesToScan"> <list> <value>com.gina.gc</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean>

 

 

而后在每一个Controller注入transactionManager:mysql

@Resource private PlatformTransactionManager transactionManager;

下面讲解如何在Controller的save方法加上编程式事务:spring

@RequestMapping("/save") @ResponseBody public String save(@Validated BaseSetting info) { DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = transactionManager.getTransaction(defaultTransactionDefinition); try { serviceA.save(A); serviceB.save(B); ... transactionManager.commit(status); } catch (Exception e) { transactionManager.rollback(status); e.printStackTrace(); log.error("sava *** error" + e.toString()); return ERROR(e.toString()); } return OK(); }

这样咱们便实现了在Controller层加上事务管理。 
虽然说你们建议把事务加在Service,但不一样状况不一样处理方案,真正到项目中还得综合考虑,灵活运用。sql

相关文章
相关标签/搜索