-
Notifications
You must be signed in to change notification settings - Fork 49
/
mongo_connection_manager.py
63 lines (52 loc) · 2.59 KB
/
mongo_connection_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) 2012 Jerry Schneider
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ConfigParser
from robot.api import logger
class MongoConnectionManager(object):
"""
Connection Manager handles the connection & disconnection to the database.
"""
def __init__(self):
"""
Initializes _dbconnection to None.
"""
self._dbconnection = None
def connect_to_mongodb(self, dbHost='localhost', dbPort=27017, dbMaxPoolSize=10, dbNetworkTimeout=None, dbDocClass=dict, dbTZAware=False):
"""
Loads pymongo and connects to the MongoDB host using parameters submitted.
Example usage:
| # To connect to foo.bar.org's MongoDB service on port 27017 |
| Connect To MongoDB | foo.bar.org | ${27017} |
"""
dbapiModuleName = 'pymongo'
db_api_2 = __import__(dbapiModuleName);
dbPort = int(dbPort)
#print "host is [ %s ]" % dbHost
#print "port is [ %s ]" % dbPort
#print "pool_size is [ %s ]" % dbPoolSize
#print "timeout is [ %s ]" % dbTimeout
#print "slave_okay is [ %s ]" % dbSlaveOkay
#print "document_class is [ %s ]" % dbDocClass
#print "tz_aware is [ %s ]" % dbTZAware
print "| Connect To MondoDB | dbHost | dbPort | dbMaxPoolSize | dbNetworktimeout | dbDocClass | dbTZAware |"
print "| Connect To MondoDB | %s | %s | %s | %s | %s | %s |" % (dbHost,dbPort,dbMaxPoolSize,dbNetworkTimeout,dbDocClass,dbTZAware)
self._dbconnection = db_api_2.connection.Connection (host=dbHost, port=dbPort, max_pool_size=dbMaxPoolSize, network_timeout=dbNetworkTimeout, document_class=dbDocClass, tz_aware=dbTZAware);
def disconnect_from_mongodb(self):
"""
Disconnects from the MongoDB server.
For example:
| Disconnect From MongoDB | # disconnects from current connection to the MongoDB server |
"""
print "| Disconnect From MongoDB |"
self._dbconnection.disconnect()