Example usage for android.database.sqlite SQLiteDatabase endTransaction

List of usage examples for android.database.sqlite SQLiteDatabase endTransaction

Introduction

In this page you can find the example usage for android.database.sqlite SQLiteDatabase endTransaction.

Prototype

public void endTransaction() 

Source Link

Document

End a transaction.

Usage

From source file:org.jumpmind.symmetric.android.AndroidSqlTemplate.java

public int update(final boolean autoCommit, final boolean failOnError, boolean failOnDrops,
        boolean failOnSequenceCreate, final int commitRate, final ISqlResultsListener resultsListener,
        final ISqlStatementSource source) {
    int row = 0;/*from  w w w . ja  va  2s.co m*/
    SQLiteDatabase database = this.databaseHelper.getWritableDatabase();
    String currentStatement = null;
    try {
        if (!autoCommit) {
            database.beginTransaction();
        }

        for (String statement = source.readSqlStatement(); statement != null; statement = source
                .readSqlStatement()) {
            currentStatement = statement;
            update(statement);
            row++;
            if (!autoCommit && row % commitRate == 0) {
                database.setTransactionSuccessful();
                database.endTransaction();
                database.beginTransaction();
            }

            if (resultsListener != null) {
                resultsListener.sqlApplied(statement, row, 0, row);
            }
        }

        if (!autoCommit) {
            database.setTransactionSuccessful();
        }
    } catch (RuntimeException ex) {
        if (resultsListener != null) {
            resultsListener.sqlErrored(currentStatement, translate(currentStatement, ex), row, false, false);
        }
        throw ex;
    } finally {
        if (!autoCommit) {
            database.endTransaction();
        }

        close(database);
    }

    return row;
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void insert(T item, int conflictAlgorithm) {
    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {//from   w  ww.  j  ava  2  s  .  c o m
        db.beginTransaction();
        db.insertWithOnConflict(mTableName, null, itemToRow(item), conflictAlgorithm);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void insert(List<T> items, int conflictAlgorithm) {
    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {//from  ww w .  j a va  2  s.  com
        db.beginTransaction();
        for (T item : items)
            db.insertWithOnConflict(mTableName, null, itemToRow(item), conflictAlgorithm);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void update(T item, int conflictAlgorithm) {
    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {/* ww w.  j a va  2s .co m*/
        db.beginTransaction();
        db.updateWithOnConflict(mTableName, itemToRow(item), "id = ?", new String[] { String.valueOf(item.id) },
                conflictAlgorithm);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:ch.sebastienzurfluh.swissmuseumguides.contentprovider.model.io.connectors.LocalConnector.java

@Override
public void addAll(final Groups groups) {
    new Thread() {
        @Override// www.  j ava 2s.  c  o  m
        public void run() {
            SQLiteDatabase writableDatabase = getWritableDatabase();

            writableDatabase.beginTransaction();
            try {
                for (Group group : groups) {
                    String query = "INSERT OR REPLACE INTO " + groups.getTableName() + " VALUES (\""
                            + group.getId() + "\", \"" + group.getName() + "\", \"" + group.getMenuId()
                            + "\");";
                    getWritableDatabase().execSQL(query);
                }
                writableDatabase.setTransactionSuccessful();
            } catch (JSONException e) {
                System.out.println("Malformed json in groups");
                e.printStackTrace();
            } finally {
                writableDatabase.endTransaction();
            }
        }
    }.start();
}

From source file:com.dm.wallpaper.board.databases.Database.java

public void addWallpapers(@NonNull List<Wallpaper> wallpapers) {
    String query = "INSERT INTO " + TABLE_WALLPAPERS + " (" + KEY_NAME + "," + KEY_AUTHOR + "," + KEY_URL + ","
            + KEY_THUMB_URL + "," + KEY_CATEGORY + "," + KEY_ADDED_ON + ") VALUES (?,?,?,?,?,?);";
    SQLiteDatabase db = this.getWritableDatabase();
    SQLiteStatement statement = db.compileStatement(query);
    db.beginTransaction();/* www.j a  v  a  2 s  .c  o m*/

    for (int i = 0; i < wallpapers.size(); i++) {
        statement.clearBindings();
        statement.bindString(1, wallpapers.get(i).getName());
        statement.bindString(2, wallpapers.get(i).getAuthor());
        statement.bindString(3, wallpapers.get(i).getUrl());
        statement.bindString(4, wallpapers.get(i).getThumbUrl());
        statement.bindString(5, wallpapers.get(i).getCategory());
        statement.bindString(6, TimeHelper.getLongDateTime());
        statement.execute();
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
}

From source file:net.zionsoft.obadiah.model.Bible.java

private boolean removeTranslation(String translationShortName) {
    SQLiteDatabase db = null;
    try {/*from   w w w .  ja  v  a2 s .c  o  m*/
        db = mDatabaseHelper.openDatabase();
        if (db == null) {
            Analytics.trackException("Failed to open database.");
            return false;
        }
        db.beginTransaction();
        TranslationHelper.removeTranslation(db, translationShortName);
        db.setTransactionSuccessful();

        return true;
    } finally {
        if (db != null) {
            if (db.inTransaction()) {
                db.endTransaction();
            }
            mDatabaseHelper.closeDatabase();
        }
    }
}

From source file:ru.gkpromtech.exhibition.db.Table.java

public void update(List<T> items, int conflictAlgorithm) {
    SQLiteDatabase db = mSqlHelper.getWritableDatabase();
    try {//w  w w . j a va 2s .c om
        db.beginTransaction();
        for (T item : items)
            db.updateWithOnConflict(mTableName, itemToRow(item), "id = ?",
                    new String[] { String.valueOf(item.id) }, conflictAlgorithm);
        db.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.endTransaction();
        db.close();
    }
}

From source file:com.concentricsky.android.khanacademy.data.remote.LibraryUpdaterTask.java

@Override
protected Integer doInBackground(Void... params) {

    SharedPreferences prefs = dataService.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE);

    // Connectivity receiver.
    final NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
    ComponentName receiver = new ComponentName(dataService, WifiReceiver.class);
    PackageManager pm = dataService.getPackageManager();
    if (activeNetwork == null || !activeNetwork.isConnected()) {
        // We've missed a scheduled update. Enable the receiver so it can launch an update when we reconnect.
        Log.d(LOG_TAG, "Missed library update: not connected. Enabling connectivity receiver.");
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
        return RESULT_CODE_FAILURE;
    } else {//w  w  w  .  j  a v a2  s .c  o  m
        // We are connected. Disable the receiver.
        Log.d(LOG_TAG, "Library updater connected. Disabling connectivity receiver.");
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    InputStream in = null;
    String etag = prefs.getString(SETTING_LIBRARY_ETAG, null);

    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        if (etag != null && !force) {
            conn.setRequestProperty("If-None-Match", etag);
        }

        int code = conn.getResponseCode();
        switch (code) {
        case HttpStatus.SC_NOT_MODIFIED:
            // If we got a 304, we're done.
            // Use failure code to indicate there is no temp db to copy over.
            Log.d(LOG_TAG, "304 in library response.");
            return RESULT_CODE_FAILURE;
        default:
            // Odd, but on 1/3/13 I received correct json responses with a -1 for responseCode. Fall through.
            Log.w(LOG_TAG, "Error code in library response: " + code);
        case HttpStatus.SC_OK:
            // Parse response.
            in = conn.getInputStream();
            JsonFactory factory = new JsonFactory();
            final JsonParser parser = factory.createJsonParser(in);

            SQLiteDatabase tempDb = tempDbHelper.getWritableDatabase();
            tempDb.beginTransaction();
            try {
                tempDb.execSQL("delete from topic");
                tempDb.execSQL("delete from topicvideo");
                tempDb.execSQL("delete from video");

                parseObject(parser, tempDb, null, 0);
                tempDb.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
                return RESULT_CODE_FAILURE;
            } finally {
                tempDb.endTransaction();
                tempDb.close();
            }

            // Save etag once we've successfully parsed the response.
            etag = conn.getHeaderField("ETag");
            prefs.edit().putString(SETTING_LIBRARY_ETAG, etag).apply();

            // Move this new content from the temp db into the main one.
            mergeDbs();

            return RESULT_CODE_SUCCESS;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        tempDbHelper.close();
    }

    return RESULT_CODE_FAILURE;
}

From source file:ch.sebastienzurfluh.swissmuseumguides.contentprovider.model.io.connectors.LocalConnector.java

@Override
public void addAll(final Pages pages) {
    new Thread() {
        @Override//from  w ww . j  ava 2 s  .  c o  m
        public void run() {
            SQLiteDatabase writableDatabase = getWritableDatabase();

            writableDatabase.beginTransaction();
            try {
                for (Page page : pages) {
                    String query = "INSERT OR REPLACE INTO " + pages.getTableName() + " VALUES (\""
                            + page.getId() + "\", \"" + page.getTitle() + "\", \"" + page.getSubtitle()
                            + "\", \"" + page.getContent() + "\", \"" + page.getMenuId() + "\");";
                    getWritableDatabase().execSQL(query);
                }
                writableDatabase.setTransactionSuccessful();
            } catch (JSONException e) {
                System.out.println("Malformed json in pages");
                e.printStackTrace();
            } finally {
                writableDatabase.endTransaction();
            }
        }
    }.start();
}