Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] about nest transaction? #146

Closed
Guang1234567 opened this issue Jan 14, 2019 · 4 comments
Closed

[Question] about nest transaction? #146

Guang1234567 opened this issue Jan 14, 2019 · 4 comments

Comments

@Guang1234567
Copy link

Guang1234567 commented Jan 14, 2019

Hello dev:

I have a code that get into trouble about nest transaction like below. Could you give me some suggestion?

class ClassroomProvider {
    Future<Classroom> insert(Classroom room) async {
        return database.transaction((txn) async {
                 room.id = await db.insert(tableClassroom, room.toMap());
                 await _teacherProvider.insert(room.getTeacher());
                 await _studentProvider.bulkInsert(room.getStudents()); // nest transaction here
                  return room;
               }
            });
  }
}

class TeacherProvider {
    Future<Teacher> insert(Teacher teacher) async {
    teacher.id = await db.insert(tableTeacher, teacher.toMap());
    return teacher;
  }
}

class StudentProvider {
    Future<List<Student>> bulkInsert(List<Student> students) async {
           // use database object in a transaction here !!!
           return database.transaction((txn) async {
                  for(var s in students) {
                              s.id = await db.insert(tableStudent, student.toMap());
                  }
                  return students;
               }
            });
}

As we known in README:

Don't use the database but only use the Transaction object in a transaction to access the database

But the above code use the database object in a transaction to access the database 😭 !!!

In additional, has any unit test or example for nest transaction?

@Guang1234567 Guang1234567 changed the title [Question] about nest Transaction? [Question] about nest transaction? Jan 14, 2019
@alextekartik
Copy link
Contributor

Nested transaction were indeed supported but it needed expensive zones in all situation. One solution is to pass the transaction object

class ClassroomProvider {
  Future<Classroom> insert(Classroom room) async {
    return database.transaction((txn) async {
      await _teacherProvider.txnInsert(txn, room.getTeacher());
      await _studentProvider.txnBulkInsert(
          txn, room.getStudents()); // nest transaction here
      // Insert room last to save the teacher and students ids
      room.id = await txn.insert(tableClassroom, room.toMap());
      return room;
    });
  }
}

class TeacherProvider {
  Future<Teacher> insert(Teacher teacher) =>
      database.transaction((txn) => txnInsert(txn, teacher));

  Future<Teacher> txnInsert(Transaction txn, Teacher teacher) async {
    teacher.id = await txn.insert(tableTeacher, teacher.toMap());
    return teacher;
  }
}

class StudentProvider {
  Future<List<Student>> bulkInsert(List<Student> students) =>
      database.transaction((txn) => txnBulkInsert(txn, students));

  Future<List<Student>> txnBulkInsert(
      Transaction txn, List<Student> students) async {
    for (var student in students) {
      student.id = await txn.insert(tableStudent, student.toMap());
    }
    return students;
  }
}

@Guang1234567
Copy link
Author

Guang1234567 commented Jan 14, 2019

@alextekartik
Thanks your reply.

In additional, has any unit test or example for nest transaction?

like this?

database.transaction((txn1) async {
                await database.transaction((txn2) async {
                               });
            });

@alextekartik
Copy link
Contributor

Nested transaction are not supported. SQLite does not support it. Inside a transaction you should not used the database object.

@Guang1234567
Copy link
Author

Guang1234567 commented Jan 16, 2019

@alextekartik

Thanks your reply. You can close this issue now.

@Other_people_that_prefer_nested_transaction:

sqlfiltebrite is my flutter pkg that base on tekartik/sqflite and rxdart.

And it support a simulation of the nest transaction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants