2014-07-11 4 views
0

여러 개의 작은 데이터베이스에 데이터를 저장해야하는 프로젝트에서 작업하고 있습니다. 이식성을 위해 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을 사용하여 스레드 풀 객체를 정적으로해야합니까?

+0

데이터 손실로 무엇을 의미합니까? 일반적으로 – Vipin

+0

은 삽입, 업데이트, 삭제 및 선택 메소드를 동기화합니다. 한 스레드가 읽기/쓰기 인 경우 다른 스레드가 대기합니다. – Vipin

+0

@Vipin 스레드 풀로 나를 도울 수 있습니까 ?? – DeepSidhu1313

답변

0

ThreadPool 내 문제가 해결되었습니다. 고마워요 :)

1

다중 스레드를 사용할 때 sql 액세스의 트랜잭션이 더 필요합니다. 첫 번째 스레드로 트랜잭션을 시작하여 주어진 쿼리로 데이터베이스에 액세스하고자 할 수 있습니다. 비슷한 성격의 모든 쿼리가 변경되면 트랜잭션을 종료 할 수 있습니다. 세이브 포인트는 변경 사항을 추적하고 마지막 커밋까지 롤백하지 않으려는 경우에도 도움이 될 수 있습니다.

+0

고마워요, 당신은 스레드 풀 나를 도울 수 있습니까 ?? 나는이 문제를 해결할 수 있다고 생각한다. – DeepSidhu1313

+0

Udemy는 데이터를 보호하기 위해 세마포어 (Semaphores) 사용을 포함하여 스레드에 관한 좋은 과정을 갖고있다. https://www.udemy.com/java-multithreading/?dtcode=KyCrRVF16O1W – kmac

+0

정말 도움이 되셨습니다. 링크 : – DeepSidhu1313