Search in sources :

Example 1 with DbSettings

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

the class Engine method openSession.

private synchronized Session openSession(ConnectionInfo ci) {
    boolean ifExists = ci.removeProperty("IFEXISTS", false);
    boolean ignoreUnknownSetting = ci.removeProperty("IGNORE_UNKNOWN_SETTINGS", false);
    String cipher = ci.removeProperty("CIPHER", null);
    String init = ci.removeProperty("INIT", null);
    Session session;
    for (int i = 0; ; i++) {
        session = openSession(ci, ifExists, cipher);
        if (session != null) {
            break;
        }
        // wait a bit to avoid a busy loop (the method is synchronized)
        if (i > 60 * 1000) {
            // retry at most 1 minute
            throw DbException.get(ErrorCode.DATABASE_ALREADY_OPEN_1, "Waited for database closing longer than 1 minute");
        }
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
        // ignore
        }
    }
    synchronized (session) {
        session.setAllowLiterals(true);
        DbSettings defaultSettings = DbSettings.getDefaultSettings();
        for (String setting : ci.getKeys()) {
            if (defaultSettings.containsKey(setting)) {
                // database setting are only used when opening the database
                continue;
            }
            String value = ci.getProperty(setting);
            try {
                CommandInterface command = session.prepareCommand("SET " + Parser.quoteIdentifier(setting) + " " + value, Integer.MAX_VALUE);
                command.executeUpdate(false);
            } catch (DbException e) {
                if (e.getErrorCode() == ErrorCode.ADMIN_RIGHTS_REQUIRED) {
                    session.getTrace().error(e, "admin rights required; user: \"" + ci.getUserName() + "\"");
                } else {
                    session.getTrace().error(e, "");
                }
                if (!ignoreUnknownSetting) {
                    session.close();
                    throw e;
                }
            }
        }
        if (init != null) {
            try {
                CommandInterface command = session.prepareCommand(init, Integer.MAX_VALUE);
                command.executeUpdate(false);
            } catch (DbException e) {
                if (!ignoreUnknownSetting) {
                    session.close();
                    throw e;
                }
            }
        }
        session.setAllowLiterals(false);
        session.commit(true);
    }
    return session;
}
Also used : CommandInterface(org.h2.command.CommandInterface) DbException(org.h2.message.DbException)

Example 2 with DbSettings

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

the class TableBase method getCreateSQL.

@Override
public String getCreateSQL() {
    Database db = getDatabase();
    if (db == null) {
        // closed
        return null;
    }
    StatementBuilder buff = new StatementBuilder("CREATE ");
    if (isTemporary()) {
        if (isGlobalTemporary()) {
            buff.append("GLOBAL ");
        } else {
            buff.append("LOCAL ");
        }
        buff.append("TEMPORARY ");
    } else if (isPersistIndexes()) {
        buff.append("CACHED ");
    } else {
        buff.append("MEMORY ");
    }
    buff.append("TABLE ");
    if (isHidden) {
        buff.append("IF NOT EXISTS ");
    }
    buff.append(getSQL());
    if (comment != null) {
        buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
    }
    buff.append("(\n    ");
    for (Column column : columns) {
        buff.appendExceptFirst(",\n    ");
        buff.append(column.getCreateSQL());
    }
    buff.append("\n)");
    if (tableEngine != null) {
        DbSettings s = db.getSettings();
        String d = s.defaultTableEngine;
        if (d == null && s.mvStore) {
            d = MVTableEngine.class.getName();
        }
        if (d == null || !tableEngine.endsWith(d)) {
            buff.append("\nENGINE ");
            buff.append(StringUtils.quoteIdentifier(tableEngine));
        }
    }
    if (!tableEngineParams.isEmpty()) {
        buff.append("\nWITH ");
        buff.resetCount();
        for (String parameter : tableEngineParams) {
            buff.appendExceptFirst(", ");
            buff.append(StringUtils.quoteIdentifier(parameter));
        }
    }
    if (!isPersistIndexes() && !isPersistData()) {
        buff.append("\nNOT PERSISTENT");
    }
    if (isHidden) {
        buff.append("\nHIDDEN");
    }
    return buff.toString();
}
Also used : DbSettings(org.h2.engine.DbSettings) StatementBuilder(org.h2.util.StatementBuilder) Database(org.h2.engine.Database) MVTableEngine(org.h2.mvstore.db.MVTableEngine)

Aggregations

CommandInterface (org.h2.command.CommandInterface)1 Database (org.h2.engine.Database)1 DbSettings (org.h2.engine.DbSettings)1 DbException (org.h2.message.DbException)1 MVTableEngine (org.h2.mvstore.db.MVTableEngine)1 StatementBuilder (org.h2.util.StatementBuilder)1