[Zope3-Users] Re: Request in Zope3-Thread

Philipp von Weitershausen philipp at weitershausen.de
Mon Aug 20 13:30:33 EDT 2007


On 20 Aug 2007, at 19:29 , Markus Leist wrote:
> maybe I'm wrong, but after several tests i have noticed this change
> (caused by threading environment):
>
> ...
> setSite(old_site)
> transaction.get().commit()
> conn.close()
> ...
>
> with transaction.commit() there are some:
>> ZODB.POSException.ConnectionStateError: Cannot close a connection  
>> joined to a transaction
> under special circumstances (more threads, some commits)
>
> Does this transaction.get()... is the right way to interact with the
> Thread-aware transaction manager?

transaction.get().commit() should be completely equivalent to  
transaction.commit(). The setSite(old_site) call doesn't look  
necessary since the reference to the site is local to the thread.


> Am Samstag 04 August 2007 um 11:13 Uhr schrieb Philipp von  
> Weitershausen <philipp at weitershausen.de>:
>> Markus Leist wrote:
>>> i have created a thread in my zope3-application like the  
>>> zope.sendmail.QueueProcessorThread.
>>>
>>> What i want to do: "fake" a request-context to get a local Utility
>>> (via Sitemanager) configured in ZODB and call some methods which  
>>> will
>>> change some object-attributes in ZODB.
>>
>> You'll have to open a new connection to the database in the thread  
>> and
>> get the root object from it (you'll have to give the thread a  
>> reference
>> to the database, which can be accessed from any persistent object  
>> thru
>> obj._p_jar, IIRC):
>>
>>    conn = db.open()
>>    root = conn.root()
>>    root_folder = root['Application']
>>
>> Now you can traverse to the site that contains the local utilities
>> (unless that's the root_folder) and make the current active site
>> (current = for this particular thread):
>>
>>    from zope.app.component.hooks import setSite
>>    site = root_folder['path']['to']['the']['site']
>>    setSite(site)
>>
>> Now do your component lookups, object modifications, etc. Don't  
>> forget
>> to commit the transaction at the end:
>>
>>    import transaction
>>    transaction.commit()
>>
>> and close the connection:
>>
>>    conn.close()
>>
>>
>



More information about the Zope3-users mailing list