Just Launched: You can now import projects and releases from Google Code onto SourceForge
We are excited to release new functionality to enable a 1-click import from Google Code onto the Allura platform on SourceForge. You can import tickets, wikis, source, releases, and more with a few simple steps. Read More
Hello, I've got some problems using the FileSessionStore or the Dynamic
Store when there are any MiddleObjects stored in the session. I'm using
MiddleKit and PostgreSQL. The SessionStore isn't able to store a
MiddleObject with a reference to _mk_store (with database connection). I
hope, I solved the problem by adding __getstate__ and __setstate__ to my
odict = self.__dict__.copy()
self.__dict__ = dict
self.__dict__['_mk_store'] = MiddleKit.Run.ObjectStore.Store
It works. But is it save?
At 02:35 PM 4/24/2001 +0200, J=F6rn Schrader wrote:
>Hello, I've got some problems using the FileSessionStore or the Dynamic=20
>Store when there are any MiddleObjects stored in the session. I'm using=20
>MiddleKit and PostgreSQL. The SessionStore isn't able to store a=20
>MiddleObject with a reference to _mk_store (with database connection). I=20
>hope, I solved the problem by adding __getstate__ and __setstate__ to my=20
> odict =3D self.__dict__.copy()
> del odict['_mk_store']
> return odict
> self.__dict__ =3D dict
> self.__dict__['_mk_store'] =3D MiddleKit.Run.ObjectStore.Store
>It works. But is it save?
The first thing that bothers me is that there could be changes to the=20
object in the store's memory and even the database, that would not be=20
reflected in the pickled session. For that reason, I would say "No, storing=
MiddleObjects in a pickled file is not 'safe', or at least not a good idea."
But I think there is still an approach that would give you what you want.=20
The essence is to pickle a reference to the middle object.
* The "hard work approach" would be to store the class name and serial=20
number of the object in the session. You can then use store.fetchObject().=
You might even be able to use the object's key (which is just the combo of=
class name and serial num), although I haven't tested this yet:
sess =3D self.session()
sess['cartKey'] =3D cart.key() # store the object
cart =3D store.object(sess['cartKey']) # retrieve the object
The problem is that using session in that way wouldn't be as natural.
* Augment the Session by providing a __getstate__ and __savestate_ that=20
does what I just described. You could code it for particular keys (perhaps=
taken from an attribute), or better yet, albeit more expensive, check for=20
instances of MiddleObject.
I strongly suggest switching techniques and I think the above approach will=
work out. Please let us know what you end up doing.