Ejemplo n.º 1
0
    def _connect(self):
        """Connect to PostgreSQL, either by reusing a connection from the pool
        if possible, or by creating the new connection.

        :rtype: psycopg2.connection

        """
        # Attempt to get a cached connection from the connection pool
        if self._use_pool and pool.has_idle_connection(self.pid):
            connection = pool.get_connection(self.pid)
            if connection:
                self._from_pool = True
                return connection

        # Create a new PostgreSQL connection
        kwargs = utils.uri_to_kwargs(self._uri)
        connection = self._psycopg2_connect(kwargs)

        # Add it to the pool, if pooling is enabled
        if self._use_pool:
            pool.add_connection(self.pid, self, connection)

        # Added in because psycopg2ct connects and leaves the connection in
        # a weird state: consts.STATUS_DATESTYLE, returning from
        # Connection._setup without setting the state as const.STATUS_OK
        if PYPY:
            connection.reset()

        # Register the custom data types
        self._register_unicode(connection)
        self._register_uuid(connection)

        return connection
Ejemplo n.º 2
0
    def _connect(self):
        """Connect to PostgreSQL, either by reusing a connection from the pool
        if possible, or by creating the new connection.

        :rtype: psycopg2.extensions.connection
        :raises: pool.NoIdleConnectionsError

        """
        # Attempt to get a cached connection from the connection pool
        try:
            connection = self._pool_manager.get(self.pid, self)
            LOGGER.debug("Re-using connection for %s", self.pid)
        except pool.NoIdleConnectionsError:
            if self._pool_manager.is_full(self.pid):
                raise

            # Create a new PostgreSQL connection
            kwargs = utils.uri_to_kwargs(self._uri)
            LOGGER.debug("Creating a new connection for %s", self.pid)
            connection = self._psycopg2_connect(kwargs)

            self._pool_manager.add(self.pid, connection)
            self._pool_manager.lock(self.pid, connection, self)

            # Added in because psycopg2ct connects and leaves the connection in
            # a weird state: consts.STATUS_DATESTYLE, returning from
            # Connection._setup without setting the state as const.STATUS_OK
            if PYPY:
                connection.reset()

            # Register the custom data types
            self._register_unicode(connection)
            self._register_uuid(connection)

        return connection
Ejemplo n.º 3
0
    def _connect(self):
        """Connect to PostgreSQL, either by reusing a connection from the pool
        if possible, or by creating the new connection.

        :rtype: psycopg2.extensions.connection
        :raises: pool.NoIdleConnectionsError

        """
        # Attempt to get a cached connection from the connection pool
        try:
            connection = self._pool_manager.get(self.pid, self)
            LOGGER.debug("Re-using connection for %s", self.pid)
        except pool.NoIdleConnectionsError:
            if self._pool_manager.is_full(self.pid):
                raise

            # Create a new PostgreSQL connection
            kwargs = utils.uri_to_kwargs(self._uri)
            LOGGER.debug("Creating a new connection for %s", self.pid)
            connection = self._psycopg2_connect(kwargs)

            self._pool_manager.add(self.pid, connection)
            self._pool_manager.lock(self.pid, connection, self)

            # Added in because psycopg2ct connects and leaves the connection in
            # a weird state: consts.STATUS_DATESTYLE, returning from
            # Connection._setup without setting the state as const.STATUS_OK
            if utils.PYPY:
                connection.reset()

            # Register the custom data types
            self._register_unicode(connection)
            self._register_uuid(connection)

        return connection
Ejemplo n.º 4
0
    def _create_connection(self, future):
        """Create a new PostgreSQL connection

        :param tornado.concurrent.Future future: future for new conn result

        """
        # Create a new PostgreSQL connection
        kwargs = utils.uri_to_kwargs(self._uri)

        connection = self._psycopg2_connect(kwargs)

        # Add the connection for use in _poll_connection
        fd = connection.fileno()
        self._connections[fd] = connection

        def on_connected(cf):
            """Invoked by the IOLoop when the future is complete for the
            connection

            :param Future cf: The future for the initial connection

            """
            if cf.exception():
                future.set_exception(cf.exception())

            else:

                # Add the connection to the pool
                self._pool_manager.add(self.pid, connection)
                self._pool_manager.lock(self.pid, connection, self)

                # Added in because psycopg2ct connects and leaves the
                # connection in a weird state: consts.STATUS_DATESTYLE,
                # returning from Connection._setup without setting the state
                # as const.STATUS_OK
                if PYPY:
                    connection.status = extensions.STATUS_READY

                # Register the custom data types
                self._register_unicode(connection)
                self._register_uuid(connection)

                # Set the future result
                future.set_result(connection)

        # Add a future that fires once connected
        self._futures[fd] = concurrent.TracebackFuture()
        self._ioloop.add_future(self._futures[fd], on_connected)

        # Add the connection to the IOLoop
        self._ioloop.add_handler(connection.fileno(),
                                 self._on_io_events,
                                 ioloop.IOLoop.WRITE)
