Search in sources :

Example 16 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class JdbcConnection method setTransactionIsolation.

/**
 * Changes the current transaction isolation level. Calling this method will
 * commit an open transaction, even if the new level is the same as the old
 * one, except if the level is not supported. Internally, this method calls
 * SET LOCK_MODE, which affects all connections. The following isolation
 * levels are supported:
 * <ul>
 * <li>Connection.TRANSACTION_READ_UNCOMMITTED = SET LOCK_MODE 0: no locking
 * (should only be used for testing).</li>
 * <li>Connection.TRANSACTION_SERIALIZABLE = SET LOCK_MODE 1: table level
 * locking.</li>
 * <li>Connection.TRANSACTION_READ_COMMITTED = SET LOCK_MODE 3: table level
 * locking, but read locks are released immediately (default).</li>
 * </ul>
 * This setting is not persistent. Please note that using
 * TRANSACTION_READ_UNCOMMITTED while at the same time using multiple
 * connections may result in inconsistent transactions.
 *
 * @param level the new transaction isolation level:
 *            Connection.TRANSACTION_READ_UNCOMMITTED,
 *            Connection.TRANSACTION_READ_COMMITTED, or
 *            Connection.TRANSACTION_SERIALIZABLE
 * @throws SQLException if the connection is closed or the isolation level
 *             is not supported
 */
@Override
public void setTransactionIsolation(int level) throws SQLException {
    try {
        debugCodeCall("setTransactionIsolation", level);
        checkClosed();
        int lockMode;
        switch(level) {
            case Connection.TRANSACTION_READ_UNCOMMITTED:
                lockMode = Constants.LOCK_MODE_OFF;
                break;
            case Connection.TRANSACTION_READ_COMMITTED:
                lockMode = Constants.LOCK_MODE_READ_COMMITTED;
                break;
            case Connection.TRANSACTION_REPEATABLE_READ:
            case Connection.TRANSACTION_SERIALIZABLE:
                lockMode = Constants.LOCK_MODE_TABLE;
                break;
            default:
                throw DbException.getInvalidValueException("level", level);
        }
        commit();
        setLockMode = prepareCommand("SET LOCK_MODE ?", setLockMode);
        setLockMode.getParameters().get(0).setValue(ValueInt.get(lockMode), false);
        setLockMode.executeUpdate(false);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 17 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class FullText method reindex.

/**
 * Re-creates the full text index for this database. Calling this method is
 * usually not needed, as the index is kept up-to-date automatically.
 *
 * @param conn the connection
 */
public static void reindex(Connection conn) throws SQLException {
    init(conn);
    removeAllTriggers(conn, TRIGGER_PREFIX);
    FullTextSettings setting = FullTextSettings.getInstance(conn);
    setting.clearWordList();
    Statement stat = conn.createStatement();
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".WORDS");
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".ROWS");
    stat.execute("TRUNCATE TABLE " + SCHEMA + ".MAP");
    ResultSet rs = stat.executeQuery("SELECT * FROM " + SCHEMA + ".INDEXES");
    while (rs.next()) {
        String schema = rs.getString("SCHEMA");
        String table = rs.getString("TABLE");
        createTrigger(conn, schema, table);
        indexExistingRows(conn, schema, table);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 18 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class FullText method setIgnoreList.

/**
 * Change the ignore list. The ignore list is a comma separated list of
 * common words that must not be indexed. The default ignore list is empty.
 * If indexes already exist at the time this list is changed, reindex must
 * be called.
 *
 * @param conn the connection
 * @param commaSeparatedList the list
 */
public static void setIgnoreList(Connection conn, String commaSeparatedList) throws SQLException {
    try {
        init(conn);
        FullTextSettings setting = FullTextSettings.getInstance(conn);
        setIgnoreList(setting, commaSeparatedList);
        Statement stat = conn.createStatement();
        stat.execute("TRUNCATE TABLE " + SCHEMA + ".IGNORELIST");
        PreparedStatement prep = conn.prepareStatement("INSERT INTO " + SCHEMA + ".IGNORELIST VALUES(?)");
        prep.setString(1, commaSeparatedList);
        prep.execute();
    } catch (DbException e) {
        throw DbException.toSQLException(e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) DbException(org.h2.message.DbException)

Example 19 with Setting

use of org.h2.engine.Setting in project h2database by h2database.

the class TestFullText method testMultiThreaded.

private void testMultiThreaded(boolean lucene) throws Exception {
    final String prefix = lucene ? "FTL" : "FT";
    trace("Testing multithreaded " + prefix);
    deleteDb("fullText");
    ArrayList<Connection> connList = new ArrayList<>();
    try {
        int len = 2;
        Task[] task = new Task[len];
        for (int i = 0; i < len; i++) {
            final Connection conn = getConnection("fullText;LOCK_TIMEOUT=60000", connList);
            Statement stat = conn.createStatement();
            initFullText(stat, lucene);
            initFullText(stat, lucene);
            final String tableName = "TEST" + i;
            stat.execute("CREATE TABLE " + tableName + "(ID INT PRIMARY KEY, DATA VARCHAR)");
            stat.execute("CALL " + prefix + "_CREATE_INDEX('PUBLIC', '" + tableName + "', NULL)");
            task[i] = new Task() {

                @Override
                public void call() throws SQLException {
                    trace("starting thread " + Thread.currentThread());
                    PreparedStatement prep = conn.prepareStatement("INSERT INTO " + tableName + " VALUES(?, ?)");
                    Statement stat = conn.createStatement();
                    Random random = new Random();
                    int x = 0;
                    while (!stop) {
                        trace("stop = " + stop + " for " + Thread.currentThread());
                        StringBuilder buff = new StringBuilder();
                        for (int j = 0; j < 1000; j++) {
                            buff.append(" ").append(random.nextInt(10000));
                            buff.append(" x").append(j);
                            buff.append(" ").append(KNOWN_WORDS[j % KNOWN_WORDS.length]);
                        }
                        prep.setInt(1, x);
                        prep.setString(2, buff.toString());
                        prep.execute();
                        x++;
                        for (String knownWord : KNOWN_WORDS) {
                            trace("searching for " + knownWord + " with " + Thread.currentThread());
                            ResultSet rs = stat.executeQuery("SELECT * FROM " + prefix + "_SEARCH('" + knownWord + "', 0, 0)");
                            assertTrue(rs.next());
                        }
                    }
                    trace("closing connection");
                    if (!config.memory) {
                        conn.close();
                    }
                    trace("completed thread " + Thread.currentThread());
                }
            };
        }
        for (Task t : task) {
            t.execute();
        }
        trace("sleeping");
        Thread.sleep(1000);
        trace("setting stop to true");
        for (Task t : task) {
            trace("joining " + t);
            t.get();
            trace("done joining " + t);
        }
    } finally {
        close(connList);
    }
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Random(java.util.Random) ResultSet(java.sql.ResultSet)

Aggregations

PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)8 DbException (org.h2.message.DbException)8 Statement (java.sql.Statement)7 SimpleResultSet (org.h2.tools.SimpleResultSet)6 Schema (org.h2.schema.Schema)4 ArrayList (java.util.ArrayList)3 Constraint (org.h2.constraint.Constraint)3 Database (org.h2.engine.Database)3 Setting (org.h2.engine.Setting)3 ValueString (org.h2.value.ValueString)3 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 HashSet (java.util.HashSet)2 DbObject (org.h2.engine.DbObject)2 Right (org.h2.engine.Right)2 Role (org.h2.engine.Role)2 Session (org.h2.engine.Session)2 User (org.h2.engine.User)2 UserAggregate (org.h2.engine.UserAggregate)2