加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 23小时后风暴崩溃

发布时间:2020-12-14 19:24:06 所属栏目:Java 来源:网络整理
导读:大家好,我有一个基本的Storm应用程序设置,它接收一个推文流并将它们存储在 MySQL数据库中.该应用程序在前约23小时左右运行良好,然后开始出现以下错误: SQL ExceptionSQL State: 08003 它做了几次就死了.我正在使用标准JBDC连接器从Java连接到数据库.存储和
大家好,我有一个基本的Storm应用程序设置,它接收一个推文流并将它们存储在 MySQL数据库中.该应用程序在前约23小时左右运行良好,然后开始出现以下错误:
SQL Exception
SQL State: 08003

它做了几次就死了.我正在使用标准JBDC连接器从Java连接到数据库.存储和设置DB连接的功能代码如下:

private String _db="";
private Connection conn = null;
private PreparedStatement pst = null;

public ArchiveBolt(String db){
    _db = db;
}

private void setupConnection() {
    //Connect to the database
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:8889/twitter_recording","root","root");
    } catch (Exception e){
        e.printStackTrace();
    }
}

public void execute(Tuple tuple,BasicOutputCollector collector) {

    Status s = (Status) tuple.getValue(0);

    //setup the connection on the first run through or if the connection got closed down
    try {
        setupConnection();
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println(e.toString());
    }


    try {

        pst = conn.prepareStatement("INSERT INTO " + _db + " (tweet)" +
                                    "VALUES (?);");

        pst.setString(1,s.toString());

        //execute the SQL
        pst.executeUpdate();

    } catch (SQLException ex) {
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());

        if(ex.getSQLState().equals("08003")){
            setupConnection();
        }

    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

在显然它因为08003错误而崩溃之后我决定如果它抛出该错误它应该重试连接的设置,但是这也没有帮助.有人能指出我正确的方向来解决这个问题吗?

解决方法

After it became apparent that it was crashing because of a 08003 error I decided that if it threw that error it should retry the set up of the connection,however that didn’t help either. Could anyone point me in the right direction for solving this issue?

这里基本上有两个问题需要解决:

>为什么连接首先会丢失?
>为什么不尝试重新连接成功?

对于第一个问题,您应该查看MySQL日志以查看是否有任何迹象.此外,在(重复)“状态

After it became apparent that it was crashing because of a 08003 error I decided that if it threw that error it should retry the set up of the connection,however that didn’t help either. Could anyone point me in the right direction for solving this issue?

3”异常之前检查SQL异常.后者只是告诉你之前连接已经死亡.

我的猜测是问题是以下之一:

>由于不活动,MySQL服务器已超时连接.如果这是问题,您可以在MySQL配置中更改连接超时.
>您的应用程序可能正在慢慢泄漏JDBC连接.

对于第二个问题,一般方法是正确的,但您的代码与描述不匹配.实际上,每次调用execute方法时,看起来总是尝试建立新的数据库连接.这使得异常处理程序中的重新连接调用变得毫无意义. (OTOH,代码显示有人一直在“殴打它”以试图让它发挥作用……这很可能是问题的一部分.)

我会检查setupConnection是否在需要时被调用,并查找可能抛出的任何异常.此外,您应该确保明确关闭()死连接对象…并重新考虑/重新编码您的连接管理,以便它不会泄漏.

为了记录,有一个名为“autoReconnect”的连接URL参数,它在过去曾用于“处理”丢失的连接.不幸的是,最初的实施是不安全的,所以他们有效地禁用了它;有关详细信息,请参阅此问题:Why does autoReconnect=true not seem to work?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读