I am connecting to the PA MySQLDb using sshtunnel, mysqlconnector with the code sample that has been shared as an example.
But when I put the same code (with block) under try except the system hangs and ultimately fails in mycursor.execute(Query) . Why this is so. It takes a lot of time to execute the connection.cursor statement.
I remove the try/except block. The code functions smoothly.
I need the try except block to retry the connection .
==========================================================================
import mysql.connector
import sshtunnel
sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0
connection=None
mycursor=None
try:
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username='Pipsy100', ssh_password='XXXXXXX',
remote_bind_address=('Pipsy100.mysql.pythonanywhere-services.com', 3306)
) as tunnel:
connection = mysql.connector.connect(
user='Pipsy100', password='XXXXXXX',
host='127.0.0.1', port=tunnel.local_bind_port,
database='Pipsy100$mysite',
)
mycursor = connection.cursor()
except Exception as e:
print("Damage")
# Retry again
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
mycursor.close()
connection.close()
==========================================================================