Skip to content

Commit a20fac0

Browse files
committed
Fix in SQLite Connection. There were never closed
* There is still a fix when we want to close a SQLite connection created in a different thread
1 parent 54117bb commit a20fac0

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

MLC/Simulation.py

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def add_generation(self, population):
4646
def erase_generations(self, from_generation):
4747
self._mlc_repository.remove_population_from(from_generation)
4848

49+
def close(self):
50+
self._mlc_repository.close()
51+
4952
@staticmethod
5053
def create_empty_population_for(generation):
5154
from MLC.Population.Population import Population

MLC/api/Experiment.py

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def get_simulation(self):
6464
Config.get_instance().read(self._config_file)
6565
set_logger(Config.get_instance().get('LOGGING', 'logmode'))
6666

67+
if Experiment.__last_simulation:
68+
Experiment.__last_simulation.close()
69+
6770
self._simulation = Simulation(self._name)
6871
Experiment.__last_simulation = self._simulation
6972

MLC/api/MLCLocal.py

100644100755
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def close_experiment(self, experiment_name):
162162
# problems between Evaluation and Preevaluation scripts from other projects
163163
experiment_dir = os.path.join(self._working_dir, experiment_name)
164164
sys.path.remove(experiment_dir)
165+
self._open_experiments[experiment_name].get_simulation().close()
165166
del self._open_experiments[experiment_name]
166167

167168
def new_experiment(self, experiment_name,
@@ -246,10 +247,12 @@ def delete_experiment(self, experiment_name):
246247

247248
experiment_dir = os.path.join(self._working_dir, experiment_name)
248249
try:
249-
shutil.rmtree(experiment_dir)
250250
del self._experiments[experiment_name]
251251
if experiment_name in self._open_experiments:
252+
simulation = self._open_experiments[experiment_name].get_simulation().close()
252253
del self._open_experiments[experiment_name]
254+
255+
shutil.rmtree(experiment_dir)
253256
except OSError:
254257
logger.info("[MLC_LOCAL] Error while trying to delete experiment file: {0}".format(file))
255258
raise

MLC/db/mlc_repository.py

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ def _add_data(self, generation, cost, evaluation_time):
7575
class MLCRepository:
7676
_instance = None
7777

78+
# Close the Database opened
79+
def close():
80+
raise NotImplementedError("This method must be implemented")
7881
# operation over generations
7982
def add_population(self, population):
8083
raise NotImplementedError("This method must be implemented")

MLC/db/sqlite/sqlite_repository.py

100644100755
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from MLC.arduino.protocol import ProtocolConfig
3333
from MLC.arduino.connection.serialconnection import SerialConnectionConfig
3434
from MLC.arduino.boards import types
35+
import traceback
3536

3637
logger = get_gui_logger()
3738

@@ -66,6 +67,9 @@ def __init__(self, database, init_db=False):
6667
self.__next_individual_id = 1 if not self.__individuals else max(self.__individuals.keys()) + 1
6768
self.__individuals_to_flush = {}
6869

70+
def close(self):
71+
self._conn.close()
72+
6973
def __initialize_db(self):
7074
cursor = self._conn.cursor()
7175

@@ -84,11 +88,19 @@ def __initialize_db(self):
8488
self._conn.commit()
8589

8690
def __get_db_connection(self, reopen_connection=False):
87-
# TODO: Workaround. SQLite throw an exception when a connection is used in different threads. To solve it,
88-
# we create a new connection every time a new connection is delivered
91+
# TODO: Workaround. SQLite throw an exception when a connection is used
92+
# in different threads. To solve it, we create a new connection
93+
# every time a new connection is delivered
8994
if self._database != SQLiteRepository.IN_MEMORY_DB:
90-
self._conn = sqlite3.connect(self._database)
95+
# Close the previous connection
96+
try:
97+
self._conn.close()
98+
except sqlite3.ProgrammingError:
99+
# FIXME: There are open connections that cannot be closed for
100+
# the Thread bug. See what can be done
101+
pass
91102

103+
self._conn = sqlite3.connect(self._database)
92104
return self._conn
93105

94106
def __insert_individuals_pending(self, individual):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
[LOGGING]
2+
logmode = testing
3+
14
[PARAMS]
25
test_param = test_value

0 commit comments

Comments
 (0)