提交 d42b9406 authored 作者: Thomas Mueller's avatar Thomas Mueller

In-memory databases can now run inside the Google App Engine.

上级 9b21163b
......@@ -222,8 +222,8 @@ public class Database implements DataHandler {
try {
open(traceLevelFile, traceLevelSystemOut);
if (closeAtVmShutdown) {
closeOnExit = new DatabaseCloser(this, 0, true);
try {
closeOnExit = new DatabaseCloser(this, 0, true);
Runtime.getRuntime().addShutdownHook(closeOnExit);
} catch (IllegalStateException e) {
// shutdown in progress - just don't register the handler
......@@ -231,6 +231,8 @@ public class Database implements DataHandler {
// database at shutdown time)
} catch (SecurityException e) {
// applets may not do that - ignore
// Google App Engine doesn't allow
// to instantiate classes that extend Thread
}
}
} catch (Throwable e) {
......
......@@ -25,7 +25,7 @@ import org.h2.util.ObjectArray;
* The writer thread is responsible to flush the transaction log file from time
* to time.
*/
public class WriterThread extends Thread {
public class WriterThread implements Runnable {
/**
* The reference to the database.
......@@ -74,11 +74,12 @@ public class WriterThread extends Thread {
* @return the writer thread object
*/
public static WriterThread create(Database database, int writeDelay) {
WriterThread thread = new WriterThread(database, writeDelay);
WriterThread writer = new WriterThread(database, writeDelay);
Thread thread = new Thread(writer);
thread.setName("H2 Log Writer " + database.getShortName());
thread.setDaemon(true);
thread.start();
return thread;
return writer;
}
private LogSystem getLog() {
......
......@@ -10,7 +10,6 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
......@@ -65,6 +64,7 @@ public class RandomUtils {
}
}
};
try {
Thread t = new Thread(runnable);
// let the process terminate even if generating the seed is really slow
t.setDaemon(true);
......@@ -83,6 +83,12 @@ public class RandomUtils {
cachedSecureRandom.setSeed(seed);
}
}
} catch (SecurityException e) {
// workaround for the Google App Engine: don't use a thread
runnable.run();
generateAlternativeSeed();
}
} catch (NoSuchAlgorithmException e) {
warn("SecureRandom", e);
cachedSecureRandom = new SecureRandom();
......@@ -125,18 +131,27 @@ public class RandomUtils {
// host name and ip addresses (if any)
try {
String hostName = InetAddress.getLocalHost().getHostName();
// workaround for the Google App Engine: don't use InetAddress
Class inetAddressClass = Class.forName("java.net.InetAddress");
Object localHost = inetAddressClass.getMethod(
"getLocalHost", new Class[0]).invoke(null, new Object[0]);
String hostName = inetAddressClass.getMethod(
"getHostName", new Class[0]).invoke(localHost, new Object[0]).toString();
out.writeUTF(hostName);
InetAddress[] list = InetAddress.getAllByName(hostName);
Object[] list = (Object[]) inetAddressClass.getMethod(
"getAllByName", new Class[] { String.class })
.invoke(null, new Object[] { hostName });
Method getAddress = inetAddressClass.getMethod("getAddress", new Class[0]);
for (int i = 0; i < list.length; i++) {
out.write(list[i].getAddress());
out.write((byte[]) getAddress.invoke(list[i], new Object[0]));
}
} catch (Exception e) {
} catch (Throwable e) {
// on some system, InetAddress is not supported
// on some system, InetAddress.getLocalHost() doesn't work
// for some reason (incorrect configuration)
}
// timing (a second thread is already running)
// timing (a second thread is already running usually)
for (int j = 0; j < 16; j++) {
int i = 0;
long end = System.currentTimeMillis();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论