Java Code Examples for android.database.sqlite.SQLiteDatabase.beginTransactionNonExclusive()

The following are Jave code examples for showing how to use beginTransactionNonExclusive() of the android.database.sqlite.SQLiteDatabase class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 7 votes
public void clearLog(int uid) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            if (uid < 0)
                db.delete("log", null, new String[]{});
            else
                db.delete("log", "uid = ?", new String[]{Integer.toString(uid)});

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

        db.execSQL("VACUUM");
    } finally {
        lock.writeLock().unlock();
    }

    notifyLogChanged();
}
 
Example 2
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void cleanupLog(long time) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            // There an index on time
            int rows = db.delete("log", "time < ?", new String[]{Long.toString(time)});
            Log.i(TAG, "Cleanup log" +
                    " before=" + SimpleDateFormat.getDateTimeInstance().format(new Date(time)) +
                    " rows=" + rows);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example 3
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void setAccess(long id, int block) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            ContentValues cv = new ContentValues();
            cv.put("block", block);
            cv.put("allowed", -1);

            if (db.update("access", cv, "ID = ?", new String[]{Long.toString(id)}) != 1)
                Log.e(TAG, "Set access failed");

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyAccessChanged();
}
 
Example 4
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void clearAccess() {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            db.delete("access", null, null);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyAccessChanged();
}
 
Example 5
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void clearAccess(int uid, boolean keeprules) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            // There is a segmented index on uid
            // There is an index on block
            if (keeprules)
                db.delete("access", "uid = ? AND block < 0", new String[]{Integer.toString(uid)});
            else
                db.delete("access", "uid = ?", new String[]{Integer.toString(uid)});

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyAccessChanged();
}
 
Example 6
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void resetUsage(int uid) {
    lock.writeLock().lock();
    try {
        // There is a segmented index on uid
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            ContentValues cv = new ContentValues();
            cv.putNull("sent");
            cv.putNull("received");
            cv.putNull("connections");
            db.update("access", cv,
                    (uid < 0 ? null : "uid = ?"),
                    (uid < 0 ? null : new String[]{Integer.toString(uid)}));

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyAccessChanged();
}
 
Example 7
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void cleanupDns() {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            // There is no index on time for write performance
            long now = new Date().getTime();
            db.execSQL("DELETE FROM dns WHERE time + ttl < " + now);
            Log.i(TAG, "Cleanup DNS");

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example 8
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void clearDns() {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            db.delete("dns", null, new String[]{});

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example 9
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void addForward(int protocol, int dport, String raddr, int rport, int ruid) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            ContentValues cv = new ContentValues();
            cv.put("protocol", protocol);
            cv.put("dport", dport);
            cv.put("raddr", raddr);
            cv.put("rport", rport);
            cv.put("ruid", ruid);

            if (db.insert("forward", null, cv) < 0)
                Log.e(TAG, "Insert forward failed");

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyForwardChanged();
}
 
Example 10
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void deleteForward() {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            db.delete("forward", null, null);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyForwardChanged();
}
 
Example 11
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 6 votes
public void deleteForward(int protocol, int dport) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            db.delete("forward", "protocol = ? AND dport = ?",
                    new String[]{Integer.toString(protocol), Integer.toString(dport)});

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyForwardChanged();
}
 
Example 12
Project: FuelUp   File: VehicleProvider.java   View Source Code Vote up 6 votes
private int deleteVehicleWithAllData(final SQLiteDatabase db, Uri uri) {
    String[] vehicleId = new String[] { String.valueOf(ContentUris.parseId(uri)) };

    db.beginTransactionNonExclusive();

    db.delete(ExpenseEntry.TABLE_NAME, ExpenseEntry.COLUMN_VEHICLE + "=?", vehicleId);
    db.delete(FillUpEntry.TABLE_NAME, FillUpEntry.COLUMN_VEHICLE + "=?", vehicleId);
    int result = db.delete(VehicleEntry.TABLE_NAME, VehicleEntry._ID + "=?", vehicleId);

    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();

    getContext().getContentResolver().notifyChange(uri, null);
    return result;
}
 
Example 13
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 5 votes
public boolean insertDns(ResourceRecord rr) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            int ttl = rr.TTL;

            int min = Integer.parseInt(prefs.getString("ttl", "259200"));
            if (ttl < min)
                ttl = min;

            ContentValues cv = new ContentValues();
            cv.put("time", rr.Time);
            cv.put("ttl", ttl * 1000L);

            int rows = db.update("dns", cv, "qname = ? AND aname = ? AND resource = ?",
                    new String[]{rr.QName, rr.AName, rr.Resource});

            if (rows == 0) {
                cv.put("qname", rr.QName);
                cv.put("aname", rr.AName);
                cv.put("resource", rr.Resource);

                if (db.insert("dns", null, cv) == -1)
                    Log.e(TAG, "Insert dns failed");
            } else if (rows != 1)
                Log.e(TAG, "Update dns failed rows=" + rows);

            db.setTransactionSuccessful();

            return (rows == 0);
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }
}
 
