0

Greetings,
I am trying to do a reconnect type of thing for when sqlserver disconnects me i check and reconnect to execute a query. here is some code that i wrote, I really don't know what to use since there is not isconnected method or reconnect method in pymssql. Any ideas or thoughts on how to implement this would be great.

import pymssql
import pdb

#first time connected
dbConn=None
for i in range(4):
   try:
     dbConn = pymssql.connect('', 'user', 'signon', 'sqlserver01:1433')
     print "connected"
     break
   except pymssql.DatabaseError, err:
     print str(err)
     print "You have a connection issue"
     time.sleep(0.2)
   except:
     print "Another Exception occured, most likely User or Signon incorrect"
     time.sleep(0.2)

class SessionStuff():
#some more code
def Cursor():
    for i in range(4):
        try:
           c=dbConn.cursor()
           print "cursor created"
           print dbConn
           return c
           break
        except:
           print "error occured creating cursor, checking connection.."
           time.sleep(0.2)
def findPerson(name):
c=Cursor()
  for i in range(4):
        try:
           c.execute("select person from workoffice where name ='" ,name,"'"))
           result=c.fetchall()
           if len(result)==1:
              return c.fetchall()[0][0]
           else:
              return "%s:%s" % (str(host), str(port))
           break
     except pymssql.DatabaseError, err:
           print str(err)
           print "DB error retrying retrieve session"
           if "None" in str(err):
              print "created new instance of dbconnection old one dead"
              dbConn=pymssql.connect('', 'mtrader', 'mtr8d3r', 'sqldevch02:1433')
              c=dbConn.cursor()
              break
           else:
              time.sleep(0.2)

lets say i have this up and running then the database kills my connection my Cursor method still creates a cursor b/c it still has the object on there, so when it attempts to execute it goes to the except block the error: internal error: None (None) But I want to have like a reconnect attemp somewhere. I tried at the except but I still get the same issue. b/c it doesn't use the new isntance of the cursor c it still uses the old one. Any other posts or forums to direct me will be great. any help is appreciated thank you.

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by jrcagle
0

Seems to me that this line ought to be an error...

c=Cursor()

because you then try to execute commands on it ... but it's not assigned to any database, right?

Maybe I'm misunderstanding.
Jeff

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.