aboutsummaryrefslogtreecommitdiffstats
path: root/app/discover/fetchers/db/db_access.py
diff options
context:
space:
mode:
authorKoren Lev <korenlev@gmail.com>2017-09-29 01:38:18 +0300
committerKoren Lev <korenlev@gmail.com>2017-09-29 01:38:18 +0300
commitd32f75145676bacefde0d08a14680a5984623451 (patch)
tree4b5eaf1107e6973b1eac636309a99c83074acbfc /app/discover/fetchers/db/db_access.py
parent0c5426cd309d720db1e30641e43d311ee0b751b0 (diff)
release 1.0 calipso for opnfv apex
Change-Id: I3e63cd27c5f4d3756e67a07c749863a68e84dde2 Signed-off-by: Koren Lev <korenlev@gmail.com>
Diffstat (limited to 'app/discover/fetchers/db/db_access.py')
-rw-r--r--app/discover/fetchers/db/db_access.py43
1 files changed, 31 insertions, 12 deletions
diff --git a/app/discover/fetchers/db/db_access.py b/app/discover/fetchers/db/db_access.py
index 49fdb5e..64d7372 100644
--- a/app/discover/fetchers/db/db_access.py
+++ b/app/discover/fetchers/db/db_access.py
@@ -7,6 +7,7 @@
# which accompanies this distribution, and is available at #
# http://www.apache.org/licenses/LICENSE-2.0 #
###############################################################################
+import functools
import mysql.connector
from discover.configuration import Configuration
@@ -15,6 +16,24 @@ from discover.scan_error import ScanError
from utils.string_utils import jsonify
+def with_cursor(method):
+ @functools.wraps(method)
+ def wrap(self, *args, **kwargs):
+ self.connect_to_db(DbAccess.query_count_per_con >= 25)
+ DbAccess.query_count_per_con += 1
+ cursor = DbAccess.conn.cursor(dictionary=True)
+ try:
+ res = method(self, *args, cursor=cursor, **kwargs)
+ DbAccess.conn.commit()
+ return res
+ except:
+ DbAccess.conn.rollback()
+ raise
+ finally:
+ cursor.close()
+ return wrap
+
+
class DbAccess(Fetcher):
conn = None
query_count_per_con = 0
@@ -47,10 +66,9 @@ class DbAccess(Fetcher):
return
DbAccess.query_count_per_con = 0
- @staticmethod
- def get_neutron_db_name():
+ @with_cursor
+ def get_neutron_db_name(self, cursor=None):
# check if DB schema 'neutron' exists
- cursor = DbAccess.conn.cursor(dictionary=True)
cursor.execute('SHOW DATABASES')
matches = [row.get('Database', '') for row in cursor
if 'neutron' in row.get('Database', '')]
@@ -68,6 +86,8 @@ class DbAccess(Fetcher):
self.log.info("DbAccess: ****** forcing reconnect, " +
"query count: %s ******",
DbAccess.query_count_per_con)
+ DbAccess.conn.commit()
+ DbAccess.conn.close()
DbAccess.conn = None
self.conf = self.config.get("mysql")
cnf = self.conf
@@ -76,16 +96,15 @@ class DbAccess(Fetcher):
cnf["user"], cnf["pwd"],
cnf["schema"])
- def get_objects_list_for_id(self, query, object_type, id):
- self.connect_to_db(DbAccess.query_count_per_con >= 25)
- DbAccess.query_count_per_con += 1
+ @with_cursor
+ def get_objects_list_for_id(self, query, object_type, object_id,
+ cursor=None):
self.log.debug("query count: %s, running query:\n%s\n",
str(DbAccess.query_count_per_con), query)
- cursor = DbAccess.conn.cursor(dictionary=True)
try:
- if id:
- cursor.execute(query, [str(id)])
+ if object_id:
+ cursor.execute(query, [str(object_id)])
else:
cursor.execute(query)
except (AttributeError, mysql.connector.errors.OperationalError) as e:
@@ -93,13 +112,13 @@ class DbAccess(Fetcher):
self.connect_to_db(True)
# try again to run the query
cursor = DbAccess.conn.cursor(dictionary=True)
- if id:
- cursor.execute(query, [str(id)])
+ if object_id:
+ cursor.execute(query, [str(object_id)])
else:
cursor.execute(query)
rows = []
- for row in cursor:
+ for row in cursor.fetchall():
rows.append(row)
return rows