Example 14
Project: aos-MediaLib   File: VideoProvider.java   View Source Code Vote up 5 votes
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
    if (DBG) Log.d(TAG, "bulkInsert " + uri);
    int match = URI_MATCHER.match(uri);

    // let ScraperProvider handle that
    if (ScraperProvider.handles(match))
        return mScraperProvider.bulkInsert(uri, values);

    if (match != -1) {
        int result = 0;
        mVobHandler.onBeginTransaction();
        SQLiteDatabase db = mDbHolder.get();
        db.beginTransactionNonExclusive();
        try {
            int numValues = values.length;
            int yield = 100;
            for (int i = 0; i < numValues; i++) {
                insert(uri, values[i]);
                if (yield-- < 0) {
                    yield = 100;
                    db.yieldIfContendedSafely();
                }
            }
            result = numValues;
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
            mVobHandler.onEndTransaction();
        }
        if (result > 0)
            mCr.notifyChange(VideoStore.ALL_CONTENT_URI, null);
        return result;
    }
    return 0;
}
 
Example 15
Project: aos-MediaLib   File: VideoProvider.java   View Source Code Vote up 5 votes
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
        throws OperationApplicationException {
    if (DBG) Log.d(TAG, "applyBatch");
    ContentProviderResult[] result = null;
    SQLiteDatabase db = mDbHolder.get();
    mVobHandler.onBeginTransaction();
    db.beginTransactionNonExclusive();
    try {
        final int numOperations = operations.size();
        final ContentProviderResult[] results = new ContentProviderResult[numOperations];
        int yield = 100;
        for (int i = 0; i < numOperations; i++) {
            results[i] = operations.get(i).apply(this, results, i);
            if (yield-- < 0) {
                yield = 100;
                db.yieldIfContendedSafely();
            }
        }
        result = results;
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
        mVobHandler.onEndTransaction();
    }
    if (result != null) {
        mCr.notifyChange(VideoStore.ALL_CONTENT_URI, null);
        mCr.notifyChange(ScraperStore.ALL_CONTENT_URI, null);
    }
    return result;
}
 
Example 16
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 4 votes
public void insertLog(Packet packet, String dname, int connection, boolean interactive) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            ContentValues cv = new ContentValues();
            cv.put("time", packet.time);
            cv.put("version", packet.version);

            if (packet.protocol < 0)
                cv.putNull("protocol");
            else
                cv.put("protocol", packet.protocol);

            cv.put("flags", packet.flags);

            cv.put("saddr", packet.saddr);
            if (packet.sport < 0)
                cv.putNull("sport");
            else
                cv.put("sport", packet.sport);

            cv.put("daddr", packet.daddr);
            if (packet.dport < 0)
                cv.putNull("dport");
            else
                cv.put("dport", packet.dport);

            if (dname == null)
                cv.putNull("dname");
            else
                cv.put("dname", dname);

            cv.put("data", packet.data);

            if (packet.uid < 0)
                cv.putNull("uid");
            else
                cv.put("uid", packet.uid);

            cv.put("allowed", packet.allowed ? 1 : 0);

            cv.put("connection", connection);
            cv.put("interactive", interactive ? 1 : 0);

            if (db.insert("log", null, cv) == -1)
                Log.e(TAG, "Insert log failed");

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyLogChanged();
}
 
