[PonyORM-list] Example with disconnect()

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Thu Dec 11 20:30:33 UTC 2014


Ok, I understand now. I suggest you to use the following pattern:

It is possible to define entities inside a function. Currently you place
entity definitions in modules: moduleDefineEntitys1, moduleDefineEntitys2,
etc. But inside each module you can write a function which will accept db
object as a parameter and define entities for that db object. Then the
application structure will look like this:

entities1.py - some of entities defined here inside define_entities()
funciton:

    def define_entities(db):

        class MyEntity1(db.Entity):
            a = Required(int)
            b = Required(int)

        class MyEntity2(db.Entity):
            c = Required(int)
            d = Required(int)

entities2.py - other entities defined here inside define_entities()
funciton:

    def define_entities(db)

        class MyEntity3(db.Entity):
            e = Required(int)
            f = Required(int)

        class MyEntity4(db.Entity):
            g = Required(int)
            h = Required(int)

all_entities.py  - this module define all entities inside define_entities()
function:

    import entities1, entities2

    def define_entities(db):
        entities1.define_entities(db)
        entities2.define_entities(db)

geneticDBMScommon.py - contains `connect` function as in you current code:

    from pony.orm import *
    from all_entities import define_entities

    def connect(...):
        db = Database()  # create new database object
        define_entities(db)  # define entities for this database
        db.bind(...)  # bind this database
        ...
        return db

some_program.py - command line program which uses `connect` function:

    from pony.orm import *
    from geneticDBMSCommon import Connect

    db = Connect(...)  # get new database object
    theDB.db = db  # store db object to global variable if necessary

    with db_session:  # process data
        result = select(e for e in db.Entity1)


We use similar approach when writing pony.orm tests. Hope it will work for
you.

Regards,
Alexander



