Gentlemen, who can clarify the question? In my application I use CursorLoader in conjunction with LoaderManager. I do this solely to obtain data from the database. And I was puzzled by the question of how to ensure the thread-safety of this bundle. Everything is clear with loadermanager, but not so with cursorloader.
Conducted an experiment, in the method that performs data retrieval from the database after db.query, but before the return cursor made a delay of one second, and in activity I call the loadermanager two times in a row. The application worked fine, nothing crashed and the second request returned as an answer. But at the same time I have never used sinchronized. As I understand it, when using SQLiteDatabase everything will be fine. it is thread safe and both requests execute sequentially. It seems that everything is clear, but nothing is clear. How does SQLiteDatabase provide thread safety? Here is the code that implements the data request from the database.

public Cursor getEmployees() { SQLiteDatabase db = getReadableDatabase(); String sqlTables = "Test"; String SELECTION_DB = COLUMN_NAME_STATION + " LIKE " + "'" + searchName + "%'"; Cursor cursor = db.query(sqlTables, null, SELECTION_DB, null, null, null, null); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } cursor.moveToFirst(); return cursor; } 

Code that is written in a class inherited from CursorLoader

 protected Cursor onLoadInBackground() { DbOfStationsSearch db = new DbOfStationsSearch(getContext(), bundle.getString("upd")); Cursor cursor = db.getEmployees(); return cursor; } 

The activity says the following

  getSupportLoaderManager().restartLoader(0, bundle, MainActivity.this); getSupportLoaderManager().restartLoader(0, bundle, MainActivity.this); 
  • one
    SQLiteDatabase does not provide thread safety. Calling CursorLoader (which is inherited from AsyncTaskLoader ) executes queries asynchronously. - tim_taller
  • This is the question. After all, it turns out that I am accessing the same database from different threads and are not synchronizing any method. At the same time, the application does not fall. why? - Garic

1 answer 1

SQLite is a complete relational database. It is designed to work with multiple users simultaneously . In this case, different flows act as users, this is not important. That is, from the code side it is not required to provide some thread safety, the database itself is thread safe.