Java Code Examples for com.j256.ormlite.support.ConnectionSource#saveSpecialConnection()
The following examples show how to use
com.j256.ormlite.support.ConnectionSource#saveSpecialConnection() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrmLiteDatabaseHelper.java From AndroidBase with Apache License 2.0 | 6 votes |
/** * 数据库降级 */ @Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { ConnectionSource cs = getConnectionSource(); DatabaseConnection conn = cs.getSpecialConnection(null); boolean clearSpecial = false; if (conn == null) { conn = new AndroidDatabaseConnection(db, true, this.cancelQueriesEnabled); try { cs.saveSpecialConnection(conn); clearSpecial = true; } catch (SQLException var11) { throw new IllegalStateException("Could not save special connection", var11); } } try { this.onDowngrade(cs, oldVersion, newVersion); } finally { if (clearSpecial) { cs.clearSpecialConnection(conn); } } }
Example 2
Source File: StatementExecutor.java From ormlite-core with ISC License | 6 votes |
private <CT> CT doCallBatchTasks(ConnectionSource connectionSource, Callable<CT> callable) throws SQLException { DatabaseConnection connection = connectionSource.getReadWriteConnection(tableInfo.getTableName()); try { /* * We are using a thread-local boolean to detect whether we are in the middle of running a number of * changes. This disables the dao change notification for every batched call. */ localIsInBatchMode.set(true); /* * We need to save the connection because we are going to be disabling auto-commit on it and we don't want * pooled connection factories to give us another connection where auto-commit might still be enabled. */ boolean saved = connectionSource.saveSpecialConnection(connection); return doCallBatchTasks(connection, saved, callable); } finally { // even if the save-special returned false, we need to clear it to decrement the usage counter connectionSource.clearSpecialConnection(connection); connectionSource.releaseConnection(connection); localIsInBatchMode.set(false); if (dao != null) { // only at the end is the DAO notified of changes dao.notifyChanges(); } } }
Example 3
Source File: OrmLiteSqliteOpenHelper.java From ormlite-android with ISC License | 5 votes |
/** * Satisfies the {@link SQLiteOpenHelper#onCreate(SQLiteDatabase)} interface method. */ @Override public final void onCreate(SQLiteDatabase db) { ConnectionSource cs = getConnectionSource(); /* * The method is called by Android database helper's get-database calls when Android detects that we need to * create or update the database. So we have to use the database argument and save a connection to it on the * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource(). */ DatabaseConnection conn = cs.getSpecialConnection(null); boolean clearSpecial = false; if (conn == null) { conn = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled); try { cs.saveSpecialConnection(conn); clearSpecial = true; } catch (SQLException e) { throw new IllegalStateException("Could not save special connection", e); } } try { onCreate(db, cs); } finally { if (clearSpecial) { cs.clearSpecialConnection(conn); } } }
Example 4
Source File: OrmLiteSqliteOpenHelper.java From ormlite-android with ISC License | 5 votes |
/** * Satisfies the {@link SQLiteOpenHelper#onUpgrade(SQLiteDatabase, int, int)} interface method. */ @Override public final void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ConnectionSource cs = getConnectionSource(); /* * The method is called by Android database helper's get-database calls when Android detects that we need to * create or update the database. So we have to use the database argument and save a connection to it on the * AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource(). */ DatabaseConnection conn = cs.getSpecialConnection(null); boolean clearSpecial = false; if (conn == null) { conn = new AndroidDatabaseConnection(db, true, cancelQueriesEnabled); try { cs.saveSpecialConnection(conn); clearSpecial = true; } catch (SQLException e) { throw new IllegalStateException("Could not save special connection", e); } } try { onUpgrade(db, cs, oldVersion, newVersion); } finally { if (clearSpecial) { cs.clearSpecialConnection(conn); } } }
Example 5
Source File: TransactionManager.java From ormlite-core with ISC License | 5 votes |
/** * Same as {@link #callInTransaction(ConnectionSource, Callable)} except this has a table-name. * * <p> * WARNING: it is up to you to properly synchronize around this method if multiple threads are using a * connection-source which works gives out a single-connection. The reason why this is necessary is that multiple * operations are performed on the connection and race-conditions will exist with multiple threads working on the * same connection. * </p> */ public static <T> T callInTransaction(String tableName, final ConnectionSource connectionSource, final Callable<T> callable) throws SQLException { DatabaseConnection connection = connectionSource.getReadWriteConnection(tableName); try { boolean saved = connectionSource.saveSpecialConnection(connection); return callInTransaction(connection, saved, connectionSource.getDatabaseType(), callable); } finally { // we should clear aggressively connectionSource.clearSpecialConnection(connection); connectionSource.releaseConnection(connection); } }