Example 17
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 4 votes
public boolean updateAccess(Packet packet, String dname, int block) {
    int rows;

    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            ContentValues cv = new ContentValues();
            cv.put("time", packet.time);
            cv.put("allowed", packet.allowed ? 1 : 0);
            if (block >= 0)
                cv.put("block", block);

            // There is a segmented index on uid, version, protocol, daddr and dport
            rows = db.update("access", cv, "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?",
                    new String[]{
                            Integer.toString(packet.uid),
                            Integer.toString(packet.version),
                            Integer.toString(packet.protocol),
                            dname == null ? packet.daddr : dname,
                            Integer.toString(packet.dport)});

            if (rows == 0) {
                cv.put("uid", packet.uid);
                cv.put("version", packet.version);
                cv.put("protocol", packet.protocol);
                cv.put("daddr", dname == null ? packet.daddr : dname);
                cv.put("dport", packet.dport);
                if (block < 0)
                    cv.put("block", block);

                if (db.insert("access", null, cv) == -1)
                    Log.e(TAG, "Insert access failed");
            } else if (rows != 1)
                Log.e(TAG, "Update access failed rows=" + rows);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyAccessChanged();
    return (rows == 0);
}
 
Example 18
Project: MKAPP   File: DatabaseHelper.java   View Source Code Vote up 4 votes
public void updateUsage(Usage usage, String dname) {
    lock.writeLock().lock();
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransactionNonExclusive();
        try {
            // There is a segmented index on uid, version, protocol, daddr and dport
            String selection = "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?";
            String[] selectionArgs = new String[]{
                    Integer.toString(usage.Uid),
                    Integer.toString(usage.Version),
                    Integer.toString(usage.Protocol),
                    dname == null ? usage.DAddr : dname,
                    Integer.toString(usage.DPort)
            };

            Cursor cursor = db.query("access", new String[]{"sent", "received", "connections"}, selection, selectionArgs, null, null, null);
            long sent = 0;
            long received = 0;
            int connections = 0;
            int colSent = cursor.getColumnIndex("sent");
            int colReceived = cursor.getColumnIndex("received");
            int colConnections = cursor.getColumnIndex("connections");
            if (cursor.moveToNext()) {
                sent = cursor.isNull(colSent) ? 0 : cursor.getLong(colSent);
                received = cursor.isNull(colReceived) ? 0 : cursor.getLong(colReceived);
                connections = cursor.isNull(colConnections) ? 0 : cursor.getInt(colConnections);
            }
            cursor.close();

            ContentValues cv = new ContentValues();
            cv.put("sent", sent + usage.Sent);
            cv.put("received", received + usage.Received);
            cv.put("connections", connections + 1);

            int rows = db.update("access", cv, selection, selectionArgs);
            if (rows != 1)
                Log.e(TAG, "Update usage failed rows=" + rows);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    } finally {
        lock.writeLock().unlock();
    }

    notifyAccessChanged();
}
 
Example 19
Project: FuelUp   File: VehicleProvider.java   View Source Code Vote up 4 votes
private int validateFillUpAndUpdate(final Uri uri, final ContentValues contentValues, long id) {

        if (contentValues.size() == 0) {
            return UPDATE_NO_CHANGE;
        }
        
        validateFillUpBasics(contentValues, true, id);
        
        if (contentValues.containsKey(FillUpEntry.COLUMN_VEHICLE)) {
            throw new IllegalArgumentException("Cannot change vehicle of FillUp. Please, create a fresh new FillUp in this case.");
        }

        boolean isDeleteAndInsertNeeded = contentValues.containsKey(FillUpEntry.COLUMN_DATE)
                || contentValues.containsKey(FillUpEntry.COLUMN_FUEL_VOLUME)
                || contentValues.containsKey(FillUpEntry.COLUMN_DISTANCE_FROM_LAST)
                || contentValues.containsKey(FillUpEntry.COLUMN_IS_FULL_FILLUP);

        if (!isDeleteAndInsertNeeded) {
            // if only Not-Neighbour-Affecting values have been changed
            final String selection = FillUpEntry._ID + "=?";
            final String[] idArgument = new String[] { String.valueOf(id) };

            getContext().getContentResolver().notifyChange(uri, null);
            return mDbHelper.getWritableDatabase().update(
                    FillUpEntry.TABLE_NAME, contentValues, selection, idArgument);
        } else {
            // if date, distance or fuelVolume have been changed, it may affect neighbouring fillUps
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            db.beginTransactionNonExclusive();

            FillUp fillUp = FillUpService.getFillUpById(id, getContext());

            ContentValues recreatedValues = new ContentValues();
            recreatedValues.putAll(contentValues);
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_VEHICLE))
                recreatedValues.put(FillUpEntry.COLUMN_VEHICLE, fillUp.getVehicle().getId());
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_DISTANCE_FROM_LAST))
                recreatedValues.put(FillUpEntry.COLUMN_DISTANCE_FROM_LAST, fillUp.getDistanceFromLastFillUp());
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_FUEL_VOLUME))
                recreatedValues.put(FillUpEntry.COLUMN_FUEL_VOLUME, fillUp.getFuelVolume().doubleValue());
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_FUEL_PRICE_PER_LITRE))
                recreatedValues.put(FillUpEntry.COLUMN_FUEL_PRICE_PER_LITRE, fillUp.getFuelPricePerLitre().doubleValue());
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_FUEL_PRICE_TOTAL))
                recreatedValues.put(FillUpEntry.COLUMN_FUEL_PRICE_TOTAL, fillUp.getFuelPriceTotal().doubleValue());
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_IS_FULL_FILLUP))
                recreatedValues.put(FillUpEntry.COLUMN_IS_FULL_FILLUP, fillUp.isFullFillUp() ? 1 : 0);
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_DATE))
                recreatedValues.put(FillUpEntry.COLUMN_DATE, fillUp.getDate().getTime());
            if (!recreatedValues.containsKey(FillUpEntry.COLUMN_INFO))
                recreatedValues.put(FillUpEntry.COLUMN_INFO, fillUp.getInfo());

            deleteFillUpInTransaction(uri, db);
            validateFillUpAndInsertInTransaction(uri, recreatedValues, db);

            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();

            return 1;
        }
    }
 
