title: Transactions with GreenDao
body: <p>I made this post because I don't understand how transaction works using GreenDao and I would love to get some explanation/recent tutorial. </p>
  
  <p>Despite of reading the <a href="https://github.com/greenrobot/greenDAO/blob/master/examples/RxDaoExample/src/main/java/org/greenrobot/greendao/rxexample/MainActivity.java" rel="nofollow noreferrer">Greendao github</a>, I don't understand how the main activity works. For example:</p>
  
  <pre><code>...
      Note note = new Note(null, noteText, comment, new Date(), NoteType.TEXT);
      noteDao.insert(note)
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Action1&lt;Note&gt;() {
                  @Override
                  public void call(Note note) {
                      Log.d("DaoExample", "Inserted new note, ID: " + note.getId());
                      updateNotes();
                  }
              });
  ...
  </code></pre>
  
  <p>Do the <em>observeOn</em> method sends every insertion/update to one limited buffer? so, it will manage automatically the insertion to the real database when every operation was finished whithout problems?  </p>
  
  <p><a href="http://greenrobot.org/files/greendao/javadoc/3.1/org/greenrobot/greendao/AbstractDaoSession.html#rxTx--" rel="nofollow noreferrer">Reading its API</a>, we could see that they use <em>rxTx</em> and <em>rxTxPlain</em> for transactions (other way?), and <a href="https://github.com/greenrobot/greenDAO/blob/master/tests/DaoTest/src/androidTest/java/org/greenrobot/greendao/daotest/DaoSessionConcurrentTest.java" rel="nofollow noreferrer">finding out where are performed</a>, I have to say I misunderstood everything. For example:</p>
  
  <pre><code>...
      public void testConcurrentInsertDuringTx() throws InterruptedException {
  
          ...
  
          Runnable runnable2 = new Runnable() {
              @Override
              public void run() {
                  dao.insertInTx(createEntity(null));
              }
          };
          Runnable runnable3 = new Runnable() {
              @Override
              public void run() {
                  daoSession.runInTx(new Runnable() {
                      @Override
                      public void run() {
                          dao.insert(createEntity(null));
                      }
                  });
              }
          };
  
          ...           
  
          // Builds the statement so it is ready immediately in the thread
          dao.insert(createEntity(null));
          doTx(new Runnable() {
              @Override
              public void run() {
                  dao.insert(createEntity(null));
              }
          });
          latchThreadsDone.await();
          assertEquals(7, dao.count());
      }
  ...
  </code></pre>
  
  <p>I know this is an example, <strong>but I don't know the diferences of using the rx methods and the observeOn/subscribe ones</strong></p>
  
  <pre><code>protected void doTx(final Runnable runnableInsideTx) {
      daoSession.runInTx(new Runnable() {
          @Override
          public void run() {
              latchInsideTx.countDown();
              // Give the concurrent thread time so it will try to acquire locks
              try {
                  Thread.sleep(TIME_TO_WAIT_FOR_THREAD);
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
              runnableInsideTx.run();
          }
      });
  }
  </code></pre>
  
  <p>I read some topics about transactions, but they are obsoletes or maybe, I didn't understand them correctly:</p>
  
  <p><a href="https://stackoverflow.com/questions/29227054/sqlite-transaction-behaviour-using-greendao">SQLite transaction behaviour using greendao</a></p>
  
  <p><a href="https://stackoverflow.com/questions/32412573/android-green-dao-multiple-transaction">Android - Green Dao Multiple Transaction</a></p>
  
  <p><a href="https://stackoverflow.com/questions/25189764/transactions-in-greendao">Transactions in GreenDao</a></p>
  
  <p><a href="https://stackoverflow.com/questions/12748899/greendao-40-seconds-to-insert-600-records">greenDAO 40 seconds to insert 600 records</a></p>
  
  <p><a href="https://groups.google.com/forum/#!topic/greendao/PXRwCRTbP6c" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/greendao/PXRwCRTbP6c</a></p>
  
  <p>Someone could help me giving me some explanation/recent tutorial? Greendao have these methods as experimentals. Thanks in advance</p>