jaibatrik
jaibatrik

Reputation: 7533

How should I handle deletion of objects in Object Relational Mapping

I have had this confusion about how I handle the different lifecycle events of an Object where they are persisted in a database. I know this can vary with design, but I would like to know any standard / best practice which are followed in such cases.

Say I have a User class like this -

class User {
  int id;
  String name;
}

An object of this type represents a row in the database. The objects are accessed by multiple threads. So, how do you manage the object? What are its methods? How do you implement deletion of such an object?

Say there are two threads, A and B which are accessing the same user object.

// Thread A
User user = UserFactory.getUser(userId);

// Thread B
User user = UserFactory.getUser(userId)
user.delete(); // Or UserFactory.delete(userId)

Then what happens to the user object in thread A? Any clarification would be very helpful.

Thanks!

Upvotes: 0

Views: 235

Answers (2)

Adam Gent
Adam Gent

Reputation: 49085

Then what happens to the user object in thread A? Any clarification would be very helpful.

Nothing. Of course that is probably your problem. What will happen if you try to persist in thread A after deletion is probably highly dependent on the ORM you are using but my guess assuming your using Hibernate is that it will fail trying to do an UPDATE/DELETE as the open Session in thread A does not know that the row is missing. This happens frequently.

Notice that thread A will always be able to mutate the user object freely with out error so long as it does not persist/delete. The error will only happen when you go persist/delete. In this case whoever is first persist/delete wins (no error).

People mitigate this problem in a variety of ways:

  1. Silently swallow the exception and either reinsert the object or ignore
  2. Promote the exception (usually an Optimistic Locking Exception) and convert to a user friendly error.
  3. Never allow deletes in the first place. Use a boolean column to indicate delete.
  4. Make your application implement Optimistic Locking in sync with your ORM's optimistic locking.
  5. Use transactions and/or synchronized (single JVM)
  6. Use transactions and row locking SELECT ... FOR UPDATE.

In most situations I go with number 2 and/or 3. Number 5 is the most pessimistic, requires lots of resources with the potential of dead locking.

Upvotes: 1

user2433662
user2433662

Reputation:

from what I understand, as far as threads accessing an object, it is a free for all which means both of them may have access to the same object unless synchronization/locking is being used.

From what I am able to gather from your pseudo code, your deletion of the object would completely remove it from the system (including any threads that are accessing it), but I believe it depends on the way you delete it. If you delete an object within the thread itself, it should only delete it from that thread, but if you delete the object outside of the threads it should delete it from the entire system.

From what I see of your code, I am having trouble determining if you are trying to delete the object within Thread B or delete it in the rest of the code, but whichever you are trying to accomplish, I hope my above explanation helps.

Upvotes: 1

Related Questions