Package org.h2.server
Class TcpServer
java.lang.Object
org.h2.server.TcpServer
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionprivate boolean
private String
private boolean
private boolean
private String
private String
private Thread
private static final String
The name of the in-memory management database used by the TCP server to keep the active sessions.private JdbcConnection
private PreparedStatement
private PreparedStatement
private String
private int
private int
private boolean
private final Set
<TcpServerThread> private static final ConcurrentHashMap
<Integer, TcpServer> private ServerSocket
private static final int
private static final int
private ShutdownHandler
private boolean
private boolean
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) void
addConnection
(int id, String url, String user) Add a connection to the management database.(package private) boolean
Check if this socket may connect to this server.(package private) void
cancelStatement
(String sessionId, int statementId) Cancel a running statement.If no key is set, return the original database name.boolean
Check if remote connections are allowed.(package private) String
Get the configured base directory.(package private) boolean
static String
getManagementDbName
(int port) Get the database name of the management database.getName()
Get the human readable name of the service.int
getPort()
Gets the port this service is listening on.boolean
getSSL()
Returns whether a secure protocol is used.getType()
Get the human readable short name of the service.getURL()
Get the URL of this service in a human readable formvoid
Initialize the service from command line options.private void
boolean
isDaemon()
Check if a daemon thread should be used.boolean
isRunning
(boolean traceError) Check if the service is running.void
listen()
Listen for incoming connections.(package private) void
Remove a thread from the list.(package private) void
removeConnection
(int id) Remove a connection from the management database.void
setShutdownHandler
(ShutdownHandler shutdownHandler) (package private) void
shutdown()
Shut down this server.static void
Stop the TCP server with the given URL.void
start()
Start the service.void
stop()
Stop the service.private void
static void
stopServer
(int port, String password, int shutdownMode) Stop a running server.(package private) void
Print a message if the trace flag is enabled.(package private) void
Print a stack trace if the trace flag is enabled.
-
Field Details
-
SHUTDOWN_NORMAL
private static final int SHUTDOWN_NORMAL- See Also:
-
SHUTDOWN_FORCE
private static final int SHUTDOWN_FORCE- See Also:
-
MANAGEMENT_DB_PREFIX
The name of the in-memory management database used by the TCP server to keep the active sessions.- See Also:
-
SERVERS
-
port
private int port -
portIsSet
private boolean portIsSet -
trace
private boolean trace -
ssl
private boolean ssl -
stop
private boolean stop -
shutdownHandler
-
serverSocket
-
running
-
baseDir
-
allowOthers
private boolean allowOthers -
isDaemon
private boolean isDaemon -
ifExists
private boolean ifExists -
managementDb
-
managementDbAdd
-
managementDbRemove
-
managementPassword
-
listenerThread
-
nextThreadId
private int nextThreadId -
key
-
keyDatabase
-
-
Constructor Details
-
TcpServer
public TcpServer()
-
-
Method Details
-
getManagementDbName
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
- Throws:
SQLException
-
shutdown
void shutdown()Shut down this server. -
setShutdownHandler
-
addConnection
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
Description copied from interface:Service
Initialize the service from command line options. -
getURL
Description copied from interface:Service
Get the URL of this service in a human readable form -
getPort
public int getPort()Description copied from interface:Service
Gets the port this service is listening on. -
getSSL
public boolean getSSL()Returns whether a secure protocol is used.- Returns:
true
if SSL socket is used,false
if plain socket is used
-
allow
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
Description copied from interface:Service
Start the service. This usually means create the server socket. This method must not block.- Specified by:
start
in interfaceService
- Throws:
SQLException
- on failure
-
listen
public void listen()Description copied from interface:Service
Listen for incoming connections. This method blocks. -
isRunning
public boolean isRunning(boolean traceError) Description copied from interface:Service
Check if the service is running. -
stop
public void stop()Description copied from interface:Service
Stop the service. -
stopServer
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
Remove a thread from the list.- Parameters:
t
- the thread to remove
-
getBaseDir
String getBaseDir()Get the configured base directory.- Returns:
- the base directory
-
trace
Print a message if the trace flag is enabled.- Parameters:
s
- the message
-
traceError
Print a stack trace if the trace flag is enabled.- Parameters:
e
- the exception
-
getAllowOthers
public boolean getAllowOthers()Description copied from interface:Service
Check if remote connections are allowed.- Specified by:
getAllowOthers
in interfaceService
- Returns:
- true if remote connections are allowed
-
getType
Description copied from interface:Service
Get the human readable short name of the service. -
getName
Description copied from interface:Service
Get the human readable name of the service. -
getIfExists
boolean getIfExists() -
shutdown
public static void shutdown(String url, String password, boolean force, boolean all) throws SQLException Stop 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:
SQLException
- on failure
-
cancelStatement
Cancel a running statement.- Parameters:
sessionId
- the session idstatementId
- the statement id
-
checkKeyAndGetDatabaseName
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
-
isDaemon
public boolean isDaemon()Description copied from interface:Service
Check if a daemon thread should be used.
-