[PonyORM-list] Example with disconnect()

Alexander Kozlovsky alexander.kozlovsky at gmail.com
Thu Dec 11 16:13:43 UTC 2014


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://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/da9d2124/attachment-0001.html>


More information about the ponyorm-list mailing list