Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9072739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:14:39+00:00 2026-06-16T18:14:39+00:00

I have an SQLiteOpenHelper class that implements the singleton pattern and has three methods:

  • 0

I have an SQLiteOpenHelper class that implements the singleton pattern and has three methods: addLogs(), getLogs(), and deleteLogs(). Seven threads (triggered by a BroadcastReceiver at certain schedules) access the database–six of them only use addLogs(), and the other one uses getLogs() and calls deleteLogs() before it finishes.

My problem is, because the last thread makes two different calls to the SQLite database, the other threads can call on addLogs() even before deleteLogs() is called, which corrupts my output. How can I make that one thread treat the two calls to the SQLiteOpenHelper as a single, atomic transaction?

What I’ve tried: Create a static ReentrantLock member in the SQLiteOpenHelper, and call lock() and unlock() from every one of the seven threads before and after their transactions. The other six threads still get through even though the last one still has the lock, which it calls before getLogs() and releases after deleteLogs().

UPDATE: This is the seventh thread that accesses the two methods.

public class Uploader extends Thread {

    private Context context;

    public Uploader(Context context) {
        this.context = context;
    }

    @Override
    public void run() {
        Logger.d("STARTED: Uploader");
        DB db = DB.getInstance(context);
        List<JsonLog> logs = db.getLogs(); // FIRST CALL
        String json = new Gson().toJson(logs); // convert to JSON

        // connect to the server and upload
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(This.URL);
        post.setHeader("content-type", "application/json");
        try {
            Logger.d("to upload: " + json);
            StringEntity se = new StringEntity(json);
            post.setEntity(se);
            StatusLine status = client.execute(post).getStatusLine();
            // if upload is successful
            if (status.getStatusCode() == HttpStatus.SC_OK) {
                db.deleteLogs(); // SECOND CALL
            } else {
                Logger.d("Uploader failed. " + status.getStatusCode() + ": " + status.getReasonPhrase());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.d("FINISHED: Uploader");
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:14:41+00:00Added an answer on June 16, 2026 at 6:14 pm

    SQLite has transactions as in db.beginTransaction(), db.setTransactionSuccessful() to mark the Tx to be committed and db.endTransaction() to finally mark the Tx as to be done.

    So you could in your getLogs() do the beginTransaction and in the deleteLogs to mark it as done (and successful).

    [Update]
    Now with your code snippet you probably would want to call beginTransaction before your 1st call and in a finally block, that adds to the try-catch block call into endTransaction. At the end of the try part if the status is ok, you would call setTransactionSuccessful (after the delete call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a db that is created like this... public class DataBaseManager extends SQLiteOpenHelper{
I have an existing database based on SQLiteOpenHelper that has several versions and code
I have a database that I have created with the following handler: public class
I have an application that contains a main Activity, which has a very simple
I have made an app that has 3 activities. In the fisrt activity(Import) i
With the help of this tutorial i have made an app that has 3
hi to all i have a table that is named books and it has
I have created a database by extending SQLiteOpenHelper class. And its created also. This
In my application, I use SQLiteOpenHelper class and it has insertOrThrow method. I want
I'm running into a very frustrating bug. I have a java class that reads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.