Package pymongo :: Module connection :: Class Connection
[frames] | no frames]

Class Connection

source code

object --+
         |
        Connection

A connection to Mongo.
Instance Methods
 
__init__(self, host=None, port=None, pool_size=None, auto_start_request=None, timeout=None, slave_okay=False, _connect=True)
Open a new connection to a Mongo instance at host:port.
source code
 
host(self)
Get the connection's current host.
source code
 
port(self)
Get the connection's current port.
source code
 
set_cursor_manager(self, manager_class)
Set this connection's cursor manager.
source code
 
start_request(self)
Start a "request".
source code
 
end_request(self)
End the current "request", if this thread is in one.
source code
 
__cmp__(self, other) source code
 
__repr__(self)
repr(x)
source code
 
__getattr__(self, name)
Get a database by name.
source code
 
__getitem__(self, name)
Get a database by name.
source code
 
close_cursor(self, cursor_id)
Close a single database cursor.
source code
 
kill_cursors(self, cursor_ids)
Kill database cursors with the given ids.
source code
 
server_info(self)
Get information about the MongoDB server we're connected to.
source code
 
database_names(self)
Get a list of all database names.
source code
 
drop_database(self, name_or_database)
Drop a database.
source code
 
__iter__(self) source code
 
next(self) source code

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Methods
 
paired(cls, left, right=None, pool_size=None, auto_start_request=None)
Open a new paired connection to Mongo.
source code
Class Variables
  HOST = 'localhost'
  PORT = 27017
  POOL_SIZE = 1
  AUTO_START_REQUEST = True
  TIMEOUT = 1.0
Properties
  slave_okay
Is it okay for this connection to connect directly to a slave?

Inherited from object: __class__

Method Details

__init__(self, host=None, port=None, pool_size=None, auto_start_request=None, timeout=None, slave_okay=False, _connect=True)
(Constructor)

source code 

Open a new connection to a Mongo instance at host:port.

The resultant connection object has connection-pooling built in. It also performs auto-reconnection when necessary. If an operation fails because of a connection error, pymongo.errors.ConnectionFailure is raised. If auto-reconnection will be performed, pymongo.errors.AutoReconnect will be raised. Application code should handle this exception (recognizing that the operation failed) and then continue to execute.

Raises TypeError if host is not an instance of string or port is not an instance of int. Raises ConnectionFailure if the connection cannot be made. Raises TypeError if pool_size is not an instance of int. Raises ValueError if pool_size is not greater than or equal to one.

NOTE: Connection pooling is not compatible with auth (yet). Please do not set the "pool_size" to anything other than 1 if auth is in use.

Parameters:
  • host - (optional): hostname or IPv4 address of the instance to connect to
  • port - (optional): port number on which to connect
  • pool_size - (optional): maximum size of the built in connection-pool
  • auto_start_request - (optional): automatically start a request on every operation - see documentation for start_request
  • slave_okay - (optional): is it okay to connect directly to and perform queries on a slave instance
  • timeout - (optional): max time to wait when attempting to acquire a connection from the connection pool before raising an exception - can be set to -1 to wait indefinitely
Overrides: object.__init__

paired(cls, left, right=None, pool_size=None, auto_start_request=None)
Class Method

source code 

Open a new paired connection to Mongo.

Raises TypeError if either left or right is not a tuple of the form (host, port). Raises ConnectionFailure if the connection cannot be made.

Parameters:
  • left - (host, port) pair for the left Mongo instance
  • right - (optional): (host, port) pair for the right Mongo instance
  • pool_size - (optional): same as argument to __init__
  • auto_start_request - (optional): same as argument to __init__

set_cursor_manager(self, manager_class)

source code 

Set this connection's cursor manager.

Raises TypeError if manager_class is not a subclass of CursorManager. A cursor manager handles closing cursors. Different managers can implement different policies in terms of when to actually kill a cursor that has been closed.

Parameters:
  • manager_class - cursor manager to use

start_request(self)

source code 

Start a "request".

A "request" is a group of operations in which order matters. Examples include inserting a document and then performing a query which expects that document to have been inserted, or performing an operation and then using database.Database.error() to perform error-checking on that operation. When a thread performs operations in a "request", the connection will perform all operations on the same socket, so Mongo will order them correctly.

This method is only relevant when the current Connection has a "pool_size" greater than one. Otherwise only a single socket will be used for all operations, so there is no need to group operations into requests.

This method only needs to be used if the "auto_start_request" option is set to False. If "auto_start_request" is True, a request will be started (if necessary) on every operation.

end_request(self)

source code 

End the current "request", if this thread is in one.

Judicious use of this method can lead to performance gains when connection-pooling is being used. By ending a request when it is safe to do so the connection is allowed to pick a new socket from the pool for that thread on the next operation. This could prevent an imbalance of threads trying to connect on the same socket. Care should be taken, however, to make sure that end_request isn't called in the middle of a sequence of operations in which ordering is important. This could lead to unexpected results.

end_request is useful even (especially) if "auto_start_request" is True.

See the documentation for start_request for more information on what a "request" is and when one should be used.

__repr__(self)
(Representation operator)

source code 
repr(x)
Overrides: object.__repr__
(inherited documentation)

__getattr__(self, name)
(Qualification operator)

source code 

Get a database by name.

Raises InvalidName if an invalid database name is used.

Parameters:
  • name - the name of the database to get

__getitem__(self, name)
(Indexing operator)

source code 

Get a database by name.

Raises InvalidName if an invalid database name is used.

Parameters:
  • name - the name of the database to get

close_cursor(self, cursor_id)

source code 

Close a single database cursor.

Raises TypeError if cursor_id is not an instance of (int, long). What closing the cursor actually means depends on this connection's cursor manager.

Parameters:
  • cursor_id - cursor id to close

kill_cursors(self, cursor_ids)

source code 

Kill database cursors with the given ids.

Raises TypeError if cursor_ids is not an instance of list.

Parameters:
  • cursor_ids - list of cursor ids to kill

drop_database(self, name_or_database)

source code 
Drop a database.
Parameters:
  • name_or_database - the name of a database to drop or the object itself

Property Details

slave_okay

Is it okay for this connection to connect directly to a slave?
Get Method:
unreachable.slave_okay(self) - Is it okay for this connection to connect directly to a slave?