简体   繁体   English

Java如何在Java中为SQLITE创建查询队列以防止数据丢失?

[英]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. 我正在研究需要将数据保存在多个小型数据库中的项目,出于可移植性原因,我正在使用SQLite。 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. 每个方法都有两个参数,第一个是db文件的位置,第二个是sql查询。 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. 只要条件为真,就会有一个线程UpdateAfterExecution(args[])被另一个线程Handler()访问。 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? 现在,如果有太多客户端向处理程序发送请求并满足多个调用同一数据库的UpdateAfterExecution调用并正在运行,那么是否有UpdateAfterExecution使用ThreadPool限制UpdateAfterExecution Thread的执行,我是否必须使该线程ThreadPool对象静态?

The need for transactions in sql access becomes even more needed when using multiple threads. 使用多个线程时,对SQL访问中事务的需求变得更加必要。 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. ThreadPool解决了我的问题。 thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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