com.taosdata.jdbc.ws

## Class WSConnectionManager

- java.lang.Object
- - com.taosdata.jdbc.ws.WSConnectionManager

All Implemented Interfaces:
  AutoCloseable

    public class WSConnectionManager
    extends Object
    implements AutoCloseable

Manages WebSocket connections, reconnection, and load balancing for TDengine. Handles connection lifecycle including initial connection, reconnection on failure, and balancing connections across multiple endpoints.

### Constructor Summary

Constructors
Constructor and Description
`WSConnectionManager(WSFunction function, ConnectionParam param, InFlightRequest inFlightRequest)` Constructs a new WSConnectionManager with the specified parameters.

### Method Summary

All Methods Instance Methods Concrete Methods
`boolean` `authenticateAfterReconnect(Transport transport)` Performs authentication after physical reconnection.
`void` `balanceConnection(Transport transport)` Balances connections across available endpoints by switching to the least loaded endpoint.
`void` `checkConnection(int connectTimeout)` Checks and establishes connection to TDengine servers.
`void` `close()` Closes all connections and releases resources.
`ConnectionParam` `getConnectionParam()` Gets the connection parameters.
`WSClient` `getCurrentClient()` Gets the current active WebSocket client.
`Endpoint` `getCurrentEndpoint()` Gets the current active endpoint.
`int` `getReconnectCount()` Gets the number of reconnection attempts made.
`boolean` `isClosed()` Checks if this connection manager is closed.
`boolean` `isConnected()` Checks if the current connection is open and active.
`boolean` `isConnectionLost()` Checks if the current connection is closed or lost.
`void` `performReconnect(boolean isTmq, Transport transport)` Performs complete reconnection process including physical reconnection and authentication.
`void` `tmqRethrowConnectionCloseException()` Throws special connection closed exception for TMQ connections.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### WSConnectionManager

    public WSConnectionManager(WSFunction function,
                               ConnectionParam param,
                               InFlightRequest inFlightRequest)
                        throws SQLException

Constructs a new WSConnectionManager with the specified parameters.

Parameters:
  `function` - the WebSocket function type
  `param` - the connection parameters
  `inFlightRequest` - the in-flight request manager
Throws:
  `SQLException` - if connection initialization fails

### Method Detail

#### performReconnect

    public void performReconnect(boolean isTmq,
                                 Transport transport)
                          throws SQLException

Performs complete reconnection process including physical reconnection and authentication.

Parameters:
  `isTmq` - whether this is a TMQ connection (TMQ has different reconnection behavior)
  `transport` - the transport instance for sending authentication requests
Throws:
  `SQLException` - if reconnection fails for all endpoints

#### authenticateAfterReconnect

    public boolean authenticateAfterReconnect(Transport transport)
                                       throws SQLException

Performs authentication after physical reconnection.

Parameters:
  `transport` - the transport instance for sending authentication request
Returns:
  true if authentication successful, false otherwise
Throws:
  `SQLException` - if authentication request fails

#### checkConnection

    public void checkConnection(int connectTimeout)
                         throws SQLException

Checks and establishes connection to TDengine servers. For first connection with slave cluster, tests all nodes and keeps only the current connection. For subsequent connections, uses load balancing to select the best endpoint.

Parameters:
  `connectTimeout` - the connection timeout in milliseconds
Throws:
  `SQLException` - if connection cannot be established within timeout

#### balanceConnection

    public void balanceConnection(Transport transport)

Balances connections across available endpoints by switching to the least loaded endpoint.

Parameters:
  `transport` - the transport instance for sending authentication during switch

#### getCurrentClient

    public WSClient getCurrentClient()

Gets the current active WebSocket client.

Returns:
  the current WSClient instance

#### isConnected

    public boolean isConnected()

Checks if the current connection is open and active.

Returns:
  true if connected, false otherwise

#### isConnectionLost

    public boolean isConnectionLost()

Checks if the current connection is closed or lost.

Returns:
  true if connection is lost, false otherwise

#### getReconnectCount

    public int getReconnectCount()

Gets the number of reconnection attempts made.

Returns:
  the reconnection count

#### getConnectionParam

    public ConnectionParam getConnectionParam()

Gets the connection parameters.

Returns:
  the connection parameters

#### getCurrentEndpoint

    public Endpoint getCurrentEndpoint()

Gets the current active endpoint.

Returns:
  the current endpoint

#### isClosed

    public boolean isClosed()

Checks if this connection manager is closed.

Returns:
  true if closed, false otherwise

#### close

    public void close()

Closes all connections and releases resources.

Specified by:
  `close` in interface `AutoCloseable`

#### tmqRethrowConnectionCloseException

    public void tmqRethrowConnectionCloseException()
                                            throws SQLException

Throws special connection closed exception for TMQ connections. TMQ handles reconnection differently in its poll mechanism.

Throws:
  `SQLException` - always throws TMQ-specific connection closed exception