Select to view content in your preferred language

Intermittent pypyodbc connection

1991
4
Jump to solution
03-08-2022 07:40 AM
JohnConnor01
Occasional Contributor

Hello,

I am experiencing intermittent pypyodbc server connections. I am able to successfully run a simple query on a SQL Server database, and without making changes, the same query won't work 5 minutes later. Any help would be appreciated.

-JC from Boston

(Update: When looking at the debugger, I got an error regarding "Trying to use a closed connection")

(Update: I believe the problem is arising with numerous open/close connections strings in the program and then running certain lines when the connection is inadvertently still closed.)

0 Kudos
1 Solution

Accepted Solutions
KimGarbade
Frequent Contributor

If this is stand alone code (I.E. its not embedded in some other code I'm not seeing) than that is odd.  I can't replicate the problem, the code works for me (with a slight modification not to return all the records in my test table by using a Where clause on your Select statement)...

Could you have created the connection in one part of your code, closed it, and then tried to call it again later by accident?

For example the code in the first image works because the "conn.close()" is commented out.  The second image shows the results of the same code with "conn.close" included and the function call fails:

KimGarbade_1-1646767702207.png

 

KimGarbade_0-1646767613084.png

 

 

Only other thing I can think of is that some process running in the database is taking a long time (in computer terms longer than 5 seconds) and your connection is timing out. You could try adding a "timeout=some number of seconds" parameter to your connection string, but that doesn't sound right even to me.

View solution in original post

4 Replies
KimGarbade
Frequent Contributor

Would you share the piece of your code that is creating/using the connection and tell us what environment your creating and running the python in... python window, notebook, Spyder...?

JohnConnor01
Occasional Contributor

Thanks for the reply... using PyCharm see below:

 

import pandas as pd
import pypyodbc

connStr = pypyodbc.connect("DRIVER={SQL Server};SERVER=server;DATABASE=database;UID=uid;PWD=password")
query=pd.read_sql("select * from table;",connStr)
connStr.close()
print(query)

 

0 Kudos
KimGarbade
Frequent Contributor

If this is stand alone code (I.E. its not embedded in some other code I'm not seeing) than that is odd.  I can't replicate the problem, the code works for me (with a slight modification not to return all the records in my test table by using a Where clause on your Select statement)...

Could you have created the connection in one part of your code, closed it, and then tried to call it again later by accident?

For example the code in the first image works because the "conn.close()" is commented out.  The second image shows the results of the same code with "conn.close" included and the function call fails:

KimGarbade_1-1646767702207.png

 

KimGarbade_0-1646767613084.png

 

 

Only other thing I can think of is that some process running in the database is taking a long time (in computer terms longer than 5 seconds) and your connection is timing out. You could try adding a "timeout=some number of seconds" parameter to your connection string, but that doesn't sound right even to me.

JohnConnor01
Occasional Contributor

Thanks for looking into this Kim, I really appreciate your time! The code is embedded in a very long program, where there are multiple pypyodbc.connect and close() statements - which might be part of the problem - ie maybe its closing when I dont need it to. I might give the connection string a shot just to try it and see what happens. Thanks again for your time.