Android - 绿道多次交易

Android - Green Dao Multiple Transaction

我在 greendao 中有多个事务的问题 例如,我已经有两个 table 和 type_book.

我已经有这样的代码了:

    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();
    }

我想要如果交易类型簿失败插入,交易簿主回滚我的意思是取消插入数据簿主。如何进行这样的交易?

任何想法、参考和示例都非常有帮助谢谢。

我想你误解了原理。尝试创建数据库,启动事务,用 daoMaster 包装数据库,使用会话插入,然后设置事务成功。看代码。

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();
}