I try to include transactions in my application because writing in database is very slow and I saw here and here that transactions are a solution but they are still very confusing to me.
I have Schedule
objects that contains an object LineStation
, and I want to write them in database using transactions.
Here, the method addSchedules
in my class ScheduleDAO
, that writes all schedules in database. It contains only one transaction.
public void addSchedules(ArrayList<Schedule> schedulesList) {
SQLiteDatabase db = this.dh.getWritableDatabase();
db.beginTransactionNonExclusive();
for (Schedule schedule : schedulesList) {
ContentValues values = new ContentValues();
// insert linestation
LineStationDAO.getLineStationDAO().addLineStation(schedule.getLineStation());
values.put(/*...*/);
/* ... */
db.insert(DatabaseHandler.TABLE_SCHEDULE, null, values);
}
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}
And this is, the method addLineStation
in my class LineStationDAO
that saves the object LineStation
given. It's called by addSchedules
and doesn't contain transaction because it is "nested" in the addSchedules
transaction.
public void addLineStation(LineStation lineStation) {
SQLiteDatabase db = this.dh.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(/*...*/);
/* ... */
db.insert(DatabaseHandler.TABLE_LINE_STATION, null, values); // database is locked (code 5)
db.close();
}
The LineStation
insert implies an SQLiteDatabaseLockedException
(database is locked -code 5). What I have done wrong, please? Thanks.
Aucun commentaire:
Enregistrer un commentaire