简体   繁体   中英

Java How to Create a query queue for SQLITE in Java to prevent dataloss?

I am working on project which requires data to be saved on multiple small databases, i'm using SQLite for the portability reasons. the databases are accessed by multiple threads. The problem is sometime data get lost because one or more operation is already going on that database. I'm using the following code to access multiple databases.

     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();
}
}

Where each method has two parameters first one db files location and second one is sql query. how can i implement any queue mechanism for threads who are accessing same db file so that data loss can be prevented ?

EDIT:

There is one thread UpdateAfterExecution(args[]) which is accessed by another thread Handler() whenever a condition become true. Now if too many clients send request to handler and satisfy the condition more than one UpdateAfterExecution get invoked and running which access same DB, is there anyway to limit the execution of UpdateAfterExecution Thread using ThreadPool and do i have to make that threadpool object static?

The need for transactions in sql access becomes even more needed when using multiple threads. You might want to start a transaction with the first thread to access the database with a given query. Once all queries of similar nature have made changes, you can end the transaction. Savepoints can also help if you want to keep track of changes and not rollback to the last commit.

ThreadPool solved my problem. thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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