Package org.h2.server

Class 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.
    • Constructor Summary

      Constructors 
      Constructor Description
      TcpServer()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String checkKeyAndGetDatabaseName​(java.lang.String db)
      If no key is set, return the original database name.
      boolean getAllowOthers()
      Check if remote connections are allowed.
      static java.lang.String getManagementDbName​(int port)
      Get the database name of the management database.
      java.lang.String 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.
      java.lang.String getType()
      Get the human readable short name of the service.
      java.lang.String getURL()
      Get the URL of this service in a human readable form
      void init​(java.lang.String... args)
      Initialize the service from command line options.
      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.
      void setShutdownHandler​(ShutdownHandler shutdownHandler)  
      static void shutdown​(java.lang.String url, java.lang.String password, boolean force, boolean all)
      Stop the TCP server with the given URL.
      void start()
      Start the service.
      void stop()
      Stop the service.
      static void stopServer​(int port, java.lang.String password, int shutdownMode)
      Stop a running server.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TcpServer

        public TcpServer()
    • 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:)
      • setShutdownHandler

        public void setShutdownHandler​(ShutdownHandler shutdownHandler)
      • init

        public void init​(java.lang.String... args)
        Description copied from interface: Service
        Initialize the service from command line options.
        Specified by:
        init in interface Service
        Parameters:
        args - the command line options
      • getURL

        public java.lang.String getURL()
        Description copied from interface: Service
        Get the URL of this service in a human readable form
        Specified by:
        getURL in interface Service
        Returns:
        the url
      • getPort

        public int getPort()
        Description copied from interface: Service
        Gets the port this service is listening on.
        Specified by:
        getPort in interface Service
        Returns:
        the port
      • getSSL

        public boolean getSSL()
        Returns whether a secure protocol is used.
        Returns:
        true if SSL socket is used, false if plain socket is used
      • start

        public void start()
                   throws java.sql.SQLException
        Description copied from interface: Service
        Start the service. This usually means create the server socket. This method must not block.
        Specified by:
        start in interface Service
        Throws:
        java.sql.SQLException - on failure
      • listen

        public void listen()
        Description copied from interface: Service
        Listen for incoming connections. This method blocks.
        Specified by:
        listen in interface Service
      • isRunning

        public boolean isRunning​(boolean traceError)
        Description copied from interface: Service
        Check if the service is running.
        Specified by:
        isRunning in interface Service
        Parameters:
        traceError - if errors should be written
        Returns:
        if the server is running
      • stop

        public void stop()
        Description copied from interface: Service
        Stop the service.
        Specified by:
        stop in interface 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 servers
        password - the password (or null)
        shutdownMode - the shutdown mode, SHUTDOWN_NORMAL or SHUTDOWN_FORCE.
      • getAllowOthers

        public boolean getAllowOthers()
        Description copied from interface: Service
        Check if remote connections are allowed.
        Specified by:
        getAllowOthers in interface Service
        Returns:
        true if remote connections are allowed
      • getType

        public java.lang.String getType()
        Description copied from interface: Service
        Get the human readable short name of the service.
        Specified by:
        getType in interface Service
        Returns:
        the type
      • getName

        public java.lang.String getName()
        Description copied from interface: Service
        Get the human readable name of the service.
        Specified by:
        getName in interface Service
        Returns:
        the name
      • shutdown

        public static void shutdown​(java.lang.String url,
                                    java.lang.String password,
                                    boolean force,
                                    boolean all)
                             throws java.sql.SQLException
        Stop the TCP server with the given URL.
        Parameters:
        url - the database URL
        password - the password
        force - if the server should be stopped immediately
        all - whether all TCP servers that are running in the JVM should be stopped
        Throws:
        java.sql.SQLException - on failure
      • 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
      • isDaemon

        public boolean isDaemon()
        Description copied from interface: Service
        Check if a daemon thread should be used.
        Specified by:
        isDaemon in interface Service
        Returns:
        true if a daemon thread should be used