Ejemplo n.º 5
0
    def _create_connection(self, future):
        """Create a new PostgreSQL connection

        :param tornado.concurrent.Future future: future for new conn result

        """
        # Create a new PostgreSQL connection
        kwargs = utils.uri_to_kwargs(self._uri)

        connection = self._psycopg2_connect(kwargs)

        # Add the connection for use in _poll_connection
        fd = connection.fileno()
        self._connections[fd] = connection

        def on_connected(cf):
            """Invoked by the IOLoop when the future is complete for the
            connection

            :param Future cf: The future for the initial connection

            """
            if cf.exception():
                future.set_exception(cf.exception())

            else:

                # Add the connection to the pool
                self._pool_manager.add(self.pid, connection)
                self._pool_manager.lock(self.pid, connection, self)

                # Added in because psycopg2ct connects and leaves the
                # connection in a weird state: consts.STATUS_DATESTYLE,
                # returning from Connection._setup without setting the state
                # as const.STATUS_OK
                if PYPY:
                    connection.status = extensions.STATUS_READY

                # Register the custom data types
                self._register_unicode(connection)
                self._register_uuid(connection)

                # Set the future result
                future.set_result(connection)

        # Add a future that fires once connected
        self._futures[fd] = concurrent.TracebackFuture()
        self._ioloop.add_future(self._futures[fd], on_connected)

        # Add the connection to the IOLoop
        self._ioloop.add_handler(connection.fileno(), self._on_io_events,
                                 ioloop.IOLoop.WRITE)
Ejemplo n.º 6
0
 def test_unix_socket_path_format2(self):
     socket_path = 'postgresql:///postgres?host=/tmp/'
     result = utils.uri_to_kwargs(socket_path)
     self.assertEqual(result['host'], '/tmp/')
Ejemplo n.º 7
0
 def test_uri_to_kwargs_options(self):
     """options should match expectation"""
     self.assertEqual(
         utils.uri_to_kwargs(self.URI)['options'], ['foo', 'bar'])
Ejemplo n.º 8
0
 def test_uri_to_kwargs_keepalive(self):
     """keepalive should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['keepalives'], 1)
Ejemplo n.º 9
0
 def test_uri_to_kwargs_password(self):
     """password should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['password'],
                      'c#^%#\'$@:')
Ejemplo n.º 10
0
    def _create_connection(self, future):
        """Create a new PostgreSQL connection

        :param tornado.concurrent.Future future: future for new conn result

        """
        LOGGER.debug('Creating a new connection for %s', self.pid)

        # Create a new PostgreSQL connection
        kwargs = utils.uri_to_kwargs(self._uri)

        try:
            connection = self._psycopg2_connect(kwargs)
        except (psycopg2.Error, OSError, socket.error) as error:
            future.set_exception(error)
            return

        # Add the connection for use in _poll_connection
        fd = connection.fileno()
        self._connections[fd] = connection

        def on_connected(cf):
            """Invoked by the IOLoop when the future is complete for the
            connection

            :param Future cf: The future for the initial connection

            """
            if cf.exception():
                self._cleanup_fd(fd, True)
                future.set_exception(cf.exception())

            else:

                try:
                    # Add the connection to the pool
                    LOGGER.debug('Connection established for %s', self.pid)
                    self._pool_manager.add(self.pid, connection)
                except (ValueError, pool.PoolException) as err:
                    LOGGER.exception('Failed to add %r to the pool', self.pid)
                    self._cleanup_fd(fd)
                    future.set_exception(err)
                    return

                self._pool_manager.lock(self.pid, connection, self)

                # Added in because psycopg2cffi connects and leaves the
                # connection in a weird state: consts.STATUS_DATESTYLE,
                # returning from Connection._setup without setting the state
                # as const.STATUS_OK
                if utils.PYPY:
                    connection.status = extensions.STATUS_READY

                # Register the custom data types
                self._register_unicode(connection)
                self._register_uuid(connection)

                # Set the future result
                future.set_result(connection)

        # Add a future that fires once connected
        self._futures[fd] = concurrent.Future()
        self._ioloop.add_future(self._futures[fd], on_connected)

        # Add the connection to the IOLoop
        self._ioloop.add_handler(connection.fileno(), self._on_io_events,
                                 ioloop.IOLoop.WRITE)
Ejemplo n.º 11
0
 def test_uri_to_kwargs_host(self):
     """hostname should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['host'], 'baz')
Ejemplo n.º 12
0
 def test_uri_to_kwargs_dbname(self):
     """dbname should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['dbname'], 'qux')
