You are correct: the code-snippet will cause an exception to be thrown
when SA attempts to flush any changes. However, the connection
callable is called *per-instance* and it is supposed to return the
connection to use to perform the flush. Inside the callable, you can
peek at the mapper and/or the instance and return whatever you deem
appropriate. What kind of failure mode are you looking for? If you
have a session with forbidden writes, what should happen? Nothing gets
written? The legitimate updates occur but the disallowed ones generate
exceptions? disallowed writes are silently swallowed by a mock
connection (probably make programmers very upset, confused and angry!)

The problem with any of these approches is -- as you point out -- that
the application is not informed of the boo-boo at the time it occurs.
I wonder what Michael is alluding to in his comment about implementing
__getattribute__? I assume he meant __setattr__?

pjjH


On Aug 25, 9:26 am, Martijn Faassen <faas...@startifact.com> wrote:
> Hey,
>
>
>
> phrrn...@googlemail.com wrote:
> > I implemented a very crude flush/commit-time version of this today
> > that disables all modifications. Michael suggested the use of
> > connection_callable to provide fine-grained control of which engine to
> > use for modifications. I haven't gone through all the details yet but
> > it seems to work for my basic tests.
>
> > pjjH
>
> > class ReadOnlySession(DefaultSession):
> >     def __init__(self, **kwargs):
> >         super(ReadOnlySession, self).__init__(**kwargs)
> >         self._mapper_flush_opts = {'connection_callable',
> > self._disable_any_modifications}
>
> >     def _disable_any_modifications(self, mapper=None, instance=None,
> > **kwargs):
> >         return None
>
> If I understand it correctly, this is on a per-session basis, correct?
> I'd like to block modification to some but not all objects in the same
> session.
>
> Regards,
>
> Martijn
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to