Skip to content

Commit

Permalink
Better connection handling, prepare for the release
Browse files Browse the repository at this point in the history
  • Loading branch information
r4fek committed Feb 5, 2016
1 parent 3951669 commit 75c774f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 37 deletions.
30 changes: 12 additions & 18 deletions django_cassandra_engine/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,23 @@ def __init__(self, **options):
password=self.password)

self.lock = Lock()
self.session = None
self.cluster = None
self.setup()

def setup(self):
from cassandra.cqlengine import connection
with self.lock:
if connection.cluster is not None:
self.session = connection.get_session()
if not (self.session is None or self.session.is_shutdown):
# already connected
return

for option, value in self.session_options.items():
setattr(Session, option, value)
connection.setup(self.hosts, self.keyspace,
**self.connection_options)
self.session = connection.get_session()
self.cluster = connection.get_cluster()

def commit(self):
pass
Expand All @@ -76,14 +80,6 @@ def rollback(self):
def cursor(self):
return Cursor(self)

@property
def session(self):
return connection.get_session()

@property
def cluster(self):
return connection.get_cluster()

def execute(self, qs, *args, **kwargs):
self.session.set_keyspace(self.keyspace)
return self.session.execute(qs, *args, **kwargs)
Expand All @@ -96,13 +92,11 @@ def close_all(self):
Close all db connections
"""
with self.lock:
if connection.cluster is not None:
connection.cluster.shutdown()
if self.cluster is not None:
self.cluster.shutdown()

if connection.session is not None:
connection.session.shutdown()
if self.session is not None and not self.session.is_shutdown:
self.session.shutdown()

connection.session = None
connection.cluster = None
connection.lazy_connect_args = None
connection.default_consistency_level = None
self.session = None
self.cluster = None
21 changes: 3 additions & 18 deletions django_cassandra_engine/models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
from django_cassandra_engine.utils import get_cassandra_connection

cassandra_connection = get_cassandra_connection()


try:
from uwsgidecorators import postfork
except ImportError:
# We're not in a uWSGI context, no need to hook Cassandra session
# initialization to the postfork event.
if cassandra_connection is not None:
cassandra_connection.connect()
else:
@postfork
def cassandra_init():
""" Initialize a new Cassandra session in the context.
Ensures that a new session is returned for every new request.
"""
if cassandra_connection is not None:
cassandra_connection.reconnect()
cassandra_connection = get_cassandra_connection()
if cassandra_connection is not None:
cassandra_connection.connect()
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# CHANGELOG

## Version 0.6.4 (04.02.2016)
## Version 0.6.4 (05.02.2016)

* Get rid of race condition in `CassandraConnection.setup`
* Remove not needed reconnecting on `@postfork`

## Version 0.6.3 (08.01.2016)

Expand Down

0 comments on commit 75c774f

Please sign in to comment.