提交 7345f4fc authored 作者: Thomas Mueller's avatar Thomas Mueller

Opening a connection with AUTO_SERVER=TRUE is now fast.

上级 1a5fa020
...@@ -220,7 +220,13 @@ public class Database implements DataHandler { ...@@ -220,7 +220,13 @@ public class Database implements DataHandler {
} }
} catch (Exception e) { } catch (Exception e) {
if (traceSystem != null) { if (traceSystem != null) {
if (e instanceof SQLException) {
SQLException e2 = (SQLException) e;
if (e2.getErrorCode() != ErrorCode.DATABASE_ALREADY_OPEN_1) {
// only write if the database is not already in use
traceSystem.getTrace(Trace.DATABASE).error("opening " + databaseName, e); traceSystem.getTrace(Trace.DATABASE).error("opening " + databaseName, e);
}
}
traceSystem.close(); traceSystem.close();
} }
closeOpenFilesAndUnlock(); closeOpenFilesAndUnlock();
...@@ -495,13 +501,7 @@ public class Database implements DataHandler { ...@@ -495,13 +501,7 @@ public class Database implements DataHandler {
if (FileUtils.exists(dataFileName)) { if (FileUtils.exists(dataFileName)) {
// if it is already read-only because ACCESS_MODE_DATA=r // if it is already read-only because ACCESS_MODE_DATA=r
readOnly = readOnly | FileUtils.isReadOnly(dataFileName); readOnly = readOnly | FileUtils.isReadOnly(dataFileName);
textStorage = isTextStorage(dataFileName, textStorage);
lobFilesInDirectories &= !ValueLob.existsLobFile(getDatabasePath());
lobFilesInDirectories |= FileUtils.exists(databaseName + Constants.SUFFIX_LOBS_DIRECTORY);
}
} }
dummy = DataPage.create(this, 0);
if (persistent) {
if (readOnly) { if (readOnly) {
traceSystem = new TraceSystem(null, false); traceSystem = new TraceSystem(null, false);
} else { } else {
...@@ -526,6 +526,12 @@ public class Database implements DataHandler { ...@@ -526,6 +526,12 @@ public class Database implements DataHandler {
startServer(lock.getUniqueId()); startServer(lock.getUniqueId());
} }
} }
if (FileUtils.exists(dataFileName)) {
textStorage = isTextStorage(dataFileName, textStorage);
lobFilesInDirectories &= !ValueLob.existsLobFile(getDatabasePath());
lobFilesInDirectories |= FileUtils.exists(databaseName + Constants.SUFFIX_LOBS_DIRECTORY);
}
dummy = DataPage.create(this, 0);
deleteOldTempFiles(); deleteOldTempFiles();
log = new LogSystem(this, databaseName, readOnly, accessModeLog); log = new LogSystem(this, databaseName, readOnly, accessModeLog);
openFileData(); openFileData();
......
...@@ -56,6 +56,7 @@ public class SessionRemote implements SessionInterface, DataHandler { ...@@ -56,6 +56,7 @@ public class SessionRemote implements SessionInterface, DataHandler {
public static final int SESSION_PREPARE_READ_PARAMS = 11; public static final int SESSION_PREPARE_READ_PARAMS = 11;
public static final int SESSION_SET_ID = 12; public static final int SESSION_SET_ID = 12;
public static final int SESSION_CANCEL_STATEMENT = 13; public static final int SESSION_CANCEL_STATEMENT = 13;
public static final int SESSION_CHECK_KEY = 14;
public static final int STATUS_ERROR = 0; public static final int STATUS_ERROR = 0;
public static final int STATUS_OK = 1; public static final int STATUS_OK = 1;
......
...@@ -83,8 +83,17 @@ public class TcpServerThread implements Runnable { ...@@ -83,8 +83,17 @@ public class TcpServerThread implements Runnable {
int command = transfer.readInt(); int command = transfer.readInt();
stop = true; stop = true;
if (command == SessionRemote.SESSION_CANCEL_STATEMENT) { if (command == SessionRemote.SESSION_CANCEL_STATEMENT) {
// cancel a running statement
int statementId = transfer.readInt(); int statementId = transfer.readInt();
server.cancelStatement(sessionId, statementId); server.cancelStatement(sessionId, statementId);
} else if (command == SessionRemote.SESSION_CHECK_KEY) {
// check if this is the correct server
db = server.checkKeyAndGetDatabaseName(sessionId);
if (!sessionId.equals(db)) {
transfer.writeInt(SessionRemote.STATUS_OK);
} else {
transfer.writeInt(SessionRemote.STATUS_ERROR);
}
} }
} }
String baseDir = server.getBaseDir(); String baseDir = server.getBaseDir();
......
...@@ -19,6 +19,8 @@ import java.util.Properties; ...@@ -19,6 +19,8 @@ import java.util.Properties;
import org.h2.constant.ErrorCode; import org.h2.constant.ErrorCode;
import org.h2.constant.SysProperties; import org.h2.constant.SysProperties;
import org.h2.engine.Constants;
import org.h2.engine.SessionRemote;
import org.h2.jdbc.JdbcSQLException; import org.h2.jdbc.JdbcSQLException;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.message.Trace; import org.h2.message.Trace;
...@@ -28,6 +30,7 @@ import org.h2.util.ByteUtils; ...@@ -28,6 +30,7 @@ import org.h2.util.ByteUtils;
import org.h2.util.NetUtils; import org.h2.util.NetUtils;
import org.h2.util.RandomUtils; import org.h2.util.RandomUtils;
import org.h2.util.SortedProperties; import org.h2.util.SortedProperties;
import org.h2.value.Transfer;
/** /**
* The file lock is used to lock a database so that only one process can write * The file lock is used to lock a database so that only one process can write
...@@ -120,6 +123,7 @@ public class FileLock { ...@@ -120,6 +123,7 @@ public class FileLock {
public synchronized void lock(String fileName, boolean allowSocket) throws SQLException { public synchronized void lock(String fileName, boolean allowSocket) throws SQLException {
this.fs = FileSystem.getInstance(fileName); this.fs = FileSystem.getInstance(fileName);
this.fileName = fileName; this.fileName = fileName;
checkServer();
if (locked) { if (locked) {
throw Message.getInternalError("already locked"); throw Message.getInternalError("already locked");
} }
...@@ -205,6 +209,43 @@ public class FileLock { ...@@ -205,6 +209,43 @@ public class FileLock {
} }
} }
private void checkServer() throws SQLException {
Properties prop = load();
String server = prop.getProperty("server");
if (server == null) {
return;
}
boolean running = false;
String id = prop.getProperty("id");
try {
Socket socket = NetUtils.createSocket(server, Constants.DEFAULT_SERVER_PORT, false);
Transfer transfer = new Transfer(null);
transfer.setSocket(socket);
transfer.init();
transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
transfer.writeString(null);
transfer.writeString(null);
transfer.writeString(id);
transfer.writeInt(SessionRemote.SESSION_CHECK_KEY);
transfer.flush();
int state = transfer.readInt();
if (state == SessionRemote.STATUS_OK) {
running = true;
}
transfer.close();
socket.close();
} catch (IOException e) {
return;
}
if (running) {
String payload = server + "/" + id;
JdbcSQLException ex = Message.getSQLException(ErrorCode.DATABASE_ALREADY_OPEN_1, "Server is running");
ex.setPayload(payload);
throw ex;
}
}
private Properties load() throws SQLException { private Properties load() throws SQLException {
try { try {
Properties p2 = SortedProperties.loadProperties(fileName); Properties p2 = SortedProperties.loadProperties(fileName);
......
...@@ -59,7 +59,6 @@ public class TestAutoServer extends TestBase { ...@@ -59,7 +59,6 @@ public class TestAutoServer extends TestBase {
if (i <= 0) { if (i <= 0) {
fail(); fail();
} }
Connection conn = getConnection(url + ";OPEN_NEW=TRUE"); Connection conn = getConnection(url + ";OPEN_NEW=TRUE");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
if (config.big) { if (config.big) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论