diff --git a/db/libdb.pm.in b/db/libdb.pm.in
index 4a1de73c1df23a1eaf94c3580aaa11b5b9db46d4..e78145f59b627d4e23b9bca5a3111191cd3a4fb4 100644
--- a/db/libdb.pm.in
+++ b/db/libdb.pm.in
@@ -152,9 +152,11 @@ sub TBDBConnect()
     }
     my $dbuser = "$scriptname:$name";
 
-    
-    while ($maxtries && !defined($DB)) {
+    while ($maxtries) {
 	$DB = Mysql->connect("localhost", $DBNAME, $dbuser, "none");
+	if (defined($DB)) {
+	    last;
+	}
 	$maxtries--;
 	sleep(1);
     }