search

Home  >  Q&A  >  body text

java - Spring-data-jpa The information just saved cannot be found

1 Backend structure

The background structure is SpringMVC, Spring, jpa(HibernateJpaDialect),
DataSource(c3p0), Mysql(InnoBDB),
transactionManager(JpaTransactionManager).

2 Problem environment

@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 Problem description

The front end calls the addScenicSpot() method, which will save the information to the database, and then send the ID in the saved data control to the message queue. Then the subscriber processes the information in the queue and queries the just saved ID based on the ID. information, and then call the external interface to query the longitude and latitude, and store the obtained longitude and latitude in the database.
The current problem is that saving information is normal, but when it comes to subscriber processing, the saved information cannot be found based on the obtained ID.

4 Guess the problem

The reason for the bug is that the spring transaction submission is later than the production message of the message queue, resulting in incorrect data obtained when the message queue consumes messages.
The inspiration comes from here: http://www.cnblogs.com/ taocon...

扔个三星炸死你扔个三星炸死你2732 days ago1448

reply all(2)I'll reply

  • 迷茫

    迷茫2017-06-12 09:27:28

    Synchronous call, change to asynchronous call?

    @Async
    getGPSFromBaiduAPI

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-12 09:27:28

    The problem has been solved, and the method here is applied: http://www.cnblogs.com/taocon...

    reply
    0
  • Cancelreply