Reputation: 23
In pharo seaside application on home button click all the data will get saved in a fuel file with some class name as Test.fl. If I call the same home page at a time in two instances, both are calling this below code to save Test.fl file with latest updates,
FLSerializer
serialize: self allObjects
toFileNamed: self name , '.fl'**
and getting exception:
CannotDeleteFileException: Could not delete the old version of file ...\Pharo3.0\Test.fl
And I have used Mutex to allow another instance to get executed after the first instance,
mutexBlock := Mutex new.
mutexBlock critical: [
FLSerializer
serialize: self allObjects
toFileNamed: self name , '.fl' ]
But still i am getting the same error
CannotDeleteFileException: Could not delete the old version of file
Please anybody help me on this to fix the error.
Upvotes: 2
Views: 121
Reputation: 15907
You do not need a mutex there. Seaside in Pharo processes requests one at a time. That is, this only works if your file writing is fast, as all other connections wait for the write to finish. If it is not, you'll need to do the writing in a separate thread and then indeed control access to it
Upvotes: 0
Reputation: 5125
Your intuition is probably correct: two processes are trying to write (delete) the same file at the same time. Your Mutex
approach will not work however because you create a new Mutex
every time you execute that code. You have to store the Mutex
in a class or instance variable (depending on how your code works) such that every process will use the same instance of the Mutex
.
Your code would then look something like this:
MyClass class>>serializeProtect
^ SerializeProtect ifNil: [ SerializeProtect := Mutex new ]
self class serializeProtect critical: [
FLSerializer
serialize: self allObjects
toFileNamed: self name , '.fl' ]
Note: it is generally not safe to initialise a Mutex
lazily. I'm simply doing it here for simplicities sake.
Upvotes: 5