zoukankan      html  css  js  c++  java
  • 一个事务下,抛出异常事务回滚,要在开启一个线程将数据持久到数据库

    需求:阅读记录孩子不存在时,发送一条私信给用户,同一个用户私信不能重复发。

    ====阅读记录孩子不存在时给用户发私信表(kid_user_station_letter_when_child_not_exist_in_reading_record)
    user_id - 用户ID
    time - 时间

    遇见的问题:同一个事物下,私信发送成功,但没有保存到数据库中。

    原因:事务回滚

    解决:在开启一个线程,保存数据

    KidChildInfo childInfo = childInfoMapper.selectByPrimaryKey(childId);

    if (childInfo == null) {
      sendChildNotExistLetterToUser(userId);
      throw new Exception(String.format("孩子不存在:用户ID=%d, 孩子ID=%d, 已经发送解决方案私信给该用户", userId, childId));
    }

    private void sendChildNotExistLetterToUser(int userId) {
            AsynTaskService.run(new Runnable() {
                @Override
                public void run() {
                    int count = commonMapper.executeQueryInt(String.format("SELECT COUNT(*) FROM kid_user_station_letter_when_child_not_exist_in_reading_record WHERE user_id=%d AND time>DATE_ADD(NOW(),INTERVAL -1 DAY)", userId));
                    if (count == 0) {
                        commonMapper.executeInsert(String.format("INSERT INTO kid_user_station_letter_when_child_not_exist_in_reading_record(user_id, time) VALUES(%d, NOW()) ON DUPLICATE KEY UPDATE time=NOW()", userId));
                        stationLetterService.sendMessage(124244, userId, ""/**~!{
            Hi, 我们发现你的账号,孩子信息出错了,会导致闪退。
            为保障能正常使用,请重新添加下孩子吧。                   
                    }*/);
                    }
                }
            });
        }
  • 相关阅读:
    c++面试题
    MFC 字符串类CString 源代码
    c++ ofstream & ifstream文件流操作
    理解ip和端口
    求解最长回文字符串
    @Document元注解的使用
    JVM、JRE和JDK的理解
    Java发展历程及各版本新特性
    Maven的安装配置
    认识Java注解
  • 原文地址:https://www.cnblogs.com/halo623/p/13813591.html
Copyright © 2011-2022 走看看