Gemstone/S – Indices and Traps ….

Ha, that’s a line of code which took me some time to understand, why it fails …

  setAttribute: (SomeObject new initialize createIndices ; yourself).
System inTransaction
  ifFalse:[  System beginTransaction ].

Here an attribute of a persistent object is set. The newly instanced object builds some indices.

Now, one may think, that in “attribute” this newsly created object should be available … but actually NOTHING has changed and the object (prior stored in attribute) is still there.

Ok, the systemwide index manager is per default running in “manual” mode – so if you delete or create indices you will be out of a transaction … even if you run in transaction auto mode.

So, in this case one always has to check the transaction state and if needed start a new transaction.

The timing of the code above is wrong. The statement in () creates the new indices, but returns being out of a transaction.

The setting of attribute happens outside of a transaction and the control statement after the setting creates a new transaction (and beginTransaction includes a prior abortTransaction).

The abortTransaction restores the old content of attribute and so your code does some work, but not the work you thought it would do.

Here is the code to do it correctly

| anInstance |
  := SomeObject new initialize createIndices ; yourself.
System inTransaction
  ifFalse:[ System beginTransaction ].
aPersistentObject setAttribute: anInstance.
This entry was posted in Smalltalk and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.