diff --git a/db.py b/db.py
index 26eb9219052feb05feb43c8a01ecb8ce4f4f7904..105c8dcd24946759ea4cae043e14aef111a26267 100644
--- a/db.py
+++ b/db.py
@@ -67,7 +67,15 @@ elif config['DB_ENGINE'] == 'mysql':
 		else:
 			db = mysql.connector.connect(user=config['MYSQL_USER'], password=config['MYSQL_PASSWD'], unix_socket=config.get('MYSQL_UNIX', None))
 		cur = db.cursor()
-		cur.execute(operation)
+		tries = 0
+		while (tries < 10):
+			try:
+				cur.execute(operation)
+			except mysql.connector.errors.InternalError as e:
+				if e.msg == 'Deadlock found when trying to get lock; try restarting transaction':
+					tries += 1
+				else:
+					raise
 		rows = []
 		try:
 			rows = cur.fetchall()
diff --git a/jobs.py b/jobs.py
index 884574d0d722f01d62b1baef6d1288c40a3d516b..0696c730c767e8d0806383e4d43f9605bfd6a61e 100644
--- a/jobs.py
+++ b/jobs.py
@@ -155,10 +155,10 @@ def jobs_schedule(hostname):
 				return 'no jobs', 503
 			modify('UPDATE jobs SET state="scheduled", worker = ?, time_scheduled = ? WHERE id = ?', hostname, datetime.now(), job['id']) 
 			query("COMMIT")
-		except OperationalError:
+		except:
 			tries += 1
 			job = None
-			sleep(random.randinti(0,50))
+			sleep(random.random())
 			if tries > 10:
 				return 'no jobs', 503