Class ConnectionFactoryImpl


  • public class ConnectionFactoryImpl
    extends ConnectionFactory
    ConnectionFactory implementation for version 3 (7.4+) connections.
    • Constructor Detail

      • ConnectionFactoryImpl

        public ConnectionFactoryImpl()
    • Method Detail

      • createSSPI

        private 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.SQLException
        java.io.IOException
      • openConnectionImpl

        public QueryExecutor openConnectionImpl​(HostSpec[] hostSpecs,
                                                java.util.Properties info)
                                         throws java.sql.SQLException
        Description copied from class: ConnectionFactory
        Implementation of ConnectionFactory.openConnection(org.postgresql.util.HostSpec[], java.util.Properties) for a particular protocol version. Implemented by subclasses of ConnectionFactory.
        Specified by:
        openConnectionImpl in class ConnectionFactory
        Parameters:
        hostSpecs - at least one host and port to connect to; multiple elements for round-robin failover
        info - extra properties controlling the connection; notably, "password" if present supplies the password to authenticate with.
        Returns:
        the new, initialized, connection, or null if 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 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 PGStream enableGSSEncrypted​(PGStream pgStream,
                                            GSSEncMode gssEncMode,
                                            java.lang.String host,
                                            java.util.Properties info,
                                            int connectTimeout)
                                     throws java.io.IOException,
                                            PSQLException
        Throws:
        java.io.IOException
        PSQLException
      • doAuthentication

        private 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.IOException
        java.sql.SQLException
      • runInitialQueries

        private void runInitialQueries​(QueryExecutor queryExecutor,
                                       java.util.Properties info)
                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • isPrimary

        private boolean isPrimary​(QueryExecutor queryExecutor)
                           throws java.sql.SQLException,
                                  java.io.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 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

        Throws:
        java.sql.SQLException
        java.io.IOException
        See Also:
        GUC_REPORT documentation, Hot standby documentation, in_hot_standby patch thread v10, in_hot_standby patch thread v14