Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible Try Catch usage. 1

Status
Not open for further replies.

petrosky

Technical User
Aug 1, 2001
512
0
0
AU
Hi there,

I am very new to trapping errors in PHP.

I have an application that has a fixed number of licenses for clients to connect.

One of my PHP scripts consumes a license.

Basically I want the script to attempt the connection and if it fails due to no available licenses to sleep for 30 seconds then try the connection again before proceeding with the rest of the script.

The script I am trying to rewrite in PHP is this Python script.

Code:
while True:
  try:
    cnxn = pyodbc.connect('DRIVER={D3 ODBC Driver};SERVER=192.168.1.1;PORTNUMBER=1603;VIRTUALMACHINE=192.168.1.1;UID=xx;PWD=;ACCOUNT=xx;ACPASSWORD=;ConnectDialog=No;AConnectDialog=No;D3VERSION=710;D3PWD=;NotNullConstraint=Yes;LoginTimeout=20;')
    cursor = cnxn.cursor()
    cursor.execute("SELECT IVMST.RSV_DESC, IVMST.AVG_COST, IVMST.COST, IVMST.INV, IVMST.MU1, IVMST.MU2, IVMST.MU3, IVMST.N_12MON, IVMST.SALE, IVMST.VENDOR, IVMST.RSV_GROUP, IVMST.IVMST_id, IVMST.BRAND, IVMST.RETAIL, IVMST.SUPNAME, IVMST.S1INV, IVMST.S2INV FROM IVMST IVMST")
    mcursor.execute("DELETE FROM d3_import")
    mconn.commit()
    for row in cursor:
      c = 0
      mcursor.execute("""INSERT INTO d3_import VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE rsv_desc=%s, avg_cost=%s, cost=%s, inv=%s, mu1=%s, mu2=%s, mu3=%s, n_12mon=%s, sale=%s, vendor=%s, rsv_group=%s, brand=%s, retail=%s, supname=%s, s1inv=%s, s2inv=%s""", (row[11], row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[12], row[13], row[14], row[15], row[16], row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[12], row[13], row[14], row[15], row[16]))
      mconn.commit()
      c = 0
      for v in row:
        if row[c] == None:
          row[c] = ""
        c += 1
      f.write(str(row[11]) + "," + str(row[0]) + "," + str(row[1]) + "," + str(row[2]) + "," + str(row[3]) + "," + str(row[4]) + "," + str(row[5]) + "," + str(row[6]) + "," + str(row[7]) + "," + str(row[8]) + "," + str(row[9]) + "," + str(row[10]) + "," + str(row[11]) + "," + str(row[14]) + "," + str(row[15]) + "," + str(row[16]) + "\n")
    break
  except:
    print "Connection failed, probably too many licenses used.  Trying again in 5 seconds..."
    time.sleep(5)

I just can't get my head around getting the script to retry the connection on failure.

Peter.

Remember- It's nice to be important,
but it's important to be nice :)
 
Well, you'd something like this:
Code:
$connected=FALSE;
while(! $connected):
      try{[i]... open the connection...[/i]
          $connected=TRUE; }
      catch(Exception $exc)
         {[i]...[/i]}
endwhile;

But please note a few things:

If the PHP script drives a web site, it is not very kinds to make the loading of the web page wait for 5 or more seconds. Better send a page refresh instead.

The output may be buffered, so the line "trying again ..." may not be displayed at all until the full page is sent.

What kind of database is "D3"?


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Hi DonQuihote.

Thanks for the heads up.

Yes it touchs a D3 database. The more I learn about these web languages, the more I realise that I know so very little.

If I set a max execution time will that be the limit of the try/catch loop?

Thanks for your help.

Remember- It's nice to be important,
but it's important to be nice :)
 
Well, yes, the maximum execution time limits everything. But I never saw an endless "try opening a database" loop before. If the database server is down, it will try "forever", until it is stopped by the max. execution time. I just try once, and if that fails I display a message to the user that the site is temporarily down. I also try to mail myself of that condition when that occurs and write to a log file if I can't. I hardly ever get these mails and the log files are all empty, so connecting is usually no problem. But that is with a MySQL connection. Is connecting to D3 more difficult?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Hi again Don,

Merry Christmas.

No the connection should go through with no problems. It is only when our licenses are consumed that this process needs to retry.

It basically scrapes a products table periodically for uploading to an external website.

Thanks again for your help and I'll let you know how it goes when I am back at work on Tuesday.

Peter.

Remember- It's nice to be important,
but it's important to be nice :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top