Discussion:
[Modeling-users] leaving the database open
Ernesto Revilla
2004-02-25 21:58:09 UTC
Permalink
Hi all,

strange thing (for me) that the AdaptorContext does not define a close operation.

Working with SQLite, I only see code to close the database when all channels are closed and the os.environ.get('MDL_TRANSIENT_DB_CONNECTION') is set (to something different as '' and 0).

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.

So I have the following questions:
* does loading the model open the database?
* is it possible to close explicitly the database, i.e. context, so that after the creation of the schema the database is closed?
* 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 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:

try:
os.unlink(administrativeConnectionDictionary['database'])
except OSError, exc:
# ignore if file does not exist, else, reraise exception
if not exc.errno==2:
raise

with best regards, Erny
Ernesto Revilla
2004-02-26 10:51:03 UTC
Permalink
Dear all,

* how do I query for DateTime fields?
* modeling validates mx.DateTime.DateTime and mx.DateTime.Date objects, but not mx.DateTime.Time. (efectively DateTimeDelta). Is it possible to handle these?

Best regards, Erny
Sebastien Bigaret
2004-02-29 11:30:02 UTC
Permalink
Post by Ernesto Revilla
Dear all,
* how do I query for DateTime fields?
Example:

ec.fetch('Writer', 'birthday == "1939-10-27 08:31:15"')
or
ec.fetch('Writer', 'birthday like "1939-10-27*"')
Post by Ernesto Revilla
* modeling validates mx.DateTime.DateTime and mx.DateTime.Date objects, but
not mx.DateTime.Time. (efectively DateTimeDelta). Is it possible to handle
these?
Yes it is: try the attached patch!

-- Sébastien.


------------------------------------------------------------------------
diff -u -r1.21 Attribute.py
--- Attribute.py 22 Feb 2004 17:31:37 -0000 1.21
+++ Attribute.py 29 Feb 2004 13:10:39 -0000
@@ -56,7 +56,7 @@
# python
import types, string, sys, time
from cgi import escape
-from mx.DateTime import DateTime, DateFrom
+from mx.DateTime import DateTime, DateFrom, Time

# Module constants
# useless, should be dropped
@@ -76,7 +76,7 @@

def date_types():
"Returns the set of valid date types"
- avail_types=[ type(DateTime(0)) ]
+ avail_types=[ type(DateTime(0)), type(Time(0)) ]
try:
import DCOracle2
avail_types.append(type(DCOracle2.Date(0,0,0)))
------------------------------------------------------------------------
Sebastien Bigaret
2004-02-29 10:54:00 UTC
Permalink
Hi and sorry for the delay in the answer,
Post by Ernesto Revilla
Hi all,
strange thing (for me) that the AdaptorContext does not define a close operation.
Working with SQLite, I only see code to close the database when all channels
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.
Post by Ernesto Revilla
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.
* 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".
Post by Ernesto Revilla
* 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
Post by Ernesto Revilla
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.
Post by Ernesto Revilla
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
os.unlink(administrativeConnectionDictionary['database'])
# ignore if file does not exist, else, reraise exception
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ébastien.
Loading...