Android Room library. Cannot access database on the main thread

I have a SQLite local database with implemented Room library. Local database creates when i complilate project.

Tried to build project and got this:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dblearning.dbroom_test/com.dblearning.dbroom_test.RecyclerActivity}: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.                              at                                                                                       at                                                                                        at                                                                            at                                                                 at                                                                           at$H.handleMessage(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loopOnce(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)
Caused by: java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
    at com.dblearning.dbroom_test.DAO_Impl.getAll(
    at com.dblearning.dbroom_test.RecyclerActivity.onCreate(

Here is my Activity class:

public class RecyclerActivity extends AppCompatActivity  {
    private Button add,del,update;
    private EditText dataField;
    List<DataEntity>  dataEntityList;

    protected void onCreate(Bundle savedInstanceState) {
        RoomDB db = Room.databaseBuilder(this,
                RoomDB.class, "tab").build();
        DAO dao = db.Dao();
        dataEntityList =dao.getAll();
        RecyclerView recyclerView = findViewById(;
        Adapter entityAdapter = new Adapter(dataEntityList);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

Here is my DAO class:

public interface DAO {

    @Query("SELECT * FROM tab")
    List<DataEntity> getAll();

    @Query("SELECT * FROM tab WHERE id IN (:userIds)")
    List<DataEntity> loadAllByIds(int[] userIds);
    @Query("DELETE FROM tab")
    void deleteAll();

    @Query("SELECT * FROM tab WHERE first_item LIKE :first AND " + "second_item LIKE :last LIMIT 1")
    DataEntity findByName(String first, String last);

    void update(DataEntity entity);

    void insertAll(DataEntity... Entities);
    void delete(DataEntity Entity);

And here is my Entity class:

@Entity(  tableName ="tab" )
public class DataEntity {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    public int uid;

    public int getUid() {
        return uid;

    public void setUid(int uid) {
        this.uid = uid;

    @ColumnInfo(name = "first_item")
    public String firstItem;

    public String getFirstItem() {
        return firstItem;

    public void setFirstItem(String firstItem) {
        this.firstItem = firstItem;

    @ColumnInfo(name = "second_item")
    public String secondItem;

    public String getSecondItem() {
        return secondItem;

    public void setSecondItem(String secondItem) {
        this.secondItem = secondItem;
    public DataEntity(){



Excpected to create an empty database for future population by GUI. Got an error while trying to compilate my project. Tryied to google solvation and search it here by error message, but found only for kotlin.


  • You can find the solution in the codelab: – dev.bmax

    You need to define your logic inside the ViewModel which will handle all you operation related to DB and also performs that on IO Thread. – Chirag Thummar

    I added a ViewModel and Repository and use it in my activity. My app was built. And CRUD operations now is working correctly. i was followed these steps and created a ViewModel with Repository. After adding ViewModel with Repository classes and adapting that for my project.