여러 개의 작은 데이터베이스에 데이터를 저장해야하는 프로젝트에서 작업하고 있습니다. 이식성을 위해 SQLite를 사용하고 있습니다. 데이터베이스는 여러 스레드에 의해 액세스됩니다. 문제는 언젠가 하나 이상의 작업이 이미 해당 데이터베이스에서 진행 중이기 때문에 데이터가 손실되는 경우가 있습니다. 여러 데이터베이스에 액세스하려면 다음 코드를 사용하고 있습니다.Java 데이터 손실을 방지하기 위해 Java에서 SQLITE 용 쿼리 대기열을 만드는 방법은 무엇입니까?
package db;
/**
*
* @author Nika
*/
import java.sql.*;
public class SQLiteJDBC {
Connection c = null;
Statement stmt = null;
ResultSet rs = null;
public SQLiteJDBC() {
}
public void closeConnection() throws SQLException {
stmt.close();
c.close();
}
public void createtable(String db, String sql) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
//System.out.println("Opened database successfully");
stmt = c.createStatement();
stmt.executeUpdate(sql);
// stmt.close();
// c.close();
System.out.println(sql);
System.out.println("Table created successfully");
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
public void insert(String db, String sql) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
// System.out.println("Opened database successfully");
stmt = c.createStatement();
stmt.executeUpdate(sql);
// stmt.close();
c.commit();
// c.close();
System.out.println(sql);
System.out.println("Records created successfully");
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
public ResultSet select(String db, String sql) throws SQLException {
ResultSet rs2=null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
// System.out.println("Opened database successfully");
stmt = c.createStatement();
rs2 = stmt.executeQuery(sql);
System.out.println(sql);
System.out.println("Operation done successfully");
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
return rs2;
}
public void Update(String db, String sql) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
// System.out.println("Opened database successfully");
stmt = c.createStatement();
stmt.executeUpdate(sql);
c.commit();
// stmt.close();
// c.close();
System.out.println(sql);
System.out.println("Operation done successfully");
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
public void delete(String db, String sql) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
// System.out.println("Opened database successfully");
stmt = c.createStatement();
stmt.executeUpdate(sql);
c.commit();
// stmt.close();
// c.close();
System.out.println(sql);
System.out.println("Operation done successfully");
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
public void execute(String db, String sql) {
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + db);
c.setAutoCommit(false);
// System.out.println("Opened database successfully");
stmt = c.createStatement();
stmt.executeUpdate(sql);
c.commit();
// stmt.close();
// c.close();
System.out.println(sql);
System.out.println("Operation done successfully");
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
public static void main(String args[]) {
SQLiteJDBC sqLiteJDBC = new SQLiteJDBC();
}
}
각 메소드에는 두 개의 매개 변수가 있으며 첫 번째 db 파일 위치와 두 번째 매개 변수는 sql 쿼리입니다. 어떻게하면 데이터 손실을 막을 수 있도록 동일한 db 파일에 액세스하는 스레드에 대한 대기열 메커니즘을 구현할 수 있습니까?
편집 :
조건이 true가 될 때마다 다른 스레드 Handler()
하여 액세스 하나 개의 스레드 UpdateAfterExecution(args[])
있습니다. 이제는 너무 많은 클라이언트가 처리기에 요청을 보내고 하나 이상의 조건을 만족한다면 UpdateAfterExecution
은 동일한 데이터베이스에 액세스하기 위해 이 어쨌든 UpdateAfterExecution
의 실행을 제한하기 위해 스레드를 실행하고 ThreadPool
을 사용하여 스레드 풀 객체를 정적으로해야합니까?
데이터 손실로 무엇을 의미합니까? 일반적으로 – Vipin
은 삽입, 업데이트, 삭제 및 선택 메소드를 동기화합니다. 한 스레드가 읽기/쓰기 인 경우 다른 스레드가 대기합니다. – Vipin
@Vipin 스레드 풀로 나를 도울 수 있습니까 ?? – DeepSidhu1313