SqlAlchemy 「Too many connections」

2019-06-12:真 有用的是這個:html

https://blog.csdn.net/weiwangchao_/article/details/80185009mysql

一、在使用 create_engine建立引擎時,若是默認不指定鏈接池設置的話,通常狀況下,SQLAlchemy會使用一個 QueuePool綁定在新建立的引擎上。並附上合適的鏈接池參數。sql

二、在這種狀況下,當你使用了session後就算顯式地調用session.close(),也不能把鏈接關閉。鏈接會由QueuePool鏈接池進行管理並複用。數據庫

三、若是想禁用SQLAlchemy提供的數據庫鏈接池,只須要在調用create_engine是指定鏈接池爲NullPool,SQLAlchemy就會在執行session.close()後馬上斷開數據庫鏈接。固然,若是session對象被析構可是沒有被調用session.close(),則數據庫鏈接不會被斷開,直到程序終止。api

from sqlalchemy.pool import NullPool

#鏈接數據庫
def getEngine():
    engine = create_engine('mysql://root:123456@127.0.0.1/baa?charset=utf8mb4', poolclass=NullPool, echo=False)
    #print(engine)
    return engine

def getSession():
    engine = getEngine()
    BaseMode.metadata.create_all(engine)## 數據庫生成表
    # Session = sessionmaker(bind=engine)
    # session = Session()

    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    return session

 

 File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1040, 'Too many connections') (Background on this error at: http://sqlalche.me/e/e3q8)

Process finished with exit code 1

 

 

一、pandas dataframe to_sql()

#鏈接數據庫
def getEngine():
    engine = create_engine('mysql://root:123456@127.0.0.1/databaseName?charset=utf8mb4',echo=False)
    #print(engine)
    return engine
def getSession():
    engine = getEngine()
    BaseMode.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)
    session = Session()
    return session

 

def get_stock_daily(ts_code, start_date,end_date,startTime):
    # print( ts_code + "  " + str(start_date) + "  " + str(end_date))
    api = ts.pro_api(token)

    try:
        df = ts.pro_bar(pro_api=api, ts_code=ts_code, start_date=start_date, end_date=end_date)  # 【獲取數據】
        if df is None:
            print("沒有獲取到數據")
            return None

        print("data count: " + str(len(df)))
        df['trade_date'] = pd.to_datetime(df['trade_date'])  # 交易日期字符串轉換成日期格式
 conn = cheDbUtil.getEngine() try:
            df.to_sql('b_stock_daily', con=conn, if_exists='append', index=False)  # replace/append/fail 【save】
        except Exception as e:
            traceback.print_exc()
            print("get_stock_daily() in BaaBusiniessPro.py XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
            return None
        finally:
            conn.dispose()
    except Exception as e:
        sleepNeed = 60
        print("Start to sleep  " + str(sleepNeed) + '')
        print("當前耗時: " + str(round((time.time() - startTime), 2) / 60) + ' 分鐘')
        time.sleep(sleepNeed)  # 休眠 ? 秒

二、SqlAlchemy  操做 mysql

#獲取日行情最後一條記錄的日期
def getMaxTradeDateForStockDaily(ts_code):
    try:
        session = getSession()
        rows = session.query(func.max(StockDaily.trade_date)).filter(StockDaily.ts_code == ts_code).one()
        # print(str(len(rows)))
        # print(type(rows))
        # print(str(rows))
        if rows is not None and len(rows) == 1:
            return rows[0]
        return None
    except Exception as e:
        traceback.print_exc()
        print("getMaxTradeDateForStockDaily() XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
        return None
    finally:
       session.close()
相關文章
相關標籤/搜索