java将这个线程设置工作或我可以做错什么

时间:2011-11-23 01:48:56

标签: java multithreading sqlite

我有点不确定,必须得到建议 我有:

public class MyApp extends JFrame{

从那里我做;

MyServer = new MyServer (this);
MyServer.execute();

MyServer是:

public class MyServer extends SwingWorker<String, Object> {   

MyServer正在listen_socket.accept()

中进行doInBackground()

并在连接时创建一个新的

class Connection implements Runnable {

我有一个单身的belove DbHelper

它拥有Sqlite连接。我在上面的MyApp中发起了它 并将引用一直传递给我的runnable:

class Connection implements Runnable {

我的问题是,如果有两个同时read或“写”,会发生什么? 我的想法是,单身人士的所有方法都是同步的 将所有调用放入队列等待锁定同步方法。

这会起作用还是我可以改变什么?

public final class DbHelper {

    private boolean initalized = false;
    private String HomePath = "";
    private File DBFile;

    private static final String SYSTEM_TABLE = "systemtable";   
    Connection con = null;
    private Statement stmt;
    private static final DbHelper instance = new DbHelper ();

    public static DbHelper getInstance() {

        return instance;

    }

    private DbHelper () {

        if (!initalized)
        {
            initDB();

            initalized = true;
        }
    }

    private void initDB()
    {
        DBFile = locateDBFile();
        try {

            Class.forName("org.sqlite.JDBC");

            // create a database connection
            con = DriverManager.getConnection("jdbc:sqlite:J:/workspace/workComputer/user_ptpp");

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private File locateDBFile()
    {
        File f = null;
        try{
            HomePath = System.getProperty("user.dir");
            System.out.println("HomePath: " + HomePath);
            f = new File(HomePath + "/user_ptpp");
            if (f.canRead())
                return f;
            else
            {
                boolean success = f.createNewFile();
                if (success) {
                    System.out.println("File did not exist and was created " + HomePath);
                    // File did not exist and was created
                } else {
                    System.out.println("File already exists " + HomePath);

                    // File already exists

                }
            }
        } catch (IOException e) {
             System.out.println("Maybe try a new directory. " + HomePath);
            //Maybe try a new directory.
        }
        return f;
    }

    public String getHomePath()
    {
        return HomePath;
    }



    public synchronized String getSelectedSystemTableColumn( String column) {

        String query = "select "+ column + " from " + SYSTEM_TABLE ;
        try {
            stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {

                String value = rs.getString(column);

                if(value == null || value == "")
                    return "";
                else
                    return value;
            }

        } catch (SQLException e ) {
            e.printStackTrace();
            return "";
        } finally {

        }
        return "";

    }

}

1 个答案:

答案 0 :(得分:1)

根据规范

java.sql中的类必须是线程安全的

http://docs.oracle.com/javase/1.3/docs/guide/jdbc/spec/jdbc-spec.frame9.html

  

我们要求对所有java.sql对象进行所有操作   多线程安全,能够正确处理几个   线程同时调用同一个对象。一些司机可能允许   比其他人更多的并发执行。开发人员可以充分考虑   并发执行;如果司机需要某种形式的   同步,它将提供它。可见的唯一区别   开发人员将会减少运行应用程序   并发性。

这不太可能是一个好主意。你应该重新考虑这个设计。我认为汇集连接并在最窄范围内关闭它们将是一个更好的主意。