java - Spring-data-jpa 刚保存的信息查找不到
扔个三星炸死你
扔个三星炸死你 2017-06-12 09:25:28
0
2
1198

1 后台结构

后台的结构是 SpringMVC, Spring, jpa(HibernateJpaDialect),
DataSource(c3p0), Mysql(InnoBDB),
transactionManager(JpaTransactionManager)。

2 问题环境

@Transactional(value = "transactionManager", isolation = Isolation.READ_UNCOMMITTED)
    public Object addScenicSpot(int tourGuideID, String jsonStr) {
        Djd_js entity = new Djd_js();
        try{
            _setEntity(entity, jsonStr);
            entity.setDaoyouID(tourGuideID);
            jdjsDao.save(entity);
            int spotId = entity.getId();
            //添加信息到消息队列中
        try {
            Sender sender = new SenderImpl();
            sender.getGPSFromBaiduAPI("jdjs", spotId, entity.getDizhi());
        } catch (InterruptedException e) {
            return false;
        }
            return spotId;
        }catch (Exception e){
            return false;
        }
    }
以上是保存的部分,并把得到的 ID 发送到消息队列中,下边是消息队列的处理部分
public boolean updateLngAndLat(MessageVo messageVo) {
        System.out.println("CreateTime--------"+messageVo.getCreateDate());
        System.out.println("Address--------"+messageVo.getContent());
        System.out.println("Id--------"+messageVo.getId());
        Djd_js entity = jdjsDao.findOne(messageVo.getId());
        System.out.println("entity-Address--------"+entity.getDizhi());
        、、运行到这里就直接卡住了,如果注释掉查询,其他的调用皆正常。
        Map<String, Object> result = LngAndLatUtil.getLngAndLat(((MessageVo) messageVo).getContent());
        System.out.println("result--------"+(int)result.get("result"));
        if (1 == (int)result.get("result")){
            entity.setJingdu(Double.valueOf(result.get("lng").toString()));
            entity.setWeidu(Double.valueOf(result.get("lat").toString()));
            System.out.println("message-------------------------------"+"lng:"+Double.valueOf(result.get("lng").toString())+", lat:"+Double.valueOf(result.get("lat").toString()));
            jdjsDao.updateLngAndLatBySenciSpotID(messageVo.getId(), (Double) result.get("lng"), (Double) result.get("lat"));
        }else {
            System.out.println("message-------------------------------False");
        }
        return false;
    }

3 问题描述

前端调用 addScenicSpot() 方法,会将信息保存到数据库中,然后将保存之后的数据控中的ID发送到消息队列中,然后订阅者处理队列中的信息,根据 ID 查询到刚保存的信息,然后调用外部接口查询到经纬度,并将得到的经纬度存储到数据库中。
现在的问题是,保存信息正常,但是到了订阅者处理这边,根据得到的 ID 查找不到保存的信息。

4 猜测问题所在

产生bug的原因是spring事务提交晚于消息队列的生产消息,导致消息队列消费消息时获取到的数据不正确,
灵感来自于这里:http://www.cnblogs.com/taocon...

扔个三星炸死你
扔个三星炸死你

répondre à tous(2)
迷茫

同步调用,改为异步调用?

@Async
getGPSFromBaiduAPI

扔个三星炸死你

已经解决了问题了,应用的这里的方法:http://www.cnblogs.com/taocon...

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!