Search in sources :

Example 1 with TraceSystem

use of org.h2.message.TraceSystem in project h2database by h2database.

the class Database method setBackgroundException.

public void setBackgroundException(DbException e) {
    if (backgroundException == null) {
        backgroundException = e;
        TraceSystem t = getTraceSystem();
        if (t != null) {
            t.getTrace(Trace.DATABASE).error(e, "flush");
        }
    }
}
Also used : TraceSystem(org.h2.message.TraceSystem)

Example 2 with TraceSystem

use of org.h2.message.TraceSystem in project h2database by h2database.

the class SessionRemote method connectServer.

private void connectServer(ConnectionInfo ci) {
    String name = ci.getName();
    if (name.startsWith("//")) {
        name = name.substring("//".length());
    }
    int idx = name.indexOf('/');
    if (idx < 0) {
        throw ci.getFormatException();
    }
    databaseName = name.substring(idx + 1);
    String server = name.substring(0, idx);
    traceSystem = new TraceSystem(null);
    String traceLevelFile = ci.getProperty(SetTypes.TRACE_LEVEL_FILE, null);
    if (traceLevelFile != null) {
        int level = Integer.parseInt(traceLevelFile);
        String prefix = getFilePrefix(SysProperties.CLIENT_TRACE_DIRECTORY);
        try {
            traceSystem.setLevelFile(level);
            if (level > 0 && level < 4) {
                String file = FileUtils.createTempFile(prefix, Constants.SUFFIX_TRACE_FILE, false, false);
                traceSystem.setFileName(file);
            }
        } catch (IOException e) {
            throw DbException.convertIOException(e, prefix);
        }
    }
    String traceLevelSystemOut = ci.getProperty(SetTypes.TRACE_LEVEL_SYSTEM_OUT, null);
    if (traceLevelSystemOut != null) {
        int level = Integer.parseInt(traceLevelSystemOut);
        traceSystem.setLevelSystemOut(level);
    }
    trace = traceSystem.getTrace(Trace.JDBC);
    String serverList = null;
    if (server.indexOf(',') >= 0) {
        serverList = StringUtils.quoteStringSQL(server);
        ci.setProperty("CLUSTER", Constants.CLUSTERING_ENABLED);
    }
    autoReconnect = ci.getProperty("AUTO_RECONNECT", false);
    // AUTO_SERVER implies AUTO_RECONNECT
    boolean autoServer = ci.getProperty("AUTO_SERVER", false);
    if (autoServer && serverList != null) {
        throw DbException.getUnsupportedException("autoServer && serverList != null");
    }
    autoReconnect |= autoServer;
    if (autoReconnect) {
        String className = ci.getProperty("DATABASE_EVENT_LISTENER");
        if (className != null) {
            className = StringUtils.trim(className, true, true, "'");
            try {
                eventListener = (DatabaseEventListener) JdbcUtils.loadUserClass(className).newInstance();
            } catch (Throwable e) {
                throw DbException.convert(e);
            }
        }
    }
    cipher = ci.getProperty("CIPHER");
    if (cipher != null) {
        fileEncryptionKey = MathUtils.secureRandomBytes(32);
    }
    String[] servers = StringUtils.arraySplit(server, ',', true);
    int len = servers.length;
    transferList.clear();
    sessionId = StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32));
    // TODO cluster: support more than 2 connections
    boolean switchOffCluster = false;
    try {
        for (String s : servers) {
            try {
                Transfer trans = initTransfer(ci, databaseName, s);
                transferList.add(trans);
            } catch (IOException e) {
                if (len == 1) {
                    throw DbException.get(ErrorCode.CONNECTION_BROKEN_1, e, e + ": " + s);
                }
                switchOffCluster = true;
            }
        }
        checkClosed();
        if (switchOffCluster) {
            switchOffCluster();
        }
        checkClusterDisableAutoCommit(serverList);
    } catch (DbException e) {
        traceSystem.close();
        throw e;
    }
}
Also used : Transfer(org.h2.value.Transfer) IOException(java.io.IOException) TraceSystem(org.h2.message.TraceSystem) DbException(org.h2.message.DbException)

