try {
b = dao.lock(this, "key");
b = dao.lock(this, "key");
- dao.unlock(new Object(), "key");
- b = dao.lock(this, "key");
+ b = dao.lock(new Object(), "key");
dao.unlock(this, "key");
+ b = dao.lock(new Object(), "key");
b = dao.lock(this, "key");
b = dao.lock(this, "key");
b = dao.lock(this, "key");
+ dao.unlock(new Object(), "key");
} catch (Exception e) {
e.printStackTrace();
package com.yuji.ef.dao;
import java.util.ArrayList;
+import java.util.Calendar;
import java.util.List;
import com.yuji.ef.utility.Debug;
import android.database.sqlite.SQLiteStatement;
public class LockDao implements IDao<Lock> {
+ private static final int DEFAULT_PERIOD = 60 * 60; // 1時間
+
private static IDao<Lock> instance = null;
private SQLiteStatement insertStmt = null;
private SQLiteStatement deleteIdStmt = null;
+ private SQLiteStatement deleteUpdatedStmt = null;
public static IDao<Lock> getInstance() {
if (instance == null) {
// + android.provider.BaseColumns._ID + " = ?");
deleteIdStmt = db.compileStatement("DELETE FROM LOCK WHERE "
+ "KEY = ? AND CLASS = ?");
- }
+ deleteUpdatedStmt = db.compileStatement("DELETE FROM LOCK WHERE "
+ + "KEY = ? AND UPDATED < ?");
+
+ }
@Override
public void start(SQLiteDatabase db) {
long id = -1;
db.beginTransaction();
try {
+ Calendar cal = Calendar.getInstance();
+ long updated = cal.getTimeInMillis();
+ updated -= DEFAULT_PERIOD * 1000;
+
+ deleteUpdatedNT(lock.getKey(), updated);
deleteNT(lock.getKey(), lock.getClazz());
id = addNT(lock);
db.setTransactionSuccessful();
return id;
}
+ public long deleteUpdatedNT(String key, long updated) {
+ long id = -1; // TODO
+ int i = 1;
+
+ SQLiteStatement stmt = deleteUpdatedStmt;
+ stmt.bindString(i++, key);
+ stmt.bindLong(i++, updated);
+ stmt.execute();
+ return id;
+ }
+
private String getClazz(Object obj) {
return obj.getClass().getName();
}
public boolean lock(Object obj, String key) {
+ Calendar cal = Calendar.getInstance();
+ long updated = cal.getTimeInMillis();
+
Lock lock = new Lock();
lock.setKey(key);
- lock.setClazz(getClazz(obj));
- // lock.setUpdated(updated); // TODO
+ lock.setClazz(getClazz(obj));
+ lock.setUpdated(updated);
if (this.add(lock) > 0) {
return true;
} else {