使用sqlalchemy和pyodbc在短时间内多次连接到sql server 2008时出现TCP错误

时间:2017-02-22 17:40:34

标签: python tcp sqlalchemy pyodbc

我试图在短时间内从python连接到sql server数据库4次以进行一些数据拉动,并且在第3次拉动时失败。

有趣的是,只有2个数据提取查询的其他脚本工作得很好。但在这种特殊情况下,有4个,一旦到达第3个查询就会失败。我也尝试使用连接池并指定池大小。

import pandas as pd
import pyodbc
import sqlalchemy
from pandas import Series,DataFrame
import numpy as np
import itertools
cnx=sqlalchemy.create_engine("mssql+pyodbc://username:password@10.0.0.1:1433/Basis?driver=/opt/microsoft/sqlncli/lib64/libsqlncli-11.0.so.1790.0",pool_size=4, max_overflow=0)

def get_data():
    sql1 = "select   Send_Customer from Pretty_Txns with (noLock)  where Send_Date >='2017-01-01' and Send_Date <= '2017-01-02'"
    sql2 = "select   Pay_Customer from Pretty_Txns with (noLock)  where Pay_Date >='2017-01-01' and Pay_Date <= '2017-01-02'"
    df1 = pd.read_sql(sql1,cnx)
    df2 = pd.read_sql(sql2,cnx)
    l1 = df1.values.tolist()
    l2 = df2.values.tolist()
    l1 = [item for sublist in l1 for item in sublist]
    l2 = [item for sublist in l2 for item in sublist]
    Send_list =  [l1[i:i + 2000] for i in xrange(0, len(l1),2000)]
    Pay_list =  [l2[i:i + 2000] for i in xrange(0, len(l2),2000)]
    for list1,list2 in itertools.izip_longest(Send_list,Pay_list):
        placeholders1 = ','.join('?' for i in range(len(list1)))
        placeholders2 = ','.join('?' for i in range(len(list2)))
        sql1="select  * from Pretty_Txns  with (noLock) where Send_Date >='2017-01-01' and Send_Date <= '2017-01-02' and Send_Customer in (%s)" % placeholders1
        sql2="select  * from Pretty_Txns with (noLock)  where Pay_Date >='2017-01-01' and Pay_Date <= '2017-01-02' and Pay_Customer in (%s)" % placeholders2
        df_send = pd.read_sql(sql1,cnx,params=list1)
        df_pay = pd.read_sql(sql2,cnx,params=list2)
        df_send['time_diff'] = pd.to_numeric(df_send['Pay_Time']-df_send['Send_Time'])
        df_send['time_diff'] = df_send['time_diff']/1000000000
        df_pay['time_diff'] = pd.to_numeric(df_pay['Pay_Time']-df_pay['Send_Time'])
        df_pay['time_diff'] = df_pay['time_diff']/1000000000
        return  {"df_send":df_send,"df_pay":df_pay}

所以在第3个查询中失败并出现以下错误:

      sql1="select  * from Pretty_Txns  with (noLock) where Send_Date >='2017-01-01' and Send_Date <= '2017-01-02' and Send_Customer in (%s)" % placeholders1
      DBAPIError: (pyodbc.Error) ('08S01', '[08S01] [Microsoft][SQL Server Native Client 11.0]TCP Provider: Error code 0x68 (104)

所以,期待一些提示和指示。

0 个答案:

没有答案