提交 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,23 +111,27 @@ public class TcpServer implements Service { ...@@ -109,23 +111,27 @@ 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) {
try { synchronized (TcpServer.class) {
managementDbAdd.setInt(1, id); try {
managementDbAdd.setString(2, url); managementDbAdd.setInt(1, id);
managementDbAdd.setString(3, user); managementDbAdd.setString(2, url);
managementDbAdd.execute(); managementDbAdd.setString(3, user);
} catch (SQLException e) { managementDbAdd.execute();
TraceSystem.traceThrowable(e); } catch (SQLException e) {
TraceSystem.traceThrowable(e);
}
} }
} }
synchronized void removeConnection(int id) { void removeConnection(int id) {
try { synchronized (TcpServer.class) {
managementDbRemove.setInt(1, id); try {
managementDbRemove.execute(); managementDbRemove.setInt(1, id);
} catch (SQLException e) { managementDbRemove.execute();
TraceSystem.traceThrowable(e); } catch (SQLException e) {
TraceSystem.traceThrowable(e);
}
} }
} }
...@@ -219,39 +225,41 @@ public class TcpServer implements Service { ...@@ -219,39 +225,41 @@ 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
if (!stop) { synchronized (TcpServer.class) {
stopManagementDb(); if (!stop) {
stop = true; stopManagementDb();
if (serverSocket != null) { stop = true;
try { if (serverSocket != null) {
serverSocket.close(); try {
} catch (IOException e) { serverSocket.close();
TraceSystem.traceThrowable(e); } catch (IOException e) {
TraceSystem.traceThrowable(e);
}
serverSocket = null;
}
if (listenerThread != null) {
try {
listenerThread.join(1000);
} catch (InterruptedException e) {
TraceSystem.traceThrowable(e);
}
} }
serverSocket = null;
} }
if (listenerThread != null) { // TODO server: using a boolean 'now' argument? a timeout?
ArrayList list = new ArrayList(running);
for (int i = 0; i < list.size(); i++) {
TcpServerThread c = (TcpServerThread) list.get(i);
c.close();
try { try {
listenerThread.join(1000); c.getThread().join(100);
} catch (InterruptedException e) { } catch (Exception e) {
TraceSystem.traceThrowable(e); TraceSystem.traceThrowable(e);
} }
} }
SERVERS.remove("" + port);
} }
// TODO server: using a boolean 'now' argument? a timeout?
ArrayList list = new ArrayList(running);
for (int i = 0; i < list.size(); i++) {
TcpServerThread c = (TcpServerThread) list.get(i);
c.close();
try {
c.getThread().join(100);
} catch (Exception e) {
TraceSystem.traceThrowable(e);
}
}
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) {
...@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论