Example 20
Project: AOSP-Kayboard-7.1.2   File: MetadataDbHelper.java   View Source Code Vote up 4 votes
/**
 * Marks a downloading entry as having successfully downloaded and being installed.
 *
 * The metadata database contains information about ongoing processes, typically ongoing
 * downloads. This marks such an entry as having finished and having installed successfully,
 * so it becomes INSTALLED.
 *
 * @param db the metadata database.
 * @param r content values about the entry to mark as processed.
 */
public static void markEntryAsFinishedDownloadingAndInstalled(final SQLiteDatabase db,
        final ContentValues r) {
    switch (r.getAsInteger(TYPE_COLUMN)) {
        case TYPE_BULK:
            DebugLogUtils.l("Ended processing a wordlist");
            // Updating a bulk word list is a three-step operation:
            // - Add the new entry to the table
            // - Remove the old entry from the table
            // - Erase the old file
            // We start by gathering the names of the files we should delete.
            final List<String> filenames = new LinkedList<>();
            final Cursor c = db.query(METADATA_TABLE_NAME,
                    new String[] { LOCAL_FILENAME_COLUMN },
                    LOCALE_COLUMN + " = ? AND " +
                    WORDLISTID_COLUMN + " = ? AND " + STATUS_COLUMN + " = ?",
                    new String[] { r.getAsString(LOCALE_COLUMN),
                            r.getAsString(WORDLISTID_COLUMN),
                            Integer.toString(STATUS_INSTALLED) },
                    null, null, null);
            try {
                if (c.moveToFirst()) {
                    // There should never be more than one file, but if there are, it's a bug
                    // and we should remove them all. I think it might happen if the power of
                    // the phone is suddenly cut during an update.
                    final int filenameIndex = c.getColumnIndex(LOCAL_FILENAME_COLUMN);
                    do {
                        DebugLogUtils.l("Setting for removal", c.getString(filenameIndex));
                        filenames.add(c.getString(filenameIndex));
                    } while (c.moveToNext());
                }
            } finally {
                c.close();
            }
            r.put(STATUS_COLUMN, STATUS_INSTALLED);
            db.beginTransactionNonExclusive();
            // Delete all old entries. There should never be any stalled entries, but if
            // there are, this deletes them.
            db.delete(METADATA_TABLE_NAME,
                    WORDLISTID_COLUMN + " = ?",
                    new String[] { r.getAsString(WORDLISTID_COLUMN) });
            db.insert(METADATA_TABLE_NAME, null, r);
            db.setTransactionSuccessful();
            db.endTransaction();
            for (String filename : filenames) {
                try {
                    final File f = new File(filename);
                    f.delete();
                } catch (SecurityException e) {
                    // No permissions to delete. Um. Can't do anything.
                } // I don't think anything else can be thrown
            }
            break;
        default:
            // Unknown type: do nothing.
            break;
    }
 }