Я немного неуверен и должен получить совет.
У меня есть:
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 {
У меня есть любимый DbHelper
это синглтон
Содержит соединение Sqlite.Я инициирую его в приведенном выше MyApp
и полностью передаю ссылки в мой runnable:
class Connection implements Runnable {
Мой вопрос: что произойдет, если будет два одновременных read
или `write?
Здесь я думал, что все методы в синглтоне синхронизированы, и
поместит все вызовы в очередь в ожидании блокировки синхронизированного метода.
Будет ли это работать или что я могу изменить?
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 "";
}
}