MCPcopy Create free account
hub / github.com/apache/cassandra-python-driver / Connection

Class Connection

cassandra/cqlengine/connection.py:60–150  ·  view source on GitHub ↗

CQLEngine Connection

Source from the content-addressed store, hash-verified

58
59
60class Connection(object):
61 """CQLEngine Connection"""
62
63 name = None
64 hosts = None
65
66 consistency = None
67 retry_connect = False
68 lazy_connect = False
69 lazy_connect_lock = None
70 cluster_options = None
71
72 cluster = None
73 session = None
74
75 def __init__(self, name, hosts, consistency=None,
76 lazy_connect=False, retry_connect=False, cluster_options=None):
77 self.hosts = hosts
78 self.name = name
79 self.consistency = consistency
80 self.lazy_connect = lazy_connect
81 self.retry_connect = retry_connect
82 self.cluster_options = cluster_options if cluster_options else {}
83 self.lazy_connect_lock = threading.RLock()
84
85 @classmethod
86 def from_session(cls, name, session):
87 instance = cls(name=name, hosts=session.hosts)
88 instance.cluster, instance.session = session.cluster, session
89 instance.setup_session()
90 return instance
91
92 def setup(self):
93 """Set up the connection"""
94 global cluster, session
95
96 if 'username' in self.cluster_options or 'password' in self.cluster_options:
97 raise CQLEngineException("Username & Password are now handled by using the native driver's auth_provider")
98
99 if self.lazy_connect:
100 return
101
102 if 'cloud' in self.cluster_options:
103 if self.hosts:
104 log.warning("Ignoring hosts %s because a cloud config was provided.", self.hosts)
105 self.cluster = Cluster(**self.cluster_options)
106 else:
107 self.cluster = Cluster(self.hosts, **self.cluster_options)
108
109 try:
110 self.session = self.cluster.connect()
111 log.debug(format_log_context("connection initialized with internally created session", connection=self.name))
112 except NoHostAvailable:
113 if self.retry_connect:
114 log.warning(format_log_context("connect failed, setting up for re-attempt on first use", connection=self.name))
115 self.lazy_connect = True
116 raise
117

Callers 1

register_connectionFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected