Class ConnectionHandler (0.15.0)

publicclass ConnectionHandlerextendsThread

Handles a connection from a client to Spanner. This ConnectionHandler uses WireMessage to receive and send all messages from and to the client, using the intermediate representation IntermediateStatement that servers as a middle layer between Postgres and Spanner.

Each ConnectionHandler is also a Thread. Although a TCP connection does not necessarily need to have its own thread, this makes the implementation more straightforward.

Inheritance

java.lang.Object > Thread > ConnectionHandler

Inherited Members

Methods

cancelActiveStatement(int connectionId, int secret)

publicbooleancancelActiveStatement(intconnectionId,intsecret)

To be used by a cancellation command to cancel a currently running statement, as contained in a specific connection identified by connectionId. Since cancellation is a flimsy contract at best, it is not imperative that the cancellation run, but it should be attempted nonetheless.

Parameters
Name Description
connectionId int

The connection whose statement must be cancelled.

secret int

The secret value linked to the connection that is being cancelled. If it does not match, we cannot cancel.

Returns
Type Description
boolean

true if the statement was cancelled.

cleanUp(IntermediateStatement statement)

publicvoidcleanUp(IntermediateStatementstatement)

Closes portals and statements if the result of an execute was the end of a transaction.

Parameter
Name Description
Exceptions
Type Description

clearActiveCopyStatement()

publicvoidclearActiveCopyStatement()

clearInvalidMessageCount()

publicvoidclearInvalidMessageCount()

Clears the number of invalid messages that was received. This is called whenever a valid message is encountered.

closeAllStatements()

publicvoidcloseAllStatements()

closePortal(String portalName)

publicvoidclosePortal(StringportalName)
Parameter
Name Description
portalName String
Exceptions
Type Description

closeStatement(String statementName)

publicvoidcloseStatement(StringstatementName)
Parameter
Name Description
statementName String

connectToSpanner(String database, Credentials credentials)

publicvoidconnectToSpanner(Stringdatabase,Credentialscredentials)
Parameters
Name Description
database String
credentials com.google.auth.Credentials

getActiveCopyStatement()

publicCopyStatementgetActiveCopyStatement()
Returns
Type Description

getAutoDescribedStatement(String sql)

publicFuture<DescribeResult>getAutoDescribedStatement(Stringsql)

Returns the parameter types of a cached auto-described statement, or null if none is available in the cache.

Parameter
Name Description
sql String
Returns
Type Description

getConnectionId()

publicintgetConnectionId()
Returns
Type Description
int

getConnectionMetadata()

publicConnectionMetadatagetConnectionMetadata()
Returns
Type Description

getDatabaseId()

publicDatabaseIdgetDatabaseId()
Returns
Type Description
com.google.cloud.spanner.DatabaseId

getExtendedQueryProtocolHandler()

publicExtendedQueryProtocolHandlergetExtendedQueryProtocolHandler()
Returns
Type Description

getInvalidMessageCount()

publicintgetInvalidMessageCount()

Returns the number of invalid messages that this connection has received in a row. This can for example happen if a client has initiated a COPY operation and the copy operation fails on the server. The server will then respond with an error response, but if the client fails to read that message and continues to send copy data messages, the server could get flooded. This value is used to detect such a situation and breaks the connection if too many invalid messages in a row are received.

Returns
Type Description
int

getPortal(String portalName)

publicIntermediatePortalStatementgetPortal(StringportalName)
Parameter
Name Description
portalName String
Returns
Type Description

getSecret()

publicintgetSecret()
Returns
Type Description
int

getServer()

publicProxyServergetServer()
Returns
Type Description

getSpannerConnection()

publicConnectiongetSpannerConnection()
Returns
Type Description

getStatement(String statementName)

publicIntermediatePreparedStatementgetStatement(StringstatementName)
Parameter
Name Description
statementName String
Returns
Type Description

getStatus()

publicsynchronizedConnectionHandler.ConnectionStatusgetStatus()
Returns
Type Description

getWellKnownClient()

publicClientAutoDetector.WellKnownClientgetWellKnownClient()
Returns
Type Description

handleMessages()

publicvoidhandleMessages()

Reads and handles wire-protocol messages. This method is normally only called from this ConnectionHandler, but certain sub-protocols such as the COPY protocol also need to process messages in line.

Exceptions
Type Description

handleTerminate()

publicvoidhandleTerminate()

Called when a Terminate message is received. This closes this ConnectionHandler.

hasPortal(String portalName)

publicbooleanhasPortal(StringportalName)
Parameter
Name Description
portalName String
Returns
Type Description

hasStatement(String statementName)

publicbooleanhasStatement(StringstatementName)
Parameter
Name Description
statementName String
Returns
Type Description

increaseInvalidMessageCount()

publicvoidincreaseInvalidMessageCount()

Increases the number of invalid messages that was received in a row by 1.

maybeDetermineWellKnownClient(Statement statement)

publicvoidmaybeDetermineWellKnownClient(Statementstatement)

This is called by the simple com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage to give the connection the opportunity to determine the client that is connected based on the SQL string that is being executed.

Parameter
Name Description
statement com.google.cloud.spanner.Statement

registerAutoDescribedStatement(String sql, Future<DescribeResult> describeResult)

publicvoidregisterAutoDescribedStatement(Stringsql,Future<DescribeResult>describeResult)

Stores the parameter types of an auto-described statement in the cache.

Parameters
Name Description
sql String
describeResult Future<DescribeResult>

registerPortal(String portalName, IntermediatePortalStatement portal)

publicvoidregisterPortal(StringportalName,IntermediatePortalStatementportal)
Parameters
Name Description
portalName String

registerStatement(String statementName, IntermediatePreparedStatement statement)

publicvoidregisterStatement(StringstatementName,IntermediatePreparedStatementstatement)
Parameters
Name Description
statementName String

run()

publicvoidrun()

Simple runner starts a loop which keeps taking inbound messages, processing them, sending them to Spanner, getting a result, processing that result, and replying to the client (in that order). Also instantiates input and output streams from the client and handles auth.

Overrides

setActiveCopyStatement(CopyStatement copyStatement)

publicvoidsetActiveCopyStatement(CopyStatementcopyStatement)
Parameter
Name Description
copyStatement CopyStatement

setMessageState(WireMessage message)

publicvoidsetMessageState(WireMessagemessage)
Parameter
Name Description
message WireMessage

setStatus(ConnectionHandler.ConnectionStatus status)

publicsynchronizedvoidsetStatus(ConnectionHandler.ConnectionStatusstatus)
Parameter
Name Description

setWellKnownClient(ClientAutoDetector.WellKnownClient wellKnownClient)

publicvoidsetWellKnownClient(ClientAutoDetector.WellKnownClientwellKnownClient)
Parameter
Name Description

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月30日 UTC.