On Thu, Dec 11, 2014 at 9:30 PM, Goldberg, Arthur P <
arthur.p.goldberg at mssm.edu> wrote:
>
>  Thanks AK!
>
>  but that doesn't quite work.
> Here's my basic pattern, reusing general purpose functionality and
> separating entity declarations from dbms applications.
>
>  One module creates the database handle: theDB.py:
>
>  from pony.orm import *
>  db = Database()
>
>
>  Multiple modules define Entitys; they all say:
>
>  from pony.orm import *
>  import theDB
>  class IDcategory(theDB.db.Entity):
>   ...
>  class ID(theDB.db.Entity):
>   ...
>
>
>  One module establishes connections to a database:
>  geneticsDBMScommon.py:
>
>  from pony.orm import *
>  import theDB
>
>   from moduleDefineEntitys1 import *
>   from moduleDefineEntitys2 import *
> ...
>
>   Many modules (command line programs) that look like:
>
>  from geneticsDBMScommon import *
>  from moduleDefineEntitys1 import *
>  if __name__ == "__main__":
>
>  Connect( dbms, ... )
>
>
>  (I shouldn't say 'import *' so much.)
> Overall, calls need to happen in this order:
>
> from pony.orm import *
> db = Database()
>  class IDcategory(theDB.db.Entity): # using the singleton Database handle
>  ...
> theDB.db.bind( ...)
> theDB.db.generate_mapping( )
> # optionally drop tables
>  theDB.db.create_tables()
>  # use theDB.db
>
>
>  I cannot put
>
> theDB.db = Database()
>
> at the top of Connect() because we get "Mapping is not generated for
> entity" errors.
> But, to disconnect(), everything needs to begin again with db =
> Database(). So it seems that a single command line program must restart to
> change the database it is using.
>
>  I think that to fix this I need to get rid of theDB.py and
> have geneticsDBMScommon.py dynamically do:
>
> db = Database()
> from moduleDefineEntitys1 import *
>
>  ...
>
>  but I need to work on other things now ...
>
>  thanks again
>
>  A
>
>
>
>
>   On Dec 11, 2014, at 11:13 AM, Alexander Kozlovsky <
> alexander.kozlovsky at gmail.com> wrote:
>
>    Hi Arthur!
>
>  Now I get your problem.
>
>  The `disconnect` method does not reverse what `bind` and
> `generate_mapping` methods do. Once the Database object is binded to
> specific physical database, it cannot be unbinded. But you can create new
> database object instead of the previous one. Also, it is possible to have
> several database objects at once.
>
>  To solve your problem, just place the next line at the top of your
> `Connect` method:
>
>      theDB.db = Database()
>
>  This way you will create new db object each time you call `Create` method.
>
>  The `disconnect` method is just optimization which allows the program to
> drop connection to previous database server and to garbage-collect previous
> db object.
>
>  By the way, it is not necessary to have `theDB` class inside of your
> `theDB` module. In Python, each module is already a Singleton
> instance.Because of this, you can just place `db = Database()` right into
> your `theDB` module itself. Then your need to replace `from theDB import *`
> to `import theDB` in all of your modules, and all should work as before.
>
>
> Regards,
>  A.K.
>
> On Thu, Dec 11, 2014 at 5:54 PM, Goldberg, Arthur P <
> arthur.p.goldberg at mssm.edu> wrote:
>>
>>  Hi Alexander
>>
>>  Thanks! Yes, your simple program works.
>> As far as I know, I'm using 1 thread, and I'd like to switch between
>> different dbmses.
>> Basically, I'm using
>> bind()
>> generate_mapping()
>> disconnect()
>>  bind()
>>
>>  and getting
>>  TypeError: Database object was already bound to MySQL provider
>>  on the 2nd bind().
>>
>>  Here's a bit of my code.
>> test program t.py
>> the connection manager, geneticsDBMScommon.py
>> a shared singleton database handle, theDB.py
>> geneticsSubjects and geneticsClasses just define classes.
>>
>>  I can simplify the example if you like.
>>
>>  A
>>
>>
>>
>>   On Dec 10, 2014, at 6:06 PM, Alexander Kozlovsky <
>> alexander.kozlovsky at gmail.com> wrote:
>>
>>     Hi Arthur!
>>
>>  For what purpose do you want to use `disconnect` method? I'm not sure I
>> correctly understand what example you want to see, but the simplest example
>> of `disconnect` method is this:
>>
>>      from pony.orm import *
>>
>>     db = Database('mysql', host="myhost", user="me", passwd="123",
>> db="mydb")
>>
>>     class Person(db.Entity):
>>         name = Required(str)
>>
>>     db.generate_mapping(create_tables=True)
>>
>>     with db_session:
>>         p1 = Person(name='John')
>>
>>     db.disconnect()
>>
>>
>> The disconnect method works for current thread only. It releases
>> connection to the server which db object holds in its connection pool.
>>
>>  This method should be called outside of any db session.
>>
>>  If you application is multi-threaded and you want to disconnect your db
>> object from the database, the you need to call `disconnect` method from
>> each thread in which you have worked with the entities of this db object.
>>
>>
>>  Regards,
>>  Alexander Kozlovsky
>>
>>
>> On Wed, Dec 10, 2014 at 5:39 AM, Goldberg, Arthur P <
>> arthur.p.goldberg at mssm.edu
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__mailto-3Aarthur.p.goldberg-40mssm.edu&d=AAMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=MdhUTa5GEj8B5HsquznH3IF3hQvAX2z34nrPC35Fbqc&s=ct-V7Z7acRopVeskia8UT4P3-cU9cGjInrFDZss0TLk&e=>
>> > wrote:
>>
>> Hi Guys
>>
>>  Could you please show an example with disconnect()? I'm using a couple
>> of dbmses in one SQL server, and getting
>>  Database object was already bound to MySQL provider
>> Sorry, I'm too rushed to debug carefully.
>>
>>  Yhanks
>> A
>>
>>
>>         ---
>>
>> Arthur Goldberg
>> Associate Professor of Psychiatry
>> Seaver Autism Center and Icahn Institute for Genomics & Multiscale Biology
>> Icahn School of Medicine at Mount Sinai
>>  Seaver Center, Room ABE-33
>>  212-241-4229
>> Arthur.Goldberg at mssm.edu
>> <https://urldefense.proofpoint.com/v2/url?u=http-3A__mailto-3AArthur.Goldberg-40mssm.edu&d=AAMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=MdhUTa5GEj8B5HsquznH3IF3hQvAX2z34nrPC35Fbqc&s=zyJFM3R79vy0YD4o6VWXoHEs0OQXaiwdQ3nrXfBIBN8&e=>
>>  Follow us on Twitter @IcahnInstitute
>> <https://urldefense.proofpoint.com/v2/url?u=https-3A__twitter.com_IcahnInstitute&d=AAMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=MdhUTa5GEj8B5HsquznH3IF3hQvAX2z34nrPC35Fbqc&s=b8XJr7NW4WKs0NkKVXPX5Ua1nTWBMEFE6nwzM3mLer0&e=>
>>
>>
>>
>>
>>
>> _______________________________________________
>> ponyorm-list mailing list
>> ponyorm-list at ponyorm.com
>> /ponyorm-list
>> <https://urldefense.proofpoint.com/v2/url?u=https-3A__mailman-2Dmail5.webfaction.com_listinfo_ponyorm-2Dlist&d=AAMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=MdhUTa5GEj8B5HsquznH3IF3hQvAX2z34nrPC35Fbqc&s=EqprhF2xxCO00Y6lH7LjUW0WmnyegTuf-EW5kECRTA8&e=>
>>
>>
>>  _______________________________________________
>> ponyorm-list mailing list
>> ponyorm-list at ponyorm.com
>>
>> https://urldefense.proofpoint.com/v2/url?u=https-3A__mailman-2Dmail5.webfaction.com_listinfo_ponyorm-2Dlist&d=AAIGaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=MdhUTa5GEj8B5HsquznH3IF3hQvAX2z34nrPC35Fbqc&s=EqprhF2xxCO00Y6lH7LjUW0WmnyegTuf-EW5kECRTA8&e=
>>
>>
>>         ---
>>
>> Arthur Goldberg
>> Associate Professor of Psychiatry
>> Seaver Autism Center and Icahn Institute for Genomics & Multiscale Biology
>> Icahn School of Medicine at Mount Sinai
>>  Seaver Center, Room ABE-33
>>  212-241-4229
>> Arthur.Goldberg at mssm.edu
>>  Follow us on Twitter @IcahnInstitute
>> <https://urldefense.proofpoint.com/v2/url?u=https-3A__twitter.com_IcahnInstitute&d=AwMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=YwVTDkQQLeA-rvnWaYkkQ9VzBXh33ZAhgFEk2hOENxI&s=cfiVP61ha8Ag45F8nOyDMtkYYqDZ-Qx_lrVppLFYpCk&e=>
>>
>>
>>
>>
>>
>> _______________________________________________
>> ponyorm-list mailing list
>> ponyorm-list at ponyorm.com
>> /ponyorm-list
>> <https://urldefense.proofpoint.com/v2/url?u=https-3A__mailman-2Dmail5.webfaction.com_listinfo_ponyorm-2Dlist&d=AwMFaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=YwVTDkQQLeA-rvnWaYkkQ9VzBXh33ZAhgFEk2hOENxI&s=IiaXyoraaZ63il62cuaacgi8celdD2x5HB31vFkvHao&e=>
>>
>>    _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
>
> https://urldefense.proofpoint.com/v2/url?u=https-3A__mailman-2Dmail5.webfaction.com_listinfo_ponyorm-2Dlist&d=AwIGaQ&c=4R1YgkJNMyVWjMjneTwN5tJRn8m8VqTSNCjYLg1wNX4&r=TntoeYH7lekXzpBRjXwLTkqiWTbWAvp3pHKo_kZp5qI&m=YwVTDkQQLeA-rvnWaYkkQ9VzBXh33ZAhgFEk2hOENxI&s=IiaXyoraaZ63il62cuaacgi8celdD2x5HB31vFkvHao&e=
>
>
>            ---
>
> Arthur Goldberg
> Associate Professor of Psychiatry
> Seaver Autism Center and Icahn Institute for Genomics & Multiscale Biology
> Icahn School of Medicine at Mount Sinai
>  Seaver Center, Room ABE-33
>  212-241-4229
> Arthur.Goldberg at mssm.edu
>  Follow us on Twitter @IcahnInstitute <https://twitter.com/IcahnInstitute>
>
>
>
>
>
> _______________________________________________
> ponyorm-list mailing list
> ponyorm-list at ponyorm.com
> /ponyorm-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </ponyorm-list/attachments/20141211/04204b4d/attachment-0001.html>


More information about the ponyorm-list mailing list