Search in sources :

Example 1 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class CommandRemote method close.

@Override
public void close() {
    if (session == null || session.isClosed()) {
        return;
    }
    synchronized (session) {
        session.traceOperation("COMMAND_CLOSE", id);
        for (Transfer transfer : transferList) {
            try {
                transfer.writeInt(SessionRemote.COMMAND_CLOSE).writeInt(id);
            } catch (IOException e) {
                trace.error(e, "close");
            }
        }
    }
    session = null;
    try {
        for (ParameterInterface p : parameters) {
            Value v = p.getParamValue();
            if (v != null) {
                v.remove();
            }
        }
    } catch (DbException e) {
        trace.error(e, "close");
    }
    parameters.clear();
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) Transfer(org.h2.value.Transfer) Value(org.h2.value.Value) IOException(java.io.IOException) DbException(org.h2.message.DbException)

Example 2 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class CommandRemote method prepare.

private void prepare(SessionRemote s, boolean createParams) {
    id = s.getNextId();
    for (int i = 0, count = 0; i < transferList.size(); i++) {
        try {
            Transfer transfer = transferList.get(i);
            boolean v16 = s.getClientVersion() >= Constants.TCP_PROTOCOL_VERSION_16;
            if (createParams) {
                s.traceOperation(v16 ? "SESSION_PREPARE_READ_PARAMS2" : "SESSION_PREPARE_READ_PARAMS", id);
                transfer.writeInt(v16 ? SessionRemote.SESSION_PREPARE_READ_PARAMS2 : SessionRemote.SESSION_PREPARE_READ_PARAMS).writeInt(id).writeString(sql);
            } else {
                s.traceOperation("SESSION_PREPARE", id);
                transfer.writeInt(SessionRemote.SESSION_PREPARE).writeInt(id).writeString(sql);
            }
            s.done(transfer);
            isQuery = transfer.readBoolean();
            readonly = transfer.readBoolean();
            cmdType = v16 && createParams ? transfer.readInt() : UNKNOWN;
            int paramCount = transfer.readInt();
            if (createParams) {
                parameters.clear();
                for (int j = 0; j < paramCount; j++) {
                    ParameterRemote p = new ParameterRemote(j);
                    p.readMetaData(transfer);
                    parameters.add(p);
                }
            }
        } catch (IOException e) {
            s.removeServer(e, i--, ++count);
        }
    }
}
Also used : ParameterRemote(org.h2.expression.ParameterRemote) Transfer(org.h2.value.Transfer) IOException(java.io.IOException)

Example 3 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class SessionRemote method close.

@Override
public void close() {
    RuntimeException closeError = null;
    if (transferList != null) {
        synchronized (this) {
            for (Transfer transfer : transferList) {
                try {
                    traceOperation("SESSION_CLOSE", 0);
                    transfer.writeInt(SessionRemote.SESSION_CLOSE);
                    done(transfer);
                    transfer.close();
                } catch (RuntimeException e) {
                    trace.error(e, "close");
                    closeError = e;
                } catch (Exception e) {
                    trace.error(e, "close");
                }
            }
        }
        transferList = null;
    }
    traceSystem.close();
    if (embedded != null) {
        embedded.close();
        embedded = null;
    }
    if (closeError != null) {
        throw closeError;
    }
}
Also used : Transfer(org.h2.value.Transfer) DbException(org.h2.message.DbException) IOException(java.io.IOException) JdbcSQLException(org.h2.jdbc.JdbcSQLException)

Example 4 with Transfer

use of org.h2.value.Transfer 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 5 with Transfer

use of org.h2.value.Transfer in project h2database by h2database.

the class SessionRemote method cancelStatement.

/**
 * Cancel the statement with the given id.
 *
 * @param id the statement id
 */
public void cancelStatement(int id) {
    for (Transfer transfer : transferList) {
        try {
            Transfer trans = transfer.openNewConnection();
            trans.init();
            trans.writeInt(clientVersion);
            trans.writeInt(clientVersion);
            trans.writeString(null);
            trans.writeString(null);
            trans.writeString(sessionId);
            trans.writeInt(SessionRemote.SESSION_CANCEL_STATEMENT);
            trans.writeInt(id);
            trans.close();
        } catch (IOException e) {
            trace.debug(e, "could not cancel statement");
        }
    }
}
Also used : Transfer(org.h2.value.Transfer) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)15 Transfer (org.h2.value.Transfer)14 DbException (org.h2.message.DbException)5 Socket (java.net.Socket)3 ResultRemote (org.h2.result.ResultRemote)3 Value (org.h2.value.Value)3 Properties (java.util.Properties)2 ParameterInterface (org.h2.expression.ParameterInterface)2 JdbcSQLException (org.h2.jdbc.JdbcSQLException)2 ServerSocket (java.net.ServerSocket)1 Iterator (java.util.Iterator)1 Constraint (org.h2.constraint.Constraint)1 ConnectionInfo (org.h2.engine.ConnectionInfo)1 SysProperties (org.h2.engine.SysProperties)1 ParameterRemote (org.h2.expression.ParameterRemote)1 TraceSystem (org.h2.message.TraceSystem)1 SortedProperties (org.h2.util.SortedProperties)1