Frage

Here is my model classes that are persisted in sqlite db using ORMLITE.

public class Site{
                 ...
                 ...
                 Collection<Visit> visits;
                 }

public class Visit{
                 ...
                 ...
                 Collection<Pic> pics;
                 }

public class Pic{
                 ...
                 ...

                 }

Now in one view i add,edit or delete from these three tables. There is a button in my view to cancel the changes(add,edit,delete). So i need to rollback to previous state of the db.

How can i achieve this roll back to a certain state of the three tables using ormlite with android?

I have read in ormlite docs about DAO.SetAutoCommit() and startThreadConnection() methods. I think i can do it by using this two. But cant figure it out how to use them. Please suggest me how to do it.

Here is my DBHelper Class:

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// name of the database file 
private static final String DATABASE_NAME = "Test.db";


private static final int DATABASE_VERSION = 1;

private Dao<Site, Integer> siteListDao = null;
private Dao<Visit, Integer> visitDao= null;
private Dao<Pic,Integer> picDao=null;

    public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) {
    try {
        TableUtils.createTable(connectionSource, Site.class);
        TableUtils.createTable(connectionSource, Visit.class);
        TableUtils.createTable(connectionSource, Pic.class);
                 } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
        throw new RuntimeException(e);
    } catch (java.sql.SQLException e) {
        e.printStackTrace();
    }

}

    public Dao<Visit, Integer> getVisitDao() {
    if (null == visitDao) {
        try {
            visitDao = getDao(Visit.class);
        }catch (java.sql.SQLException e) {
            e.printStackTrace();
        }
    }
    return visitDao;
}
    .....
     .....
War es hilfreich?

Lösung 2

Yeah I mostly agree with @Stefan. I think you need to do this inside of your application and not rely on a database construct to all you to return to a previous state.

Turning off auto-commit (which ORMLite uses transactions under Sqlite) or using transactions directly is not how to do this. Transactions are designed to allow you to make multiple changes to the database and then roll them all back if one database change fails. They are not intended to stay open for seconds waiting for user input.

  • You could have a committed boolean field or some such in your data to make it easier to "revert" the database by deleting all objects where the committed = false or some such.
  • You could have separate tables so objects are stored to a session table and then copied over to the main tables when the user commits their work.

Andere Tipps

Hmm don't know about any methods that will do this for you automatically. If you really wanna do it automatically, you could also check out Transactions. They have build-in controls to roll-back on fail. Not exactly what you want, but maybe adjustable.

You could also just do it more manually and save 1 (last) object in memory, the object you're going to delete. If a person wants to 'undo' the change, you can just re-add that object with ORMLite. Probably a lot easier than other options.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top