Class ConnectionUtils

java.lang.Object
org.gridgain.internal.mcp.utils.ConnectionUtils

public class ConnectionUtils extends Object
Utility class for managing a shared IgniteClient instance across the MCP server lifecycle. This class implements a singleton pattern for IgniteClient. Ensures efficient resource utilization and connection pooling. The shared client is initialized once during server startup and reused across all tool invocations.
  • Constructor Details

    • ConnectionUtils

      public ConnectionUtils()
  • Method Details

    • initialize

      public static void initialize(ConnectionConfig config)
      Initializes the shared IgniteClient with the provided configuration.

      This method should be called exactly once during server startup, before any tools attempt to access the client. The client is configured with:

      • Server addresses (host:port)
      • Connection timeout
      • Basic authentication (if credentials provided)
      • SSL/TLS encryption (if enabled)

      Subsequent calls to this method will be ignored with a warning logged.

      Parameters:
      config - connection configuration containing host, port, credentials, and SSL settings
      Throws:
      IllegalArgumentException - if SSL is enabled but trust store path is not configured
      IllegalStateException - if client initialization fails due to invalid configuration or network issues
    • shutdown

      public static void shutdown()
      Closes the shared IgniteClient and releases all associated resources.

      This method should be called during server shutdown (typically in a shutdown hook) to ensure graceful cleanup of database connections and prevent resource leaks.

      After calling this method, the client instance is set to null, and any subsequent calls to getSharedClient() will throw IllegalStateException until initialize(ConnectionConfig) is called again.

      If the client is not initialized (null), this method has no effect.

      This method is idempotent and safe to call multiple times.

    • getSharedClient

      public static org.apache.ignite.client.IgniteClient getSharedClient()
      Retrieves the shared IgniteClient instance for database operations.

      This method provides access to the singleton client that is shared across all tool invocations. The client is thread-safe and can be used concurrently by multiple tools without additional synchronization.

      Important: This method must only be called after initialize(ConnectionConfig) has been invoked during server startup. Calling this method before initialization will result in an exception.

      Returns:
      the shared IgniteClient instance
      Throws:
      IllegalStateException - if the client has not been initialized via initialize(ConnectionConfig)
      See Also:
    • getCurrentUserId

      public static String getCurrentUserId()
      Returns the current user ID from the IgniteClient configuration.
      Returns:
      user ID from client authenticator, or "anonymous" if not authenticated