提交 93698a6d authored 作者: Thomas Mueller's avatar Thomas Mueller

--no commit message

--no commit message
上级 18bb2a2e
......@@ -8,6 +8,7 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
......@@ -21,6 +22,7 @@ import java.util.Set;
import org.h2.Driver;
import org.h2.engine.Constants;
import org.h2.message.Message;
import org.h2.message.TraceSystem;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
......@@ -109,7 +111,8 @@ public class TcpServer implements Service {
return ports;
}
synchronized void addConnection(int id, String url, String user) {
void addConnection(int id, String url, String user) {
synchronized (TcpServer.class) {
try {
managementDbAdd.setInt(1, id);
managementDbAdd.setString(2, url);
......@@ -119,8 +122,10 @@ public class TcpServer implements Service {
TraceSystem.traceThrowable(e);
}
}
}
synchronized void removeConnection(int id) {
void removeConnection(int id) {
synchronized (TcpServer.class) {
try {
managementDbRemove.setInt(1, id);
managementDbRemove.execute();
......@@ -128,6 +133,7 @@ public class TcpServer implements Service {
TraceSystem.traceThrowable(e);
}
}
}
private void stopManagementDb() {
synchronized (TcpServer.class) {
......@@ -219,8 +225,9 @@ public class TcpServer implements Service {
}
}
public synchronized void stop() {
public void stop() {
// TODO server: share code between web and tcp servers
synchronized (TcpServer.class) {
if (!stop) {
stopManagementDb();
stop = true;
......@@ -253,6 +260,7 @@ public class TcpServer implements Service {
}
SERVERS.remove("" + port);
}
}
public static synchronized void stopServer(int port, String password, int shutdownMode) {
TcpServer server = (TcpServer) SERVERS.get("" + port);
......@@ -276,7 +284,7 @@ public class TcpServer implements Service {
}
}
synchronized void remove(TcpServerThread t) {
void remove(TcpServerThread t) {
running.remove(t);
}
......@@ -320,4 +328,51 @@ public class TcpServer implements Service {
return ifExists;
}
public static synchronized void shutdown(String url, String password, boolean force) throws SQLException {
int port = Constants.DEFAULT_SERVER_PORT;
int idx = url.indexOf(':', "jdbc:h2:".length());
if (idx >= 0) {
String p = url.substring(idx + 1);
idx = p.indexOf('/');
if (idx >= 0) {
p = p.substring(0, idx);
}
port = MathUtils.decodeInt(p);
}
String db = TcpServer.getManagementDbName(port);
try {
org.h2.Driver.load();
} catch (Throwable e) {
throw Message.convert(e);
}
for (int i = 0; i < 2; i++) {
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DriverManager.getConnection("jdbc:h2:" + url + "/" + db, "sa", password);
prep = conn.prepareStatement("CALL STOP_SERVER(?, ?, ?)");
prep.setInt(1, port);
prep.setString(2, password);
prep.setInt(3, force ? TcpServer.SHUTDOWN_FORCE : TcpServer.SHUTDOWN_NORMAL);
try {
prep.execute();
} catch (SQLException e) {
if (force) {
// ignore
} else {
throw e;
}
}
break;
} catch (SQLException e) {
if (i == 1) {
throw e;
}
} finally {
JdbcUtils.closeSilently(prep);
JdbcUtils.closeSilently(conn);
}
}
}
}
......@@ -5,9 +5,6 @@
package org.h2.tools;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.h2.constant.ErrorCode;
......@@ -20,8 +17,6 @@ import org.h2.server.TcpServer;
import org.h2.server.ftp.FtpServer;
import org.h2.server.pg.PgServer;
import org.h2.server.web.WebServer;
import org.h2.util.JdbcUtils;
import org.h2.util.MathUtils;
import org.h2.util.StartBrowser;
/**
......@@ -309,50 +304,7 @@ public class Server implements Runnable, ShutdownHandler {
* @throws SQLException
*/
public static void shutdownTcpServer(String url, String password, boolean force) throws SQLException {
int port = Constants.DEFAULT_SERVER_PORT;
int idx = url.indexOf(':', "jdbc:h2:".length());
if (idx >= 0) {
String p = url.substring(idx + 1);
idx = p.indexOf('/');
if (idx >= 0) {
p = p.substring(0, idx);
}
port = MathUtils.decodeInt(p);
}
String db = TcpServer.getManagementDbName(port);
try {
org.h2.Driver.load();
} catch (Throwable e) {
throw Message.convert(e);
}
for (int i = 0; i < 2; i++) {
Connection conn = null;
PreparedStatement prep = null;
try {
conn = DriverManager.getConnection("jdbc:h2:" + url + "/" + db, "sa", password);
prep = conn.prepareStatement("CALL STOP_SERVER(?, ?, ?)");
prep.setInt(1, port);
prep.setString(2, password);
prep.setInt(3, force ? TcpServer.SHUTDOWN_FORCE : TcpServer.SHUTDOWN_NORMAL);
try {
prep.execute();
} catch (SQLException e) {
if (force) {
// ignore
} else {
throw e;
}
}
break;
} catch (SQLException e) {
if (i == 1) {
throw e;
}
} finally {
JdbcUtils.closeSilently(prep);
JdbcUtils.closeSilently(conn);
}
}
TcpServer.shutdown(url, password, force);
}
String getStatus() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论