public synchronized Session get()

in server/src/main/java/org/apache/cassandra/sidecar/cluster/CQLSessionProviderImpl.java [176:245]


    public synchronized Session get() throws CassandraUnavailableException
    {
        if (session != null)
        {
            return session;
        }

        Cluster cluster = null;
        try
        {
            logger.info("Connecting to cluster using contact points {}", contactPoints);

            LoadBalancingPolicy lbp = new SidecarLoadBalancingPolicy(localInstances, localDc, numAdditionalConnections,
                                                                     driverUtils);
            // Prevent spurious reconnects of ignored down nodes on `onUp` events
            QueryOptions queryOptions = new QueryOptions().setReprepareOnUp(false);
            Cluster.Builder builder
            = Cluster.builder()
                     .addContactPointsWithPorts(contactPoints)
                     .withReconnectionPolicy(reconnectionPolicy)
                     .withoutMetrics()
                     .withLoadBalancingPolicy(lbp)
                     .withQueryOptions(queryOptions)
                     // tests can create a lot of these Cluster objects, to avoid creating HWTs and
                     // event thread pools for each we have the override
                     .withNettyOptions(nettyOptions);

            SslContext sslContext = createSslContext(sslConfiguration);
            if (sslContext != null)
            {
                RemoteEndpointAwareNettySSLOptions sslOptions
                = new RemoteEndpointAwareNettySSLOptions(sslContext);
                builder.withSSL(sslOptions);
            }

            if (username != null && password != null)
            {
                builder.withCredentials(username, password);
            }
            // During mTLS connections, when client sends in keystore, we should have an AuthProvider passed along.
            // hence we pass empty username and password in PlainTextAuthProvider here, in case user hasn't already
            // configured username and password.
            else if (sslConfiguration != null && sslConfiguration.isKeystoreConfigured())
            {
                builder.withAuthProvider(new PlainTextAuthProvider("", ""));
            }

            cluster = builder.build();
            session = cluster.connect();
            logger.info("Successfully connected to Cassandra!");
            return session;
        }
        catch (Exception connectionException)
        {
            logger.error("Failed to reach Cassandra", connectionException);
            if (cluster != null)
            {
                try
                {
                    cluster.close();
                }
                catch (Exception closeException)
                {
                    logger.error("Failed to close cluster in cleanup", closeException);
                    connectionException.addSuppressed(closeException);
                }
            }
            throw new CassandraUnavailableException(CQL, connectionException);
        }
    }