Hazelcast Spring (boot) JPA - saving a new entity

2019-09-15 07:26发布

问题:

I have an existing client/server, Spring boot project that uses JPA. I followed the spring-data-jpa-hazelcast-migration sample to create a Hazelcast client & server. There is existing data in a db that populates the Hazelcast Map. Each of these db entries has an Integer id which becomes the Map key. During this loadAllKeys() on the server, I populate a Set which is distributed/managed by Hazelcast; in the MemCenter UI, I see the populated Map & the Set containing the expected number of Integer keys.

Per the example mentioned above, the client application has a service which uses a HazelcastRepository to save() new entities. Since a new entity does not have an id, on the client side (where a new entity is created), I have an IdGenerator that returns nextKey(). The (distributed) Set is asked if it contains said id/key to prevent key collisions.

Then, once this save() on the client side makes its way to the server, I attempt to add the new key to the Set to avoid any collisions in the future. In doing so, I get the following exception:

Thread[hz._hzInstance_1_dev.partition-operation.thread-3,5,_hzInstance_1_dev] cannot make remote call: com.hazelcast.collection.impl.collection.operations.CollectionAddOperation{serviceName='hz:impl:setService', identityHash=1523576050, partitionId=64, replicaIndex=0, callId=0, invocationTime=-1 (1969-12-31 15:59:59.999), waitTimeout=-1, callTimeout=60000, name=com.intelligrated.hazelcast.server.maploader.ScannersMapStore_Set}

For some reason, I have not found a single example of how to handle the creation of new data especially when data already exists. The examples/test are trivial and when a new object is saved, the id is hard-coded (usually to '1').

I devised the above solution hoping that I can make this work. If there is a better way, kindly point me to an useful example.

回答1:

So, I found this post helpful.

Basically, you can't have "operations" in MapStore b/c of the write through.:

"Because writethrough map store operations run on partition thread, and using another partition based operation(like Containskey) can cause deadlock. That is why we have a check and an exception there"

So, removed all the Set business from my MapStore & added necessary logic to a separate "Listener" class. The map store sends "set update events" on a Topic to which said Listener is subscribed to.



标签: hazelcast