Top Description Fields Constructors Methods
org.h2.server

public Class TcpServer

extends Object
implements Service
Class Inheritance
All Implemented Interfaces
org.h2.server.Service
Imports
java.io.IOException, java.net.ServerSocket, .Socket, .UnknownHostException, java.sql.PreparedStatement, .SQLException, .Statement, java.util.ArrayList, .Collections, .HashSet, .Set, java.util.concurrent.ConcurrentHashMap, org.h2.api.ErrorCode, org.h2.engine.Constants, org.h2.jdbc.JdbcConnection, org.h2.message.DbException, org.h2.util.MathUtils, .NetUtils, .StringUtils, .Tool, .Utils, .Utils10, .Utils21

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

Modifier and TypeField and Description
private boolean
private String
private boolean
private boolean
private String
private String
private Thread
private static final String
MANAGEMENT_DB_PREFIX

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
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
pack-priv synchronized void
addConnection(int
the connection id
id
,
String
the database URL
url
,
String
the user name
user
)

Add a connection to the management database.

pack-priv boolean

Returns:

true if this client may connect
allow
(Socket
the socket
socket
)

Check if this socket may connect to this server.

pack-priv void
cancelStatement(String
the session id
sessionId
,
int
the statement id
statementId
)

Cancel a running statement.

public String

Returns:

the database name
checkKeyAndGetDatabaseName
(String
the key to test (or database name if no key is used)
db
)

If no key is set, return the original database name.

public boolean
getAllowOthers()

Implements org.h2.server.Service.getAllowOthers.

Check if remote connections are allowed.

pack-priv String

Returns:

the base directory
getBaseDir
()

Get the configured base directory.

pack-priv boolean
public static String

Returns:

the database name (usually starting with mem:)
getManagementDbName
(int
the TCP server port
port
)

Get the database name of the management database.

public String
getName()

Implements org.h2.server.Service.getName.

Get the human readable name of the service.

public int
getPort()

Implements org.h2.server.Service.getPort.

Gets the port this service is listening on.

public boolean

Returns:

true if SSL socket is used, false if plain socket is used
getSSL
()

Returns whether a secure protocol is used.

public String
getType()

Implements org.h2.server.Service.getType.

Get the human readable short name of the service.

public String
getURL()

Implements org.h2.server.Service.getURL.

Get the URL of this service in a human readable form

public void
init(String...
the command line options
args
)

Implements org.h2.server.Service.init.

Initialize the service from command line options.

private void
public boolean
isDaemon()

Implements org.h2.server.Service.isDaemon.

Check if a daemon thread should be used.

public synchronized boolean
isRunning(boolean
if errors should be written
traceError
)

Implements org.h2.server.Service.isRunning.

Check if the service is running.

public void
listen()

Implements org.h2.server.Service.listen.

Listen for incoming connections.

pack-priv void
remove(TcpServerThread
the thread to remove
t
)

Remove a thread from the list.

pack-priv synchronized void
removeConnection(int
the connection id
id
)

Remove a connection from the management database.

public void
pack-priv void
shutdown()

Shut down this server.

public static synchronized void
shutdown(String
the database URL
url
,
String
the password
password
,
boolean
if the server should be stopped immediately
force
,
boolean
whether all TCP servers that are running in the JVM should be stopped
all
)

Stop the TCP server with the given URL.

public synchronized void
start()

Implements org.h2.server.Service.start.

Start the service.

public void
stop()

Implements org.h2.server.Service.stop.

Stop the service.

private synchronized void
public static void
stopServer(int
the port where the server runs, or 0 for all running servers
port
,
String
the password (or null)
password
,
int
the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.
shutdownMode
)

Stop a running server.

pack-priv void
trace(String
the message
s
)

Print a message if the trace flag is enabled.

pack-priv void
traceError(Throwable
the exception
e
)

Print a stack trace if the trace flag is enabled.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowOthersback to summary
private boolean allowOthers
baseDirback to summary
private String baseDir
ifExistsback to summary
private boolean ifExists
isDaemonback to summary
private boolean isDaemon
keyback to summary
private String key
keyDatabaseback to summary
private String keyDatabase
listenerThreadback to summary
private Thread listenerThread
MANAGEMENT_DB_PREFIXback to summary
private static final String MANAGEMENT_DB_PREFIX

The name of the in-memory management database used by the TCP server to keep the active sessions.

managementDbback to summary
private JdbcConnection managementDb
managementDbAddback to summary
private PreparedStatement managementDbAdd
managementDbRemoveback to summary
private PreparedStatement managementDbRemove
managementPasswordback to summary
private String managementPassword
nextThreadIdback to summary
private int nextThreadId
portback to summary
private int port
portIsSetback to summary
private boolean portIsSet
runningback to summary
private final Set<TcpServerThread> running
SERVERSback to summary
private static final ConcurrentHashMap<Integer, TcpServer> SERVERS
serverSocketback to summary
private ServerSocket serverSocket
SHUTDOWN_FORCEback to summary
private static final int SHUTDOWN_FORCE
SHUTDOWN_NORMALback to summary
private static final int SHUTDOWN_NORMAL
shutdownHandlerback to summary
private ShutdownHandler shutdownHandler
sslback to summary
private boolean ssl
stopback to summary
private boolean stop
traceback to summary
private boolean trace
virtualThreadsback to summary
private boolean virtualThreads

Constructor Detail

TcpServerback to summary
public TcpServer()

