Package org.postgresql.core.v3
Class ConnectionFactoryImpl
- java.lang.Object
-
- org.postgresql.core.ConnectionFactory
-
- org.postgresql.core.v3.ConnectionFactoryImpl
-
public class ConnectionFactoryImpl extends ConnectionFactory
ConnectionFactory implementation for version 3 (7.4+) connections.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classConnectionFactoryImpl.StartupParam
-
Field Summary
Fields Modifier and Type Field Description private static intAUTH_REQ_CRYPTprivate static intAUTH_REQ_GSSprivate static intAUTH_REQ_GSS_CONTINUEprivate static intAUTH_REQ_KRB4private static intAUTH_REQ_KRB5private static intAUTH_REQ_MD5private static intAUTH_REQ_OKprivate static intAUTH_REQ_PASSWORDprivate static intAUTH_REQ_SASLprivate static intAUTH_REQ_SASL_CONTINUEprivate static intAUTH_REQ_SASL_FINALprivate static intAUTH_REQ_SCMprivate static intAUTH_REQ_SSPIprivate static java.lang.StringIN_HOT_STANDBYprivate static java.util.logging.LoggerLOGGER
-
Constructor Summary
Constructors Constructor Description ConnectionFactoryImpl()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.StringcreatePostgresTimeZone()Convert Java time zone to postgres time zone.private static ISSPIClientcreateSSPI(PGStream pgStream, java.lang.String spnServiceClass, boolean enableNegotiate)private static voiddoAuthentication(PGStream pgStream, java.lang.String host, java.lang.String user, java.util.Properties info)private static PGStreamenableGSSEncrypted(PGStream pgStream, GSSEncMode gssEncMode, java.lang.String host, java.util.Properties info, int connectTimeout)private static PGStreamenableSSL(PGStream pgStream, SslMode sslMode, java.util.Properties info, int connectTimeout)private static java.lang.StringgetAuthenticationMethodName(int authReq)private static java.util.List<ConnectionFactoryImpl.StartupParam>getParametersForStartup(java.lang.String user, java.lang.String database, java.util.Properties info)private static booleanisPrimary(QueryExecutor queryExecutor)Since PG14 there is GUC_REPORT ParamStatusin_hot_standbywhich is set to "on" when the server is in archive recovery or standby mode.private static voidlog(java.util.logging.Level level, java.lang.String msg, java.lang.Throwable thrown, java.lang.Object... params)QueryExecutoropenConnectionImpl(HostSpec[] hostSpecs, java.util.Properties info)Implementation ofConnectionFactory.openConnection(org.postgresql.util.HostSpec[], java.util.Properties)for a particular protocol version.private static voidrunInitialQueries(QueryExecutor queryExecutor, java.util.Properties info)private static voidsendStartupPacket(PGStream pgStream, ProtocolVersion protocolVersion, java.util.List<ConnectionFactoryImpl.StartupParam> params)private PGStreamtryConnect(java.util.Properties info, javax.net.SocketFactory socketFactory, HostSpec hostSpec, SslMode sslMode, GSSEncMode gssEncMode)-
Methods inherited from class org.postgresql.core.ConnectionFactory
closeStream, openConnection
-
-
-
-
Field Detail
-
LOGGER
private static final java.util.logging.Logger LOGGER
-
AUTH_REQ_OK
private static final int AUTH_REQ_OK
- See Also:
- Constant Field Values
-
AUTH_REQ_KRB4
private static final int AUTH_REQ_KRB4
- See Also:
- Constant Field Values
-
AUTH_REQ_KRB5
private static final int AUTH_REQ_KRB5
- See Also:
- Constant Field Values
-
AUTH_REQ_PASSWORD
private static final int AUTH_REQ_PASSWORD
- See Also:
- Constant Field Values
-
AUTH_REQ_CRYPT
private static final int AUTH_REQ_CRYPT
- See Also:
- Constant Field Values
-
AUTH_REQ_MD5
private static final int AUTH_REQ_MD5
- See Also:
- Constant Field Values
-
AUTH_REQ_SCM
private static final int AUTH_REQ_SCM
- See Also:
- Constant Field Values
-
AUTH_REQ_GSS
private static final int AUTH_REQ_GSS
- See Also:
- Constant Field Values
-
AUTH_REQ_GSS_CONTINUE
private static final int AUTH_REQ_GSS_CONTINUE
- See Also:
- Constant Field Values
-
AUTH_REQ_SSPI
private static final int AUTH_REQ_SSPI
- See Also:
- Constant Field Values
-
AUTH_REQ_SASL
private static final int AUTH_REQ_SASL
- See Also:
- Constant Field Values
-
AUTH_REQ_SASL_CONTINUE
private static final int AUTH_REQ_SASL_CONTINUE
- See Also:
- Constant Field Values
-
AUTH_REQ_SASL_FINAL
private static final int AUTH_REQ_SASL_FINAL
- See Also:
- Constant Field Values
-
IN_HOT_STANDBY
private static final java.lang.String IN_HOT_STANDBY
- See Also:
- Constant Field Values
-
-
Method Detail
-
createSSPI
private static ISSPIClient createSSPI(PGStream pgStream, java.lang.String spnServiceClass, boolean enableNegotiate)
-
tryConnect
private PGStream tryConnect(java.util.Properties info, javax.net.SocketFactory socketFactory, HostSpec hostSpec, SslMode sslMode, GSSEncMode gssEncMode) throws java.sql.SQLException, java.io.IOException
- Throws:
java.sql.SQLExceptionjava.io.IOException
-
openConnectionImpl
public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, java.util.Properties info) throws java.sql.SQLException
Description copied from class:ConnectionFactoryImplementation ofConnectionFactory.openConnection(org.postgresql.util.HostSpec[], java.util.Properties)for a particular protocol version. Implemented by subclasses ofConnectionFactory.- Specified by:
openConnectionImplin classConnectionFactory- Parameters:
hostSpecs- at least one host and port to connect to; multiple elements for round-robin failoverinfo- extra properties controlling the connection; notably, "password" if present supplies the password to authenticate with.- Returns:
- the new, initialized, connection, or
nullif this protocol version is not supported by the server. - Throws:
java.sql.SQLException- if the connection could not be established for a reason other than protocol version incompatibility.
-
getParametersForStartup
private static java.util.List<ConnectionFactoryImpl.StartupParam> getParametersForStartup(java.lang.String user, java.lang.String database, java.util.Properties info)
-
log
private static void log(java.util.logging.Level level, java.lang.String msg, java.lang.Throwable thrown, java.lang.Object... params)
-
createPostgresTimeZone
private static java.lang.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:
- The current JVM time zone in postgresql format.
-
enableGSSEncrypted
private static PGStream enableGSSEncrypted(PGStream pgStream, GSSEncMode gssEncMode, java.lang.String host, java.util.Properties info, int connectTimeout) throws java.io.IOException, PSQLException
- Throws:
java.io.IOExceptionPSQLException
-
enableSSL
private static PGStream enableSSL(PGStream pgStream, SslMode sslMode, java.util.Properties info, int connectTimeout) throws java.io.IOException, PSQLException
- Throws:
java.io.IOExceptionPSQLException
-
sendStartupPacket
private static void sendStartupPacket(PGStream pgStream, ProtocolVersion protocolVersion, java.util.List<ConnectionFactoryImpl.StartupParam> params) throws java.sql.SQLException, java.io.IOException
- Throws:
java.sql.SQLExceptionjava.io.IOException
-
getAuthenticationMethodName
private static java.lang.String getAuthenticationMethodName(int authReq)
-
doAuthentication
private static void doAuthentication(PGStream pgStream, java.lang.String host, java.lang.String user, java.util.Properties info) throws java.io.IOException, java.sql.SQLException
- Throws:
java.io.IOExceptionjava.sql.SQLException
-
runInitialQueries
private static void runInitialQueries(QueryExecutor queryExecutor, java.util.Properties info) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
isPrimary
private static boolean isPrimary(QueryExecutor queryExecutor) throws java.sql.SQLException, java.io.IOException
Since PG14 there is GUC_REPORT ParamStatusin_hot_standbywhich is set to "on" when the server is in archive recovery or standby mode. In driver's lingo such server is calledHostRequirement.secondary. Previouslytransaction_read_onlywas used as a workable substitute. Howevertransaction_read_onlycould 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_standbyGUC was reported by the server during initial connection:in_hot_standbywas reported and the value was "on" then the server is a replica and database is read-only by definition, false is returned.in_hot_standbywas reported and the value was "off" then the server is indeed primary but database may be in read-only mode nevertheless. We proceed to conservativelyshow transaction_read_onlysince users may not be expecting a readonly connection fortargetServerType=primary- If
in_hot_standbyhas not been reported we fallback to pre v14 behavior.
Do not confuse
hot_standbyandin_hot_standbyParamStatuses- Throws:
java.sql.SQLExceptionjava.io.IOException- See Also:
- GUC_REPORT documentation, Hot standby documentation, in_hot_standby patch thread v10, in_hot_standby patch thread v14
-
-