Ejemplo n.º 13
0
 def test_unix_socket_path_format_one(self):
     socket_path = 'postgresql://%2Fvar%2Flib%2Fpostgresql/dbname'
     result = utils.uri_to_kwargs(socket_path)
     self.assertEqual(result['host'], '/var/lib/postgresql')
Ejemplo n.º 14
0
 def test_uri_to_kwargs_keepalive(self):
     """keepalive should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['keepalives'], 1)
Ejemplo n.º 15
0
 def test_unix_socket_path_format_one(self):
     socket_path = 'postgresql://%2Fvar%2Flib%2Fpostgresql/dbname'
     result = utils.uri_to_kwargs(socket_path)
     self.assertEqual(result['host'], '/var/lib/postgresql')
Ejemplo n.º 16
0
 def test_uri_to_kwargs_invalid(self):
     """invalid query argument should not be in kwargs"""
     self.assertNotIn('invaid', utils.uri_to_kwargs(self.URI))
Ejemplo n.º 17
0
    def _create_connection(self, future):
        """Create a new PostgreSQL connection

        :param tornado.concurrent.Future future: future for new conn result

        """
        LOGGER.debug('Creating a new connection for %s', self.pid)

        # Create a new PostgreSQL connection
        kwargs = utils.uri_to_kwargs(self._uri)

        try:
            connection = self._psycopg2_connect(kwargs)
        except (psycopg2.Error, OSError, socket.error) as error:
            future.set_exception(error)
            return

        # Add the connection for use in _poll_connection
        fd = connection.fileno()
        self._connections[fd] = connection

        def on_connected(cf):
            """Invoked by the IOLoop when the future is complete for the
            connection

            :param Future cf: The future for the initial connection

            """
            if cf.exception():
                self._cleanup_fd(fd, True)
                future.set_exception(cf.exception())

            else:

                try:
                    # Add the connection to the pool
                    LOGGER.debug('Connection established for %s', self.pid)
                    self._pool_manager.add(self.pid, connection)
                except (ValueError, pool.PoolException) as err:
                    LOGGER.exception('Failed to add %r to the pool', self.pid)
                    self._cleanup_fd(fd)
                    future.set_exception(err)
                    return

                self._pool_manager.lock(self.pid, connection, self)

                # Added in because psycopg2cffi connects and leaves the
                # connection in a weird state: consts.STATUS_DATESTYLE,
                # returning from Connection._setup without setting the state
                # as const.STATUS_OK
                if utils.PYPY:
                    connection.status = extensions.STATUS_READY

                # Register the custom data types
                self._register_unicode(connection)
                self._register_uuid(connection)

                # Set the future result
                future.set_result(connection)

        # Add a future that fires once connected
        self._futures[fd] = concurrent.Future()
        self._ioloop.add_future(self._futures[fd], on_connected)

        # Add the connection to the IOLoop
        self._ioloop.add_handler(connection.fileno(),
                                 self._on_io_events,
                                 ioloop.IOLoop.WRITE)
Ejemplo n.º 18
0
 def test_uri_to_kwargs_invalid(self):
     """invalid query argument should not be in kwargs"""
     self.assertNotIn('invaid', utils.uri_to_kwargs(self.URI))
Ejemplo n.º 19
0
 def test_uri_to_kwargs_host(self):
     """hostname should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['host'], 'baz')
Ejemplo n.º 20
0
 def test_unix_socket_path_format2(self):
     socket_path = 'postgresql:///postgres?host=/tmp/'
     result = utils.uri_to_kwargs(socket_path)
     self.assertEqual(result['host'], '/tmp/')
Ejemplo n.º 21
0
 def test_uri_to_kwargs_port(self):
     """port should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['port'], 5444)
Ejemplo n.º 22
0
 def test_uri_to_kwargs_port(self):
     """port should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['port'], 5444)
Ejemplo n.º 23
0
 def test_uri_to_kwargs_dbname(self):
     """dbname should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['dbname'], 'qux')
Ejemplo n.º 24
0
 def test_uri_to_kwargs_username(self):
     """user should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['user'], 'foo')
Ejemplo n.º 25
0
 def test_uri_to_kwargs_username(self):
     """user should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['user'], 'foo')
Ejemplo n.º 26
0
 def test_uri_to_kwargs_options(self):
     """options should match expectation"""
     self.assertEqual(utils.uri_to_kwargs(self.URI)['options'],
                      ['foo', 'bar'])
Ejemplo n.º 27
0
 def test_uri_to_kwargs_password(self):
     """password should match expectation"""
     self.assertEqual(
         utils.uri_to_kwargs(self.URI)['password'], 'c#^%#\'$@:')