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

--no commit message

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