Database connection closed in unload(), still closed after the next load()
Posted: Wed Nov 25, 2015 3:24 pm
I've been working on a new Hero-Wars based Warcraft mod for CS:GO with Predz. I took an approach where I create a Database class which connects to the SQL database in its __init__. The database object is initialized in the plugin's load() function. In the unload() function, I call the database's close() method which commits changes, closes the connection, and sets the connection to None.
It all works fine, until I reload the plugin, which is when I get an error that the database object's connection attribute is NoneType object (or if I don't set it to None in Database.close(), I get a "cannot operate on closed database" error):
Here's the GitHub repository: https://github.com/Warcraft-GO-Team/Warcraft-GO
Here's the Database class: https://github.com/Warcraft-GO-Team/Warcraft-GO/blob/master/addons/source-python/plugins/wcgo/database.py
Here are the load() and unload() functions: https://github.com/Warcraft-GO-Team/Warcraft-GO/blob/master/addons/source-python/plugins/wcgo/wcgo.py#L38-L66
I've been trying to figure this out for quite a while. It's as if the plugin's load() is called before the previous unload() is finished, but I tried adding prints and that was not true at all. I also tried "sp unload wcgo" and then wait a few seconds before "sp load wcgo" without any success, so it's not that. Somehow the database object is not reset when I reload the plugin. Any ideas?
It all works fine, until I reload the plugin, which is when I get an error that the database object's connection attribute is NoneType object (or if I don't set it to None in Database.close(), I get a "cannot operate on closed database" error):
Code: Select all
sp reload wcgo
[SP] Unloading plugin 'wcgo'...
[SP] Successfully unloaded plugin 'wcgo'.
[SP] Loading plugin 'wcgo'...
[SP] Successfully loaded plugin 'wcgo'.
[SP] Caught an Exception:
Traceback (most recent call last):
File '..\addons\source-python\packages\source-python\events\listener.py', line
92, in fire_game_event
callback(game_event)
File '..\addons\source-python\plugins\wcgo\wcgo.py', line 103, in _on_player_s
pawn
database.save_player(player)
File '..\addons\source-python\plugins\wcgo\database.py', line 50, in save_play
er
self.connection.execute(
AttributeError: 'NoneType' object has no attribute 'execute'
Here's the GitHub repository: https://github.com/Warcraft-GO-Team/Warcraft-GO
Here's the Database class: https://github.com/Warcraft-GO-Team/Warcraft-GO/blob/master/addons/source-python/plugins/wcgo/database.py
Here are the load() and unload() functions: https://github.com/Warcraft-GO-Team/Warcraft-GO/blob/master/addons/source-python/plugins/wcgo/wcgo.py#L38-L66
I've been trying to figure this out for quite a while. It's as if the plugin's load() is called before the previous unload() is finished, but I tried adding prints and that was not true at all. I also tried "sp unload wcgo" and then wait a few seconds before "sp load wcgo" without any success, so it's not that. Somehow the database object is not reset when I reload the plugin. Any ideas?