Top Description Inners Fields Constructors Methods
org.postgresql.core.v3

public Class ConnectionFactoryImpl

extends ConnectionFactory
Class Inheritance
Static Imports
org.postgresql.util.internal.Nullness.castNonNull

ConnectionFactory implementation for version 3 (7.4+) connections.
Author
Oliver Jowett ([email protected]), based on the previous implementation

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final String
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static String

Returns:

The current JVM time zone in postgresql format.
createPostgresTimeZone
()

Convert Java time zone to postgres time zone.

private ISSPIClient
createSSPI(PGStream pgStream, @Nullable String spnServiceClass, boolean enableNegotiate)

private void
doAuthentication(PGStream pgStream, String host, String user, Properties info)

private PGStream
enableGSSEncrypted(PGStream pgStream, GSSEncMode gssEncMode, String host, Properties info, int connectTimeout)

private PGStream
enableSSL(PGStream pgStream, SslMode sslMode, Properties info, int connectTimeout)

private List<ConnectionFactoryImpl.StartupParam>
private boolean
isPrimary(QueryExecutor queryExecutor)

Since PG14 there is GUC_REPORT ParamStatus in_hot_standby which is set to "on" when the server is in archive recovery or standby mode.

private static void
log(Level level, String msg, Throwable thrown, Object... params)

public QueryExecutor
openConnectionImpl(HostSpec[]
at least one host and port to connect to; multiple elements for round-robin failover
hostSpecs
,
Properties
extra properties controlling the connection; notably, "password" if present supplies the password to authenticate with.
info
)

Implements abstract org.postgresql.core.ConnectionFactory.openConnectionImpl.

Implementation of openConnection for a particular protocol version.

private void
private void
private PGStream
tryConnect(Properties info, SocketFactory socketFactory, HostSpec hostSpec, SslMode sslMode, GSSEncMode gssEncMode)

Inherited from org.postgresql.core.ConnectionFactory:
closeStreamopenConnection

Field Detail

AUTH_REQ_CRYPTback to summary
private static final int AUTH_REQ_CRYPT
AUTH_REQ_GSSback to summary
private static final int AUTH_REQ_GSS
AUTH_REQ_GSS_CONTINUEback to summary
private static final int AUTH_REQ_GSS_CONTINUE
AUTH_REQ_KRB4back to summary
private static final int AUTH_REQ_KRB4
AUTH_REQ_KRB5back to summary
private static final int AUTH_REQ_KRB5
AUTH_REQ_MD5back to summary
private static final int AUTH_REQ_MD5
AUTH_REQ_OKback to summary
private static final int AUTH_REQ_OK
AUTH_REQ_PASSWORDback to summary
private static final int AUTH_REQ_PASSWORD
AUTH_REQ_SASLback to summary
private static final int AUTH_REQ_SASL
AUTH_REQ_SASL_CONTINUEback to summary
private static final int AUTH_REQ_SASL_CONTINUE
AUTH_REQ_SASL_FINALback to summary
private static final int AUTH_REQ_SASL_FINAL
AUTH_REQ_SCMback to summary
private static final int AUTH_REQ_SCM
AUTH_REQ_SSPIback to summary
private static final int AUTH_REQ_SSPI
IN_HOT_STANDBYback to summary
private static final String IN_HOT_STANDBY
LOGGERback to summary
private static final Logger LOGGER

Hides org.postgresql.core.ConnectionFactory.LOGGER.

Constructor Detail

ConnectionFactoryImplback to summary
public ConnectionFactoryImpl()

Method Detail

createPostgresTimeZoneback to summary
private static String createPostgresTimeZone()

Convert Java time zone to postgres time zone. All others stay the same except that GMT+nn changes to GMT-nn and vise versa. If you provide GMT+/-nn postgres uses POSIX rules which has a positive sign for west of Greenwich JAVA uses ISO rules which the positive sign is east of Greenwich To make matters more interesting postgres will always report in ISO

