user2793390
user2793390

Reputation: 771

Using a DB sequence as the cache key

I have a write-through caching with Oracle Coherence and DB. One of the pojos I'm caching, uses a DB sequence as its ID. I'm using the id as a cache key.

How do I insert a new cache object when I don't have the key (ID) until a record is inserted in the DB?

What is the best way to handle this? Should I even be using sequences with a write-through cache?

Upvotes: 1

Views: 166

Answers (1)

cpurdy
cpurdy

Reputation: 1236

That is indeed a tricky one. Coherence uses keys to uniquely identify each entry, and using write-through or write-behind caching, Coherence will store an entry into the underlying data store (via the CacheStore interface). Obviously, to put the entry into the cache, it has to have a key, and only subsequently will the entry be stored to (inserted into) the database, at which point you are assigning the key. It's definitely a chicken-and-egg problem.

  1. If you're using something like an Oracle SEQUENCE object, then you can query that before putting the entry into the cache, and use that value as the key.

  2. If you're using a trigger or other "assign on insert" mechanism, then I'm not sure how to solve this for either write-through or write-behind. You may need to first insert the data into the database, then write it into the cache, which does seem both complicated and redundant.

Upvotes: 2

Related Questions