客户端崩溃时如何防止事务死锁?

时间:2016-06-30 09:21:53

标签: hazelcast

我服务器中的代码:

            TransactionContext txCxt = hz.newTransactionContext(txop);
            txCxt.beginTransaction();
            TransactionalMap<Object, Object> map = txCxt.getMap(NearCacheTest.MAP);

            try {
                String tmp = String.valueOf(System.currentTimeMillis());
                map.put("1", tmp); // reader1 is blocked
                tmp = String.valueOf(System.currentTimeMillis());
                map.put("2", tmp); // reader2 is blocked

                // evict all client map cache
                IList<Object> evictList = hz.getList(NearCacheTest.SEMAPHORE_EVICT);
                evictList.add("1");
                evictList.add("2");

                int size = hz.getClientService().getConnectedClients().size();
                hz.getSemaphore(NearCacheTest.SEMAPHORE_EVICT).release(size);
                hz.getAtomicLong(NearCacheTest.SEMAPHORE_EVICT).set(size);
                logger.info("client count:{}", size);

                // wait to response from clients
                logger.info("waiting for event:{}", NearCacheTest.SEMAPHORE_ACK);
                hz.getSemaphore(NearCacheTest.SEMAPHORE_ACK).acquire();

我的问题是: 当其中一个客户端崩溃时,semaphore_ack永远不会被释放;

我该怎样做才能防止死锁?

1 个答案:

答案 0 :(得分:0)

                    // evict all client map cache
                IList<Object> evictList = hz.getList(NearCacheTest.SEMAPHORE_EVICT);
                evictList.add("1");
                evictList.add("2");

                int size = hz.getClientService().getConnectedClients().size();
                ISemaphore semaphore = hz.getSemaphore(NearCacheTest.SEMAPHORE_EVICT);
                semaphore.release(size);
                logger.info("client count:{}", size);

                // wait to response from clients
                while (semaphore.tryAcquire()) {
                }