Re: [Modeling-users] leaving the database open
Status: Abandoned
Brought to you by:
sbigaret
From: Sebastien B. <sbi...@us...> - 2004-02-29 12:53:19
|
Hi and sorry for the delay in the answer, "Ernesto Revilla" <er...@si...> wrote: > Hi all, >=20 > strange thing (for me) that the AdaptorContext does not define a close > operation. >=20 > Working with SQLite, I only see code to close the database when all chann= els > are closed and the os.environ.get('MDL_TRANSIENT_DB_CONNECTION') is set (= to > something different as '' and 0). You're right: the connection to the db is only closed in this situation. So, if you need to close it after an operation, you have to set MDL_TRANSIENT_DB_CONNECTION to true before that operation. The mechanism will probably change however in the near future; maybe a delegate can handle those situations. Now if you need a method to close the database connection after some operations took place, this can be made the way it is in AbstractDBAPI2AdaptorContext.adaptorChannelDidClose() -- the important thing is to set self._cnx to None so that it automatically reconnects when needed. If you're doing this in a multi-thread environment with many ECs, you should lock the ObjectStoreCoordinator (which is any EC's rootObjectStore()) before closing the connection. > The problem arose when I executed the mdl_create_DB_schema.py with > execfile from within my PyModel definition with execfile with the -R > option. The database was open (after running a schema creation). So > rerunning the script with some changes in the model, didn't drop the > DB. >=20 > So I have the following questions: > * does loading the model open the database? Not at all --if you want to see when the db connection is opened you can MDL_ENABLE_DATABASE_LOGGING to "yes". > * is it possible to close explicitly the database, i.e. context, so > that after the creation of the schema the database is closed? I'll suggest setting MDL_TRANSIENT_DB_CONNECTION > * what does the MDL_TRANSIENT_DB_CONNECTION mean: > a) do not close the DB when all cursor have closed, or > b) the opposite, close the DB when all channels are closed? If set to true, it closes the db when the last opened channel is closed. The default behaviour is now to keep the db connection opened. BTW I've just realized that the User's Guide says exactly the opposite, this should be fixed. >=20 > If mdl_generate_DB_schema.py is invoked with the -R option, using > SQLiteAptor, and the file does not exist, an error is rosen. Perhaps this > could be ignored. (-R is: drop the database if it exists). This could be > done with the following code in > dropDatabaseWithAdministrativeConnectionDictionary: >=20 > try: > os.unlink(administrativeConnectionDictionary['database']) > except OSError, exc: > # ignore if file does not exist, else, reraise exception > if not exc.errno=3D=3D2: > raise Well, the error shouldn't be ignored when the file does not exist to be consistent with other adaptors which also raise when an attempt is made to drop an non-existing database. In the script, -R is: drop the db and recreate it and agreed, it fails if the database does not exist; if you want to ignore the error, the option -i/--ignore-errors is your friend :) Now if you want to explictly ask for DB creation (not re-creation), then you want to use -C. I'm not sure I fully understood your use-case with executing the script, hopefully this answers your question. -- S=E9bastien. |