Returns:String

The current JVM time zone in postgresql format.

createSSPIback to summary
private ISSPIClient createSSPI(PGStream pgStream, @Nullable String spnServiceClass, boolean enableNegotiate)
doAuthenticationback to summary
private void doAuthentication(PGStream pgStream, String host, String user, Properties info) throws IOException, SQLException
enableGSSEncryptedback to summary
private PGStream enableGSSEncrypted(PGStream pgStream, GSSEncMode gssEncMode, String host, Properties info, int connectTimeout) throws IOException, PSQLException
enableSSLback to summary
private PGStream enableSSL(PGStream pgStream, SslMode sslMode, Properties info, int connectTimeout) throws IOException, PSQLException
getParametersForStartupback to summary
private List<ConnectionFactoryImpl.StartupParam> getParametersForStartup(String user, String database, Properties info)
isPrimaryback to summary
private boolean isPrimary(QueryExecutor queryExecutor) throws SQLException, IOException

Since PG14 there is GUC_REPORT ParamStatus in_hot_standby which is set to "on" when the server is in archive recovery or standby mode. In driver's lingo such server is called org.postgresql.hostchooser.HostRequirement#secondary. Previously transaction_read_only was used as a workable substitute. However transaction_read_only could have been manually overridden on the primary server by database user leading to a false positives: ie server is effectively read-only but technically is "primary" (not in a recovery/standby mode).

This method checks whether in_hot_standby GUC was reported by the server during initial connection:

  • in_hot_standby was reported and the value was "on" then the server is a replica and database is read-only by definition, false is returned.
  • in_hot_standby was reported and the value was "off" then the server is indeed primary but database may be in read-only mode nevertheless. We proceed to conservatively show transaction_read_only since users may not be expecting a readonly connection for targetServerType=primary
  • If in_hot_standby has not been reported we fallback to pre v14 behavior.

Do not confuse hot_standby and in_hot_standby ParamStatuses

See Also
GUC_REPORT documentation, Hot standby documentation, in_hot_standby patch thread v10, in_hot_standby patch thread v14
logback to summary
private static void log(Level level, String msg, Throwable thrown, Object... params)
openConnectionImplback to summary
public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, Properties info) throws SQLException

Implements abstract org.postgresql.core.ConnectionFactory.openConnectionImpl.

Doc from org.postgresql.core.ConnectionFactory.openConnectionImpl.

Implementation of openConnection for a particular protocol version. Implemented by subclasses of ConnectionFactory.

Parameters
hostSpecs:HostSpec[]

at least one host and port to connect to; multiple elements for round-robin failover

info:Properties

extra properties controlling the connection; notably, "password" if present supplies the password to authenticate with.

Returns:QueryExecutor

the new, initialized, connection, or null if this protocol version is not supported by the server.

Annotations
@Override
Exceptions
SQLException:
if the connection could not be established for a reason other than protocol version incompatibility.
runInitialQueriesback to summary
private void runInitialQueries(QueryExecutor queryExecutor, Properties info) throws SQLException
sendStartupPacketback to summary
private void sendStartupPacket(PGStream pgStream, List<ConnectionFactoryImpl.StartupParam> params) throws IOException
tryConnectback to summary
private PGStream tryConnect(Properties info, SocketFactory socketFactory, HostSpec hostSpec, SslMode sslMode, GSSEncMode gssEncMode) throws SQLException, IOException
org.postgresql.core.v3 back to summary

private Class ConnectionFactoryImpl.StartupParam

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final String
private final String

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public byte[]
public byte[]
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

keyback to summary
private final String key
valueback to summary
private final String value

Constructor Detail

StartupParamback to summary
pack-priv StartupParam(String key, String value)

Method Detail

getEncodedKeyback to summary
public byte[] getEncodedKey()
getEncodedValueback to summary
public byte[] getEncodedValue()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override