Java Code Examples for org.sqlite.SQLiteConfig#SynchronousMode

The following examples show how to use org.sqlite.SQLiteConfig#SynchronousMode . 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: DBQueries.java    From cqengine with Apache License 2.0 6 votes vote down vote up
public static void setSyncAndJournaling(final Connection connection, final SQLiteConfig.SynchronousMode pragmaSynchronous, final SQLiteConfig.JournalMode pragmaJournalMode){
    Statement statement = null;
    try {
        final boolean autoCommit = DBUtils.setAutoCommit(connection, true);
        statement = connection.createStatement();
        statement.execute("PRAGMA synchronous = " + pragmaSynchronous.getValue());

        // This little transaction will also cause a wanted fsync on the OS to flush the data still in the OS cache to disc.
        statement.execute("PRAGMA journal_mode = " + pragmaJournalMode.getValue());

        DBUtils.setAutoCommit(connection, autoCommit);
    }catch (SQLException e){
        throw new IllegalStateException("Unable to set the 'synchronous' and 'journal_mode' pragmas", e);
    }finally{
        DBUtils.closeQuietly(statement);
    }
}
 
Example 2
Source File: DBQueries.java    From cqengine with Apache License 2.0 6 votes vote down vote up
public static SQLiteConfig.SynchronousMode getPragmaSynchronousOrNull(final Connection connection){
    Statement statement = null;
    try {
        statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("PRAGMA synchronous;");
        if (resultSet.next()){
            final int syncPragmaId = resultSet.getInt(1);
            if (!resultSet.wasNull()) {
                switch (syncPragmaId){
                    case 0: return SQLiteConfig.SynchronousMode.OFF;
                    case 1: return SQLiteConfig.SynchronousMode.NORMAL;
                    case 2: return SQLiteConfig.SynchronousMode.FULL;
                    default: return null;
                }
            }
        }
        return null;
    }catch (Exception e){
        return null;
    }finally{
        DBUtils.closeQuietly(statement);
    }
}
 
Example 3
Source File: DBQueriesTest.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Test
public void suspendSyncAndJournaling() throws Exception {
    Connection connection = null;
    try {

        ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
        connection = connectionManager.getConnection(null, noQueryOptions());

        final SQLiteConfig.JournalMode journalMode = DBQueries.getPragmaJournalModeOrNull(connection);
        final SQLiteConfig.SynchronousMode synchronousMode = DBQueries.getPragmaSynchronousOrNull(connection);

        DBQueries.suspendSyncAndJournaling(connection);

        final SQLiteConfig.JournalMode journalModeDisabled = DBQueries.getPragmaJournalModeOrNull(connection);
        final SQLiteConfig.SynchronousMode synchronousModeDisabled = DBQueries.getPragmaSynchronousOrNull(connection);

        Assert.assertEquals(journalModeDisabled, SQLiteConfig.JournalMode.OFF);
        Assert.assertEquals(synchronousModeDisabled, SQLiteConfig.SynchronousMode.OFF);

        DBQueries.setSyncAndJournaling(connection, SQLiteConfig.SynchronousMode.FULL, SQLiteConfig.JournalMode.DELETE);

        final SQLiteConfig.JournalMode journalModeReset = DBQueries.getPragmaJournalModeOrNull(connection);
        final SQLiteConfig.SynchronousMode synchronousModeReset = DBQueries.getPragmaSynchronousOrNull(connection);

        Assert.assertEquals(journalModeReset, journalMode);
        Assert.assertEquals(synchronousModeReset, synchronousMode);

    }finally {
        DBUtils.closeQuietly(connection);
    }
}