Skip to content

Commit

Permalink
loop app
Browse files Browse the repository at this point in the history
  • Loading branch information
yordanovsstoyan committed Nov 12, 2024
1 parent aeef0fb commit 6eeacc4
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions mysql-connect-app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
database = os.getenv('DB_NAME', 'mydb')

connection = None
try:
# Connect to DB
connection = pymysql.connect(
host=host,
user=user,
password=password,
database=database
)
print(f"Connection to MySQL database successful! Hello World")

except pymysql.MySQLError as e:
print(f"Error connecting to MySQL database: {e}")

finally:
if connection:
connection.close()
print("Connection closed.")

while True:
try:
# Try connecting to the DB
connection = pymysql.connect(
host=host,
user=user,
password=password,
database=database
)
print("Connection to MySQL database successful! Hello World")

time.sleep(5)

except pymysql.MySQLError as e:
print(f"Error connecting to MySQL database: {e}")
print("Retrying in 5 seconds...")
time.sleep(5)

finally:
if connection:
connection.close()
print("Connection closed.")

0 comments on commit 6eeacc4

Please sign in to comment.