进行 SQL 数据库查询时随机发生“未知错误”

问题描述 投票:0回答:1

我正在尝试在浏览器中制作一个游戏,该游戏使用 Azure SQL 服务器来进行一些基本的多人游戏(例如聊天消息或物品交易),但我遇到了一个随机问题(例如 50% 的请求,主要是尝试选择玩家装备,但有时也会获取消息,或者在本例中获取玩家的等级)pymssql 连接崩溃,并由于“未知错误”而拒绝发送任何更多请求,完整代码可在此处找到:https://github.com/Edspeedy1/Server-Hosted-Website/blob/main/server.py

错误信息:

Error:  (0, b'Unknown error') 
---------------------------------------- 
Exception occurred during processing of request from ('127.0.0.1', 50549) 
Traceback (most recent call last): 
  File "src\\pymssql\\_pymssql.pyx", line 447, in pymssql._pymssql.Cursor.execute 
  File "src\\pymssql\\_mssql.pyx", line 1125, in pymssql._mssql.MSSQLConnection.execute_query 
  File "src\\pymssql\\_mssql.pyx", line 1156, in pymssql._mssql.MSSQLConnection.execute_query 
  File "src\\pymssql\\_mssql.pyx", line 1289, in pymssql._mssql.MSSQLConnection.format_and_run_query 
  File "src\\pymssql\\_mssql.pyx", line 1852, in pymssql._mssql.check_cancel_and_raise 
  File "src\\pymssql\\_mssql.pyx", line 1898, in pymssql._mssql.raise_MSSQLDatabaseException 
pymssql._mssql.MSSQLDatabaseException: (0, b'Unknown error') 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python311\Lib\socketserver.py", line 691, in process_request_thread 
    self.finish_request(request, client_address) 
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python311\Lib\socketserver.py", line 361, in finish_request 
    self.RequestHandlerClass(request, client_address, self) 
  File "c:\Users\Eric\code\Server Hosted Website\server.py", line 158, in __init__ 
    super().__init__(request, client_address, server) 
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python311\Lib\http\server.py", line 671, in __init__ 
    super().__init__(*args, **kwargs) 
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python311\Lib\socketserver.py", line 755, in __init__ 
    self.handle() 
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python311\Lib\http\server.py", line 436, in handle 
    self.handle_one_request() 
  File "C:\Users\Eric\AppData\Local\Programs\Python\Python311\Lib\http\server.py", line 424, in handle_one_request 
    method() 
  File "c:\Users\Eric\code\Server Hosted Website\server.py", line 196, in do_POST 
    self.get_basic_user_data(username) 
  File "c:\Users\Eric\code\Server Hosted Website\server.py", line 220, in get_basic_user_data 
    tempPlayerData = self.send_SQL_query('SELECT * FROM basicPlayerData where username = %s', (username,), DB_CONN, get=True, fetchAll=False) 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\Eric\code\Server Hosted Website\server.py", line 321, in send_SQL_query 
    raise e 
  File "c:\Users\Eric\code\Server Hosted Website\server.py", line 311, in send_SQL_query 
    cursor.execute(query, params) 
  File "src\\pymssql\\_pymssql.pyx", line 465, in pymssql._pymssql.Cursor.execute 
pymssql.exceptions.OperationalError: (0, b'Unknown error') 
----------------------------------------

这是执行 SQL 请求的代码:

    def send_SQL_query(self, query, params, db, get=False, fetchAll=True):
        try:
            cursor = db.cursor()
            cursor.execute(query, params)
            if get:
                if fetchAll:
                    return cursor.fetchall()
                else:
                    return cursor.fetchone()
        except Exception as e:
            print("Error: ", e)
            global DB_CONN
            # re-establish connection since the old one in no longer usable (just keeps saying unknown error)
            DB_CONN = pymssql.connect(server=server,user=user,password=password,database=database)
            raise e
        finally:
            if not get:
                db.commit()
            cursor.close()

该问题在大多数发送的选择请求上随机发生,但在插入请求上也已注意到。这个问题非常难以复制,直到我添加了一个名为 playerEquipment 的新表,现在它出现的频率增加了 10 倍。一旦发生“未知错误”,那么将来发送的任何 SQL 查询都将遇到相同的错误,但如果我用

DB_CONN = pymssql.connect(server=server,user=user,password=password,database=database)
重做连接,那么问题似乎会在接下来的 2-3 个请求中消失,直到最终再次失败由于“未知错误”。

python sql-server azure pymssql
1个回答
0
投票

套接字服务器上正在进行的一些多线程处理似乎导致了问题,因为库存页面有一些图像,所以几乎可以保证需要打开另一个线程来处理消息轮询。我希望错误消息实际上告诉我这是一个线程问题,但是哦,好吧。

简单修复:

class CustomRequestHandler(RangeHTTPServer.RangeRequestHandler):
    def __init__(self, request, client_address, server):
        # individual connection per instance of the request handler instead of 1 global connection
        self.DB_CONN = pymssql.connect(server=serverConnection, user=user, password=password, database=database) 
        super().__init__(request, client_address, server)
© www.soinside.com 2019 - 2024. All rights reserved.