Commit aa73eaf0 authored by shenshuo's avatar shenshuo

修改db连接

parent cd7d726c
...@@ -8,6 +8,7 @@ role : 数据库连接 ...@@ -8,6 +8,7 @@ role : 数据库连接
import pymysql import pymysql
from urllib.parse import quote_plus from urllib.parse import quote_plus
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.pool import NullPool
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from .consts import const from .consts import const
from .configs import configs from .configs import configs
...@@ -28,7 +29,8 @@ def init_engine(**settings): ...@@ -28,7 +29,8 @@ def init_engine(**settings):
dbname = db_conf[const.DBNAME_KEY] dbname = db_conf[const.DBNAME_KEY]
engine = create_engine('mysql+pymysql://{user}:{pwd}@{host}:{port}/{dbname}?charset=utf8' engine = create_engine('mysql+pymysql://{user}:{pwd}@{host}:{port}/{dbname}?charset=utf8'
.format(user=dbuser, pwd=quote_plus(dbpwd), host=dbhost, port=dbport, dbname=dbname), .format(user=dbuser, pwd=quote_plus(dbpwd), host=dbhost, port=dbport, dbname=dbname),
logging_name=dbkey) # logging_name=dbkey)
logging_name=dbkey, poolclass=NullPool)
engines[dbkey] = engine engines[dbkey] = engine
...@@ -58,9 +60,9 @@ class DBContext(object): ...@@ -58,9 +60,9 @@ class DBContext(object):
self.__engine = engine self.__engine = engine
self.need_commit = need_commit self.need_commit = need_commit
@property # @property
def db_key(self): # def db_key(self):
return self.__db_key # return self.__db_key
@staticmethod @staticmethod
def __get_db_engine(db_key, **settings): def __get_db_engine(db_key, **settings):
...@@ -77,6 +79,7 @@ class DBContext(object): ...@@ -77,6 +79,7 @@ class DBContext(object):
return self.__session return self.__session
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
print(exc_type, exc_val, exc_tb)
if self.need_commit: if self.need_commit:
if exc_type: if exc_type:
self.__session.rollback() self.__session.rollback()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment