简体   繁体   English

Java-SQL和线程-处理冻结

[英]Java - SQL And Threads - Dealing With Freezing

I'm developing a plugin for Bukkit and I'm starting to experiment with SQL. 我正在为Bukkit开发插件,并开始尝试SQL。 I have everything working, and have functions for less code bloat, etc. 我可以进行所有工作,并具有减少代码膨胀等功能。

My problem is that I noticed when I execute SQL updates the server will freeze all operations for about 5 seconds. 我的问题是我执行SQL更新时注意到服务器将冻结所有操作约5秒钟。 With more looking into it, I had just enclosed all SQL statements into threads, and the issue has gone away. 随着更多的研究,我将所有SQL语句封装到线程中,问题已经消失了。 From my own personal guess, I imagine the freezing is caused by the SQL executing inside of the server thread, when these statements are executed since I'm waiting back on a result from the database, it will cause the main server thread to freeze until it retrieves these results, thus by putting the statements into a seperate thread this fixes the issue. 根据我个人的猜测,我认为冻结是由服务器线程内部执行的SQL引起的,由于我等待数据库的结果而执行这些语句时,它将导致主服务器线程冻结直到它检索这些结果,从而通过将语句放入单独的线程中来解决此问题。

My question is, is it okay that I just put all SQL statements in threads? 我的问题是,将所有SQL语句放入线程中可以吗? Is this a duct tape fix? 这是胶带修复吗? Can I do a better job at fixing this issue? 我可以在解决此问题上做得更好吗?

Example Function 示例功能

public void makeUpdate(String statement){

    // TO BE USED WITH - INSERT, UPDATE, or DELETE
    Connection con = null;
    Statement st = null;

    String url = "jdbc:mysql://127.0.0.1/mydata_base";
    String user = "myuser";
    String password = "mypassword";

    try {
        con = (Connection) DriverManager.getConnection(url, user, password);
        st = (Statement) con.createStatement();

        int rs = st.executeUpdate(statement);

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Version.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    } finally {
        try {
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }
}

Example of code that causes server to freeze 导致服务器冻结的代码示例

// Here would be 3 statements updating user info.
// This would cause the server to freeze for about 5 seconds until these completed.
makeUpdate("UPDATE blah SET blah = blah WHERE blah = blah");
makeUpdate("UPDATE blah SET blah = blah WHERE blah = blah");
makeUpdate("UPDATE blah SET blah = blah WHERE blah = blah");

Example of code that fixes freezing. 修复冻结的代码示例。

new Thread(new Runnable(){
public void run(){
    makeUpdate("UPDATE blah SET blah = blah WHERE blah = blah");
    makeUpdate("UPDATE blah SET blah = blah WHERE blah = blah");
    makeUpdate("UPDATE blah SET blah = blah WHERE blah = blah");
    }
}).start();

I don't think it's a duct tape fix, threads are the right way of dealing with asynchronous tasks. 我不认为这是解决问题的方法,线程是处理异步任务的正确方法。

Having said that, you should take a look at the Executor interface, which gives you a less hands-on way of creating and managing background tasks. 话虽如此,您应该看一下Executor界面,它为您创建和管理后台任务提供了一种不太实际的方法。 (It will still be backed by threads but it gives you a lot of common functionality out of the box.) (它仍将由线程支持,但它为您提供了许多常用功能。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM