在Controller中使用saveOrUpdate时,不会调用Transaction Interceptor

时间:2018-01-10 10:51:46

标签: spring hibernate transactions

背景/概念:

我试图通过使用Hibernate(5.0.5.Final)+ Spring(4.2.3.RELEASE)将数据保存到Mysql数据库。

DAO:

@Repository
@Transactional(propagation = Propagation.REQUIRED,rollbackFor =
{Exception.class})
 public abstract class DaoImpl implements Dao {

@Autowired
private SessionFactory sessionFactory;

  @Override
public <T extends Entity> void saveOrUpdate(T entity) {
    if (entity.getId() == null || entity.getId() == 0) {
        create(entity);
    } else {
        update(entity);
    }
}

服务:

@Service("entityService")
public class EntityServiceImpl implements EntityService {
    .....
}

Tx配置:

 <tx:annotation-driven transaction-manager="transactionManager" />

 <!-- Spring/Hibernate TransactionManager -->
 <bean id="transactionManager" 
 class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="dataSource" ref="mainDataSource" />
</bean>

<!-- the transactional advice  -->
<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" propagation="SUPPORTS" isolation="DEFAULT" 
 read-only="true"/>
        <tx:method name="find*" propagation="SUPPORTS" isolation="DEFAULT" 
read-only="true"/>
        <tx:method name="list*" propagation="SUPPORTS" isolation="DEFAULT" 
read-only="true"/>
        <tx:method name="is*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
        <tx:method name="check*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
        <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="change*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
        <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="servicesConfig"
                  expression="execution(* com.example.service..*Service.*(..))"/>
    <aop:advisor pointcut-ref="servicesConfig" advice-ref="defaultTxAdvice"/>
</aop:config>

Web xml:

   <filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>sessionFactory</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

问题:

  1. 我创建了Junit Test并将服务(saveOrUpdate)数据调用到db。它正在工作(拦截器叫做&amp; data saved)。
  2. 我调用了我的服务来更新控制器中的数据并且它无法正常工作(拦截器未被调用且数据未被保存)。
  3. 单元测试

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:application-main-context.xml")
    public class TestService {
    
        @Autowired
        private UserHistoryService entityService;
    
        @Test
        public void testSaveNewKeyword(){
            entityService.saveKeyWord("SRD");
            entityService.updateScroing("text", "SRD");
        }
    
    }
    

    可以将数据更新到数据库的控制器代码

    @Controller
    public class SearchController {     
    
    @Autowired
    private UserHistoryService entityService;
    
    @RequestMapping(value = "/rd/search/result/{base}/{page}")
    public String rdSearchResult(Model model, @PathVariable("base") String base, 
    @PathVariable("page") int page,
            @ModelAttribute("searchDataModel") SearchDataModel searchDataModel) 
     {
        entityService.saveKeyWord("SRD");
        entityService.updateScroing("text", "SRD");
     }
    }
    

    我试图调试并观察这两者之间的差异。 我在Junit中运行测试我看到了Transaction Interceptor被调用。 但我运行Web应用程序,我没有看到事务拦截器调用。

0 个答案:

没有答案