Package org.h2.server
Class TcpServer
- java.lang.Object
-
- org.h2.server.TcpServer
-
- All Implemented Interfaces:
Service
public class TcpServer extends java.lang.Object implements Service
The TCP server implements the native H2 database server protocol. It supports multiple client connections to multiple databases (many to many). The same database may be opened by multiple clients. Also supported is the mixed mode: opening databases in embedded mode, and at the same time start a TCP server to allow clients to connect to the same database over the network.
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowOthersprivate java.lang.StringbaseDirprivate booleanifExistsprivate booleanisDaemonprivate java.lang.Stringkeyprivate java.lang.StringkeyDatabaseprivate java.lang.ThreadlistenerThreadprivate static java.lang.StringMANAGEMENT_DB_PREFIXThe name of the in-memory management database used by the TCP server to keep the active sessions.private JdbcConnectionmanagementDbprivate java.sql.PreparedStatementmanagementDbAddprivate java.sql.PreparedStatementmanagementDbRemoveprivate java.lang.StringmanagementPasswordprivate intnextThreadIdprivate intportprivate booleanportIsSetprivate java.util.Set<TcpServerThread>runningprivate static java.util.concurrent.ConcurrentHashMap<java.lang.Integer,TcpServer>SERVERSprivate java.net.ServerSocketserverSocketprivate static intSHUTDOWN_FORCEprivate static intSHUTDOWN_NORMALprivate ShutdownHandlershutdownHandlerprivate booleansslprivate booleanstopprivate booleantrace
-
Constructor Summary
Constructors Constructor Description TcpServer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidaddConnection(int id, java.lang.String url, java.lang.String user)Add a connection to the management database.(package private) booleanallow(java.net.Socket socket)Check if this socket may connect to this server.(package private) voidcancelStatement(java.lang.String sessionId, int statementId)Cancel a running statement.java.lang.StringcheckKeyAndGetDatabaseName(java.lang.String db)If no key is set, return the original database name.booleangetAllowOthers()Check if remote connections are allowed.(package private) java.lang.StringgetBaseDir()Get the configured base directory.(package private) booleangetIfExists()static java.lang.StringgetManagementDbName(int port)Get the database name of the management database.java.lang.StringgetName()Get the human readable name of the service.intgetPort()Gets the port this service is listening on.booleangetSSL()Returns whether a secure protocol is used.java.lang.StringgetType()Get the human readable short name of the service.java.lang.StringgetURL()Get the URL of this service in a human readable formvoidinit(java.lang.String... args)Initialize the service from command line options.private voidinitManagementDb()booleanisDaemon()Check if a daemon thread should be used.booleanisRunning(boolean traceError)Check if the service is running.voidlisten()Listen for incoming connections.(package private) voidremove(TcpServerThread t)Remove a thread from the list.(package private) voidremoveConnection(int id)Remove a connection from the management database.voidsetShutdownHandler(ShutdownHandler shutdownHandler)(package private) voidshutdown()Shut down this server.static voidshutdown(java.lang.String url, java.lang.String password, boolean force, boolean all)Stop the TCP server with the given URL.voidstart()Start the service.voidstop()Stop the service.private voidstopManagementDb()static voidstopServer(int port, java.lang.String password, int shutdownMode)Stop a running server.(package private) voidtrace(java.lang.String s)Print a message if the trace flag is enabled.(package private) voidtraceError(java.lang.Throwable e)Print a stack trace if the trace flag is enabled.
-
-
-
Field Detail
-
SHUTDOWN_NORMAL
private static final int SHUTDOWN_NORMAL
- See Also:
- Constant Field Values
-
SHUTDOWN_FORCE
private static final int SHUTDOWN_FORCE
- See Also:
- Constant Field Values
-
MANAGEMENT_DB_PREFIX
private static final java.lang.String MANAGEMENT_DB_PREFIX
The name of the in-memory management database used by the TCP server to keep the active sessions.- See Also:
- Constant Field Values
-
SERVERS
private static final java.util.concurrent.ConcurrentHashMap<java.lang.Integer,TcpServer> SERVERS
-
port
private int port
-
portIsSet
private boolean portIsSet
-
trace
private boolean trace
-
ssl
private boolean ssl
-
stop
private boolean stop
-
shutdownHandler
private ShutdownHandler shutdownHandler
-
serverSocket
private java.net.ServerSocket serverSocket
-
running
private final java.util.Set<TcpServerThread> running
-
baseDir
private java.lang.String baseDir
-
allowOthers
private boolean allowOthers
-
isDaemon
private boolean isDaemon
-
ifExists
private boolean ifExists
-
managementDb
private JdbcConnection managementDb
-
managementDbAdd
private java.sql.PreparedStatement managementDbAdd
-
managementDbRemove
private java.sql.PreparedStatement managementDbRemove
-
managementPassword
private java.lang.String managementPassword
-
listenerThread
private java.lang.Thread listenerThread
-
nextThreadId
private int nextThreadId
-
key
private java.lang.String key
-
keyDatabase
private java.lang.String keyDatabase
-
-
Method Detail
-
getManagementDbName
public static java.lang.String getManagementDbName(int port)
Get the database name of the management database. The management database contains a table with active sessions (SESSIONS).- Parameters:
port- the TCP server port- Returns:
- the database name (usually starting with mem:)
-
initManagementDb
private void initManagementDb() throws java.sql.SQLException- Throws:
java.sql.SQLException
-
shutdown
void shutdown()
Shut down this server.
-
setShutdownHandler
public void setShutdownHandler(ShutdownHandler shutdownHandler)
-
addConnection
void addConnection(int id, java.lang.String url, java.lang.String user)Add a connection to the management database.- Parameters:
id- the connection idurl- the database URLuser- the user name
-
removeConnection
void removeConnection(int id)
Remove a connection from the management database.- Parameters:
id- the connection id
-
stopManagementDb
private void stopManagementDb()
-
init
public void init(java.lang.String... args)
Description copied from interface:ServiceInitialize the service from command line options.
-
getURL
public java.lang.String getURL()
Description copied from interface:ServiceGet the URL of this service in a human readable form
-
getPort
public int getPort()
Description copied from interface:ServiceGets the port this service is listening on.
-
getSSL
public boolean getSSL()
Returns whether a secure protocol is used.- Returns:
trueif SSL socket is used,falseif plain socket is used
-
allow
boolean allow(java.net.Socket socket)
Check if this socket may connect to this server. Remote connections are not allowed if the flag allowOthers is set.- Parameters:
socket- the socket- Returns:
- true if this client may connect
-
start
public void start() throws java.sql.SQLExceptionDescription copied from interface:ServiceStart the service. This usually means create the server socket. This method must not block.
-
listen
public void listen()
Description copied from interface:ServiceListen for incoming connections. This method blocks.
-
isRunning
public boolean isRunning(boolean traceError)
Description copied from interface:ServiceCheck if the service is running.
-
stop
public void stop()
Description copied from interface:ServiceStop the service.
-
stopServer
public static void stopServer(int port, java.lang.String password, int shutdownMode)Stop a running server. This method is called via reflection from the STOP_SERVER function.- Parameters:
port- the port where the server runs, or 0 for all running serverspassword- the password (or null)shutdownMode- the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.
-
remove
void remove(TcpServerThread t)
Remove a thread from the list.- Parameters:
t- the thread to remove
-
getBaseDir
java.lang.String getBaseDir()
Get the configured base directory.- Returns:
- the base directory
-
trace
void trace(java.lang.String s)
Print a message if the trace flag is enabled.- Parameters:
s- the message
-
traceError
void traceError(java.lang.Throwable e)
Print a stack trace if the trace flag is enabled.- Parameters:
e- the exception
-
getAllowOthers
public boolean getAllowOthers()
Description copied from interface:ServiceCheck if remote connections are allowed.- Specified by:
getAllowOthersin interfaceService- Returns:
- true if remote connections are allowed
-
getType
public java.lang.String getType()
Description copied from interface:ServiceGet the human readable short name of the service.
-
getName
public java.lang.String getName()
Description copied from interface:ServiceGet the human readable name of the service.
-
getIfExists
boolean getIfExists()
-
shutdown
public static void shutdown(java.lang.String url, java.lang.String password, boolean force, boolean all) throws java.sql.SQLExceptionStop the TCP server with the given URL.- Parameters:
url- the database URLpassword- the passwordforce- if the server should be stopped immediatelyall- whether all TCP servers that are running in the JVM should be stopped- Throws:
java.sql.SQLException- on failure
-
cancelStatement
void cancelStatement(java.lang.String sessionId, int statementId)Cancel a running statement.- Parameters:
sessionId- the session idstatementId- the statement id
-
checkKeyAndGetDatabaseName
public java.lang.String checkKeyAndGetDatabaseName(java.lang.String db)
If no key is set, return the original database name. If a key is set, check if the key matches. If yes, return the correct database name. If not, throw an exception.- Parameters:
db- the key to test (or database name if no key is used)- Returns:
- the database name
- Throws:
DbException- if a key is set but doesn't match
-
-