diff --git a/diaspora_auth_provider.py b/diaspora_auth_provider.py
index d22b0580971a895db84ab50fb2800554e1c6b66c..80befdf5d62506a7c28c8c0eee4105d82e450a5d 100644
--- a/diaspora_auth_provider.py
+++ b/diaspora_auth_provider.py
@@ -23,24 +23,17 @@ import bcrypt
 
 import logging
 
-__VERSION__ = "0.0.1"
+__VERSION__ = "0.0.2"
 
 logger = logging.getLogger(__name__)
 
 
 class DiasporaAuthProvider:
-    __version__ = "0.0.1"
+    __version__ = "0.0.2"
 
     def __init__(self, config, account_handler):
         self.account_handler = account_handler
         self.config = config
-        self.connection = psycopg2.connect(
-            dbname=self.config.db_name,
-            user=self.config.db_username,
-            password=self.config.db_password,
-            host=self.config.db_host,
-            port=self.config.db_port
-        )
 
     @defer.inlineCallbacks
     def check_password(self, user_id, password):
@@ -49,41 +42,52 @@ class DiasporaAuthProvider:
         # user_id is @localpart:hs_bare. we only need the localpart.
         local_part = user_id.split(':', 1)[0][1:]
         logger.info("Checking if user {} exists.".format(local_part))
-        with self.connection.cursor() as cursor:
-            yield threads.deferToThread( # Don't think this is needed, but w/e
-                cursor.execute,
-                "SELECT username, encrypted_password FROM users WHERE username=%s",
-                (local_part,)
-            )
-            user = yield threads.deferToThread(
-                cursor.fetchone
-            )
-        # check if the user exists.
-        if not user:
-            logger.info("User {} does not exist. Rejecting auth request".format(local_part))
+        try:
+            with psycopg2.connect(
+                dbname=self.config.db_name,
+                user=self.config.db_username,
+                password=self.config.db_password,
+                host=self.config.db_host,
+                port=self.config.db_port
+            ) as connection:
+                    with connection.cursor() as cursor:
+                        yield threads.deferToThread( # Don't think this is needed, but w/e
+                            cursor.execute,
+                            "SELECT username, encrypted_password FROM users WHERE username=%s",
+                            (local_part,)
+                        )
+                        user = yield threads.deferToThread(
+                            cursor.fetchone
+                        )
+                    # check if the user exists.
+                    if not user:
+                        logger.info("User {} does not exist. Rejecting auth request".format(local_part))
+                        defer.returnValue(False)
+                    logger.debug("User {} exists. Checking password".format(local_part))
+                    # user exists, check if the password is correct.
+                    encrypted_password = user[1]
+                    peppered_pass = "{}{}".format(password, self.config.pepper)
+                    if not (bcrypt.hashpw(peppered_pass, encrypted_password) == encrypted_password):
+                        logger.info("Password given for {} is wrong. Rejecting auth request.".format(local_part))
+                        defer.returnValue(False)
+                    # Ok, user's password is correct. check if the user exists in the homeserver db.
+                    # and create it if doesn't exist.
+                    if (yield self.account_handler.check_user_exists(user_id)):
+                        logger.info("User {} does exist in synapse db. Authentication complete".format(local_part))
+                        defer.returnValue(True)
+                    # User not in synapse db. need to create it.
+                    logger.info("User {} does not exist in synapse db. creating it.".format(local_part))
+                    user_id, access_token = (
+                        yield self.account_handler.register(localpart=local_part)
+                    )
+                    logger.info(
+                        "Registration based on diaspora complete. UserID: {}.".format(user_id)
+                    )
+                    logger.info("Confirming authentication request.")
+                    defer.returnValue(True)
+        except psycopg2.Error as e:
+            logger.warning("Error during diaspora authentication: {}".format(e))
             defer.returnValue(False)
-        logger.debug("User {} exists. Checking password".format(local_part))
-        # user exists, check if the password is correct.
-        encrypted_password = user[1]
-        peppered_pass = "{}{}".format(password, self.config.pepper)
-        if not (bcrypt.hashpw(peppered_pass, encrypted_password) == encrypted_password):
-            logger.info("Password given for {} is wrong. Rejecting auth request.".format(local_part))
-            defer.returnValue(False)
-        # Ok, user's password is correct. check if the user exists in the homeserver db.
-        # and create it if doesn't exist.
-        if (yield self.account_handler.check_user_exists(user_id)):
-            logger.info("User {} does exist in synapse db. Authentication complete".format(local_part))
-            defer.returnValue(True)
-        # User not in synapse db. need to create it.
-        logger.info("User {} does not exist in synapse db. creating it.".format(local_part))
-        user_id, access_token = (
-            yield self.account_handler.register(localpart=local_part)
-        )
-        logger.info(
-            "Registration based on diaspora complete. UserID: {}.".format(user_id)
-        )
-        logger.info("Confirming authentication request.")
-        defer.returnValue(True)
 
     @staticmethod
     def parse_config(config):