use of org.h2.test.db.Db in project h2database by h2database.
the class TcpServerThread method run.
@Override
public void run() {
try {
transfer.init();
trace("Connect");
// and a list of allowed clients
try {
Socket socket = transfer.getSocket();
if (socket == null) {
// the transfer is already closed, prevent NPE in TcpServer#allow(Socket)
return;
}
if (!server.allow(transfer.getSocket())) {
throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED);
}
int minClientVersion = transfer.readInt();
int maxClientVersion = transfer.readInt();
if (maxClientVersion < Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED) {
throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, "" + clientVersion, "" + Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED);
} else if (minClientVersion > Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) {
throw DbException.get(ErrorCode.DRIVER_VERSION_ERROR_2, "" + clientVersion, "" + Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED);
}
if (maxClientVersion >= Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED) {
clientVersion = Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED;
} else {
clientVersion = maxClientVersion;
}
transfer.setVersion(clientVersion);
String db = transfer.readString();
String originalURL = transfer.readString();
if (db == null && originalURL == null) {
String targetSessionId = transfer.readString();
int command = transfer.readInt();
stop = true;
if (command == SessionRemote.SESSION_CANCEL_STATEMENT) {
// cancel a running statement
int statementId = transfer.readInt();
server.cancelStatement(targetSessionId, statementId);
} else if (command == SessionRemote.SESSION_CHECK_KEY) {
// check if this is the correct server
db = server.checkKeyAndGetDatabaseName(targetSessionId);
if (!targetSessionId.equals(db)) {
transfer.writeInt(SessionRemote.STATUS_OK);
} else {
transfer.writeInt(SessionRemote.STATUS_ERROR);
}
}
}
String baseDir = server.getBaseDir();
if (baseDir == null) {
baseDir = SysProperties.getBaseDir();
}
db = server.checkKeyAndGetDatabaseName(db);
ConnectionInfo ci = new ConnectionInfo(db);
ci.setOriginalURL(originalURL);
ci.setUserName(transfer.readString());
ci.setUserPasswordHash(transfer.readBytes());
ci.setFilePasswordHash(transfer.readBytes());
int len = transfer.readInt();
for (int i = 0; i < len; i++) {
ci.setProperty(transfer.readString(), transfer.readString());
}
// override client's requested properties with server settings
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
if (server.getIfExists()) {
ci.setProperty("IFEXISTS", "TRUE");
}
transfer.writeInt(SessionRemote.STATUS_OK);
transfer.writeInt(clientVersion);
transfer.flush();
if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_13) {
if (ci.getFilePasswordHash() != null) {
ci.setFileEncryptionKey(transfer.readBytes());
}
}
session = Engine.getInstance().createSession(ci);
transfer.setSession(session);
server.addConnection(threadId, originalURL, ci.getUserName());
trace("Connected");
} catch (Throwable e) {
sendError(e);
stop = true;
}
while (!stop) {
try {
process();
} catch (Throwable e) {
sendError(e);
}
}
trace("Disconnect");
} catch (Throwable e) {
server.traceError(e);
} finally {
close();
}
}
use of org.h2.test.db.Db in project h2database by h2database.
the class ResultTempTable method find.
private Cursor find(Row row) {
if (index == null) {
// for the case "in(select ...)", the query might
// use an optimization and not create the index
// up front
createIndex();
}
Cursor cursor = index.find(session, row, row);
while (cursor.next()) {
SearchRow found = cursor.getSearchRow();
boolean ok = true;
Database db = session.getDatabase();
for (int i = 0; i < row.getColumnCount(); i++) {
if (!db.areEqual(row.getValue(i), found.getValue(i))) {
ok = false;
break;
}
}
if (ok) {
return cursor;
}
}
return null;
}
use of org.h2.test.db.Db in project h2database by h2database.
the class RowList method writeAllRows.
private void writeAllRows() {
if (file == null) {
Database db = session.getDatabase();
String fileName = db.createTempFile();
file = db.openFile(fileName, "rw", false);
file.setCheckedWriting(false);
file.seek(FileStore.HEADER_LENGTH);
rowBuff = Data.create(db, Constants.DEFAULT_PAGE_SIZE);
file.seek(FileStore.HEADER_LENGTH);
}
Data buff = rowBuff;
initBuffer(buff);
for (int i = 0, size = list.size(); i < size; i++) {
if (i > 0 && buff.length() > Constants.IO_BUFFER_SIZE) {
flushBuffer(buff);
initBuffer(buff);
}
Row r = list.get(i);
writeRow(buff, r);
}
flushBuffer(buff);
file.autoDelete();
list.clear();
memory = 0;
}
use of org.h2.test.db.Db in project h2database by h2database.
the class TcpServer method shutdown.
/**
* Stop the TCP server with the given URL.
*
* @param url the database URL
* @param password the password
* @param force if the server should be stopped immediately
* @param all whether all TCP servers that are running in the JVM should be
* stopped
*/
public static synchronized void shutdown(String url, String password, boolean force, boolean all) throws SQLException {
try {
int port = Constants.DEFAULT_TCP_PORT;
int idx = url.lastIndexOf(':');
if (idx >= 0) {
String p = url.substring(idx + 1);
if (StringUtils.isNumber(p)) {
port = Integer.decode(p);
}
}
String db = getManagementDbName(port);
try {
org.h2.Driver.load();
} catch (Throwable e) {
throw DbException.convert(e);
}
for (int i = 0; i < 2; i++) {
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DriverManager.getConnection("jdbc:h2:" + url + "/" + db, "", password);
prep = conn.prepareStatement("CALL STOP_SERVER(?, ?, ?)");
prep.setInt(1, all ? 0 : port);
prep.setString(2, password);
prep.setInt(3, force ? SHUTDOWN_FORCE : SHUTDOWN_NORMAL);
try {
prep.execute();
} catch (SQLException e) {
if (force) {
// ignore
} else {
if (e.getErrorCode() != ErrorCode.CONNECTION_BROKEN_1) {
throw e;
}
}
}
break;
} catch (SQLException e) {
if (i == 1) {
throw e;
}
} finally {
JdbcUtils.closeSilently(prep);
JdbcUtils.closeSilently(conn);
}
}
} catch (Exception e) {
throw DbException.toSQLException(e);
}
}
use of org.h2.test.db.Db in project h2database by h2database.
the class TestTools method testTraceFile.
private void testTraceFile(String url) throws SQLException {
Connection conn;
Recover.main("-removePassword", "-dir", getBaseDir(), "-db", "toolsConvertTraceFile");
conn = getConnection(url, "sa", "");
Statement stat = conn.createStatement();
ResultSet rs;
rs = stat.executeQuery("select * from test");
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("Hello \\'Joe\n\\'", rs.getString(2));
assertEquals("10.20", rs.getBigDecimal(3).toString());
assertFalse(rs.next());
rs = stat.executeQuery("select * from test2");
rs.next();
assertEquals(Float.MIN_VALUE, rs.getFloat("a"));
assertEquals(Double.MIN_VALUE, rs.getDouble("b"));
assertEquals(Long.MIN_VALUE, rs.getLong("c"));
assertEquals(Short.MIN_VALUE, rs.getShort("d"));
assertFalse(rs.getBoolean("e"));
assertEquals(new byte[] { (byte) 10, (byte) 20 }, rs.getBytes("f"));
assertEquals("2007-12-31", rs.getString("g"));
assertEquals("23:59:59", rs.getString("h"));
assertEquals("2007-12-31 23:59:59", rs.getString("i"));
assertFalse(rs.next());
conn.close();
}
Aggregations