Method Detail

addConnectionback to summary
pack-priv synchronized void addConnection(int id, String url, String user)

Add a connection to the management database.

Parameters
id:int

the connection id

url:String

the database URL

user:String

the user name

allowback to summary
pack-priv boolean allow(Socket socket)

Check if this socket may connect to this server. Remote connections are not allowed if the flag allowOthers is set.

Parameters
socket:Socket

the socket

Returns:boolean

true if this client may connect

cancelStatementback to summary
pack-priv void cancelStatement(String sessionId, int statementId)

Cancel a running statement.

Parameters
sessionId:String

the session id

statementId:int

the statement id

checkKeyAndGetDatabaseNameback to summary
public String checkKeyAndGetDatabaseName(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:String

the key to test (or database name if no key is used)

Returns:String

the database name

Exceptions
DbException:
if a key is set but doesn't match
getAllowOthersback to summary
public boolean getAllowOthers()

Implements org.h2.server.Service.getAllowOthers.

Doc from org.h2.server.Service.getAllowOthers.

Check if remote connections are allowed.

Returns:boolean

true if remote connections are allowed

Annotations
@Override
getBaseDirback to summary
pack-priv String getBaseDir()

Get the configured base directory.

Returns:String

the base directory

getIfExistsback to summary
pack-priv boolean getIfExists()
getManagementDbNameback to summary
public static String getManagementDbName(int port)

Get the database name of the management database. The management database contains a table with active sessions (SESSIONS).

Parameters
port:int

the TCP server port

Returns:String

the database name (usually starting with mem:)

getNameback to summary
public String getName()

Implements org.h2.server.Service.getName.

Doc from org.h2.server.Service.getName.

Get the human readable name of the service.

Returns:String

the name

Annotations
@Override
getPortback to summary
public int getPort()

Implements org.h2.server.Service.getPort.

Doc from org.h2.server.Service.getPort.

Gets the port this service is listening on.

Returns:int

the port

Annotations
@Override
getSSLback to summary
public boolean getSSL()

Returns whether a secure protocol is used.

Returns:boolean

true if SSL socket is used, false if plain socket is used

getTypeback to summary
public String getType()

Implements org.h2.server.Service.getType.

Doc from org.h2.server.Service.getType.

Get the human readable short name of the service.

Returns:String

the type

Annotations
@Override
getURLback to summary
public String getURL()

Implements org.h2.server.Service.getURL.

Doc from org.h2.server.Service.getURL.

Get the URL of this service in a human readable form

Returns:String

the url

Annotations
@Override
initback to summary
public void init(String... args)

Implements org.h2.server.Service.init.

Doc from org.h2.server.Service.init.

Initialize the service from command line options.

Parameters
args:String[]

the command line options

Annotations
@Override
initManagementDbback to summary
private void initManagementDb() throws SQLException
isDaemonback to summary
public boolean isDaemon()

Implements org.h2.server.Service.isDaemon.

Doc from org.h2.server.Service.isDaemon.

Check if a daemon thread should be used.

Returns:boolean

true if a daemon thread should be used

Annotations
@Override
isRunningback to summary
public synchronized boolean isRunning(boolean traceError)

Implements org.h2.server.Service.isRunning.

Doc from org.h2.server.Service.isRunning.

Check if the service is running.

Parameters
traceError:boolean

if errors should be written

Returns:boolean

if the server is running

Annotations
@Override
listenback to summary
public void listen()

Implements org.h2.server.Service.listen.

Doc from org.h2.server.Service.listen.

Listen for incoming connections. This method blocks.

Annotations
@Override
removeback to summary
pack-priv void remove(TcpServerThread t)

Remove a thread from the list.

Parameters
t:TcpServerThread

the thread to remove

removeConnectionback to summary
pack-priv synchronized void removeConnection(int id)

Remove a connection from the management database.

Parameters
id:int

the connection id

setShutdownHandlerback to summary
public void setShutdownHandler(ShutdownHandler shutdownHandler)
shutdownback to summary
pack-priv void shutdown()

Shut down this server.

shutdownback to summary
public static synchronized void shutdown(String url, String password, boolean force, boolean all) throws SQLException

Stop the TCP server with the given URL.

Parameters
url:String

the database URL

password:String

the password

force:boolean

if the server should be stopped immediately

all:boolean

whether all TCP servers that are running in the JVM should be stopped

Exceptions
SQLException:
on failure
startback to summary
public synchronized void start() throws SQLException

Implements org.h2.server.Service.start.

Doc from org.h2.server.Service.start.

Start the service. This usually means create the server socket. This method must not block.

Annotations
@Override
Exceptions
SQLException:
on failure
stopback to summary
public void stop()

Implements org.h2.server.Service.stop.

Doc from org.h2.server.Service.stop.

Stop the service.

Annotations
@Override
stopManagementDbback to summary
private synchronized void stopManagementDb()
stopServerback to summary
public static void stopServer(int port, String password, int shutdownMode)

Stop a running server. This method is called via reflection from the STOP_SERVER function.

Parameters
port:int

the port where the server runs, or 0 for all running servers

password:String

the password (or null)

shutdownMode:int

the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.

traceback to summary
pack-priv void trace(String s)

Print a message if the trace flag is enabled.

Parameters
s:String

the message

traceErrorback to summary
pack-priv void traceError(Throwable e)

Print a stack trace if the trace flag is enabled.

Parameters
e:Throwable

the exception