There is synchronization with the server. Runs a couple of threads that request data and write each to their own table using transactions. Is it possible to ensure the synchronization of all synchronization using internal transactions or in another way?

A simple example :

new AsyncTask<Void,Void,Void>(){ @Override protected void onPreExecute() { super.onPreExecute(); db.beginTransactionNonExclusive(); } @Override protected Void doInBackground(Void... params) { db.beginTransactionNonExclusive(); try { ... db.setTransactionSuccessful(); } finally { db.endTransaction(); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); db.setTransactionSuccessful(); db.endTransaction(); } }.execute(); 

Bd is locked and on the second db.beginTransactionNonExclusive() (if there is no transaction, then on data insertion) the flow stops. Connect to the base of one. yieldIfContendedSafely() seems to be helping, but it returns false.

    1 answer 1

    If I understood correctly, then you are interested in the thing called "Critical sections". Also, like you, I first encountered this when using multiple threads that operated with the same code in working with the database. JAVA provides a solution in the form of a synchronized method. If this is what you need, then I think it will not be enough to google)

    • With synchronization is familiar and apparently did not catch your thought. Something is needed: 1. Open a transaction. 2. Run the streams. 3. If the threads have worked without errors, then close the transaction, otherwise roll back everything at all. - Nakrul