use of org.h2.util.SortedProperties in project h2database by h2database.
the class TestAutoServer method testAutoServer.
private void testAutoServer(boolean port) throws Exception {
if (config.memory || config.networked) {
return;
}
deleteDb(getTestName());
String url = getURL(getTestName() + ";AUTO_SERVER=TRUE", true);
if (port) {
url += ";AUTO_SERVER_PORT=11111";
}
String user = getUser(), password = getPassword();
Connection connServer = getConnection(url + ";OPEN_NEW=TRUE", user, password);
int i = ITERATIONS;
for (; i > 0; i--) {
Thread.sleep(100);
SortedProperties prop = SortedProperties.loadProperties(getBaseDir() + "/" + getTestName() + ".lock.db");
String key = prop.getProperty("id");
String server = prop.getProperty("server");
if (server != null) {
String u2 = url.substring(url.indexOf(';'));
u2 = "jdbc:h2:tcp://" + server + "/" + key + u2;
Connection conn = DriverManager.getConnection(u2, user, password);
conn.close();
int gotPort = Integer.parseInt(server.substring(server.lastIndexOf(':') + 1));
if (port) {
assertEquals(11111, gotPort);
}
break;
}
}
if (i <= 0) {
fail();
}
Connection conn = getConnection(url + ";OPEN_NEW=TRUE");
Statement stat = conn.createStatement();
if (config.big) {
try {
stat.execute("SHUTDOWN");
} catch (SQLException e) {
assertKnownException(e);
// the connection is closed
}
}
conn.close();
connServer.close();
deleteDb("autoServer");
}
use of org.h2.util.SortedProperties in project h2database by h2database.
the class FileLock method lockSocket.
private void lockSocket() {
method = SOCKET;
properties = new SortedProperties();
properties.setProperty("method", String.valueOf(method));
setUniqueId();
// if this returns 127.0.0.1,
// the computer is probably not networked
ipAddress = NetUtils.getLocalAddress();
FileUtils.createDirectories(FileUtils.getParent(fileName));
if (!FileUtils.createFile(fileName)) {
waitUntilOld();
long read = FileUtils.lastModified(fileName);
Properties p2 = load();
String m2 = p2.getProperty("method", SOCKET);
if (m2.equals(FILE)) {
lockFile();
return;
} else if (!m2.equals(SOCKET)) {
throw getExceptionFatal("Unsupported lock method " + m2, null);
}
String ip = p2.getProperty("ipAddress", ipAddress);
if (!ipAddress.equals(ip)) {
throw getExceptionAlreadyInUse("Locked by another computer: " + ip);
}
String port = p2.getProperty("port", "0");
int portId = Integer.parseInt(port);
InetAddress address;
try {
address = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
throw getExceptionFatal("Unknown host " + ip, e);
}
for (int i = 0; i < 3; i++) {
try {
Socket s = new Socket(address, portId);
s.close();
throw getExceptionAlreadyInUse("Locked by another process");
} catch (BindException e) {
throw getExceptionFatal("Bind Exception", null);
} catch (ConnectException e) {
trace.debug(e, "socket not connected to port " + port);
} catch (IOException e) {
throw getExceptionFatal("IOException", null);
}
}
if (read != FileUtils.lastModified(fileName)) {
throw getExceptionFatal("Concurrent update", null);
}
FileUtils.delete(fileName);
if (!FileUtils.createFile(fileName)) {
throw getExceptionFatal("Another process was faster", null);
}
}
try {
// 0 to use any free port
serverSocket = NetUtils.createServerSocket(0, false);
int port = serverSocket.getLocalPort();
properties.setProperty("ipAddress", ipAddress);
properties.setProperty("port", String.valueOf(port));
} catch (Exception e) {
trace.debug(e, "lock");
serverSocket = null;
lockFile();
return;
}
save();
locked = true;
watchdog = new Thread(this, "H2 File Lock Watchdog (Socket) " + fileName);
watchdog.setDaemon(true);
watchdog.start();
}
use of org.h2.util.SortedProperties in project h2database by h2database.
the class FileLock method lockSerialized.
private void lockSerialized() {
method = SERIALIZED;
FileUtils.createDirectories(FileUtils.getParent(fileName));
if (FileUtils.createFile(fileName)) {
properties = new SortedProperties();
properties.setProperty("method", String.valueOf(method));
setUniqueId();
save();
} else {
while (true) {
try {
properties = load();
} catch (DbException e) {
// ignore
}
return;
}
}
}
Aggregations