0

I have some problem for multiple transaction in greendao example i already have two table with name book and type_book.

i already have code like this :

    SQLiteDatabase db = bookMasterDao.getDatabase();
    db.beginTransaction();

    try {
        bookMasterDao.insert(bookMaster);
        idBook = bookMaster.getId().intValue();
        db.setTransactionSuccessful();
    } catch (Exception ex) {
        System.out.println("Error insert book master " + ex);
    } finally {
        db.endTransaction();
    }



    SQLiteDatabase dbTypeBook = typeBookMasterDao.getDatabase();
    dbTypeBook.beginTransaction();

    try {
        typeBookMasterDao.insert(bookMaster);
        dbTypeBook.setTransactionSuccessful();
    } catch (Exception ex) {
        System.out.println("Error insert type book" + ex);
    } finally {
        dbTypeBook.endTransaction();
    }

I want if transaction type book failure insert , transaction book master rollback i mean cancel insert data book master. How to make transaction like that?

any idea, reference and example very help thank you.

Anonim
  • 61
  • 1
  • 13

1 Answers1

3

I think you misunderstood the principe. Try create db, start transaction, wrap the databse with daoMaster, use session for inserting, then set transaction successfull. Look at the code.

SQLiteDatabase db = openOrCreateDatabase("db", MODE_PRIVATE, null);
DaoMaster.createAllTables(db, true);

 db.beginTransaction();
 try {

  DaoMaster daoMaster = new DaoMaster(db);
  DaoSession daoSession = daoMaster.newSession();

  Book book = new Book();
  Book2 book2 = new Book2();

  //insert on specific session
  daoSession.getBookDao().insert(book);

  //set some relation to inserted book
  book2.setBook(book);

  //if this fails, book and book2 won't be in db
  //insert withou specific session
  daoSession.insert(book2); 

  db.setTransactionSuccessful();
} catch (Exception ex) {
    ex.printStackTrace();
} finally {
    db.endTransaction();
    db.close();
}
l0v3
  • 963
  • 7
  • 26
  • why create database again ? dao generator have file DaoMaster and in there file already create table, now in fragment i want code like that but openOrCreateDatabase cannot resolve in fragment. – Anonim Sep 11 '15 at 02:40
  • It's not creating again, it creates tables only if not exists. Just for sure you have right db schema. And yes, you can't use openOrCreateDatabase(), but you shouldn't. Better practise is to let context (activity/application) hold your database reference. Anyway, try getActivity().openOrCreateDatabase() from your fragment. This should work even if i not recommend it. – l0v3 Sep 11 '15 at 18:22
  • thank's i understand now i want test,but i confused how to send reference database from activity to fragment, and which way you very recommend and safe. – Anonim Sep 15 '15 at 10:56
  • ahh i success make it, in fragment just change SQLiteDatabase db = openOrCreateDatabase("db", MODE_PRIVATE, null); DaoMaster.createAllTables(db, true); TO SQLiteDatabase db = daoSession.getDatabase(); and Remove db.close from your code and it's working for me, thanks to help me for misunderstood the principe.. have a good day !! – Anonim Sep 16 '15 at 05:16
  • Thanks, this gave me the hint I needed! :) – Tobias Reich Mar 17 '16 at 14:47