Example 3 with TraceSystem

use of org.h2.message.TraceSystem in project h2database by h2database.

the class FileLister method tryUnlockDatabase.

/**
 * Try to lock the database, and then unlock it. If this worked, the
 * .lock.db file will be removed.
 *
 * @param files the database files to check
 * @param message the text to include in the error message
 * @throws SQLException if it failed
 */
public static void tryUnlockDatabase(List<String> files, String message) throws SQLException {
    for (String fileName : files) {
        if (fileName.endsWith(Constants.SUFFIX_LOCK_FILE)) {
            FileLock lock = new FileLock(new TraceSystem(null), fileName, Constants.LOCK_SLEEP);
            try {
                lock.lock(FileLockMethod.FILE);
                lock.unlock();
            } catch (DbException e) {
                throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, message).getSQLException();
            }
        } else if (fileName.endsWith(Constants.SUFFIX_MV_FILE)) {
            try (FileChannel f = FilePath.get(fileName).open("r")) {
                java.nio.channels.FileLock lock = f.tryLock(0, Long.MAX_VALUE, true);
                lock.release();
            } catch (Exception e) {
                throw DbException.get(ErrorCode.CANNOT_CHANGE_SETTING_WHEN_OPEN_1, e, message).getSQLException();
            }
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) TraceSystem(org.h2.message.TraceSystem) SQLException(java.sql.SQLException) DbException(org.h2.message.DbException) DbException(org.h2.message.DbException)

Example 4 with TraceSystem

use of org.h2.message.TraceSystem in project h2database by h2database.

the class TestTraceSystem method testAdapter.

private void testAdapter() {
    TraceSystem ts = new TraceSystem(null);
    ts.setName("test");
    ts.setLevelFile(TraceSystem.ADAPTER);
    ts.getTrace("test").debug("test");
    ts.getTrace("test").info("test");
    ts.getTrace("test").error(new Exception(), "test");
    // The used SLF4J-nop logger has all log levels disabled,
    // so this should be reflected in the trace system.
    assertFalse(ts.isEnabled(TraceSystem.INFO));
    assertFalse(ts.getTrace("test").isInfoEnabled());
    ts.close();
}
Also used : TraceSystem(org.h2.message.TraceSystem)

Example 5 with TraceSystem

use of org.h2.message.TraceSystem in project h2database by h2database.

the class TestFileLock method testFutureModificationDate.

private void testFutureModificationDate() throws Exception {
    File f = new File(getFile());
    f.delete();
    f.createNewFile();
    f.setLastModified(System.currentTimeMillis() + 10000);
    FileLock lock = new FileLock(new TraceSystem(null), getFile(), Constants.LOCK_SLEEP);
    lock.lock(FileLockMethod.FILE);
    lock.unlock();
}
Also used : FileLock(org.h2.store.FileLock) TraceSystem(org.h2.message.TraceSystem) File(java.io.File)

Aggregations

TraceSystem (org.h2.message.TraceSystem)14 FileLock (org.h2.store.FileLock)5 DbException (org.h2.message.DbException)4 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 FileChannel (java.nio.channels.FileChannel)1 AccessControlException (java.security.AccessControlException)1 BitSet (java.util.BitSet)1 Properties (java.util.Properties)1 CreateTableData (org.h2.command.ddl.CreateTableData)1 Constraint (org.h2.constraint.Constraint)1 Database (org.h2.engine.Database)1 Cursor (org.h2.index.Cursor)1 Schema (org.h2.schema.Schema)1 Column (org.h2.table.Column)1 IndexColumn (org.h2.table.IndexColumn)1 Transfer (org.h2.value.Transfer)1