public boolean acquireLock(MongoDatabase db) { Document insertObj = new Document(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL).append("status", "LOCK_HELD"); // acquire lock by attempting to insert the same value in the collection - if it already exists (i.e. lock held) // there will be an exception try { db.getCollection(lockCollectionName).insertOne(insertObj); } catch (MongoWriteException ex) { if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) { logger.warn("Duplicate key exception while acquireLock. Probably the lock has been already acquired."); } return false; } return true; }
public boolean acquireLock(MongoDatabase db) { Document insertObj = new Document(KEY_PROP_NAME, LOCK_ENTRY_KEY_VAL).append("status", "LOCK_HELD"); // acquire lock by attempting to insert the same value in the collection - if it already exists (i.e. lock held) // there will be an exception try { db.getCollection(lockCollectionName).insertOne(insertObj); } catch (MongoWriteException ex) { if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) { logger.warn("Duplicate key exception while acquireLock. Probably the lock has been already acquired."); } return false; } return true; }
@ManagedOperation(description = "Adds the key to the store") @Override public boolean add(E key) { Document document = new Document("_id", key); try { collection.insertOne(document); } catch (com.mongodb.MongoWriteException ex) { if (ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY) { return false; } throw ex; } return true; }
/** * Common handler for creating new objects. Assures that errors are handled in a * consistent way. * * @param collection * @param object * @throws SiteWhereException */ public static void insert(MongoCollection<Document> collection, Document object, ErrorCode ifDuplicate) throws SiteWhereException { try { long start = System.currentTimeMillis(); collection.insertOne(object); LOGGER.trace("Insert took " + (System.currentTimeMillis() - start) + " ms."); } catch (MongoWriteException e) { ErrorCategory category = e.getError().getCategory(); if (ErrorCategory.DUPLICATE_KEY == category) { throw new ResourceExistsException(ifDuplicate); } throw new SiteWhereException("Error during MongoDB insert.", e); } catch (MongoClientException e) { throw handleClientException(e); } }
private void insertUpdate(LockEntry newLock, boolean onlyIfSameOwner) { boolean lockHeld; try { final Bson acquireLockQuery = getAcquireLockQuery(newLock.getKey(), newLock.getOwner(), onlyIfSameOwner); final UpdateResult result = collection.updateMany( acquireLockQuery, new Document().append("$set", newLock.buildFullDBObject()), new UpdateOptions().upsert(!onlyIfSameOwner)); lockHeld = result.getModifiedCount() <= 0 && result.getUpsertedId() == null; } catch (MongoWriteException ex) { lockHeld = ex.getError().getCategory() == ErrorCategory.DUPLICATE_KEY; if (!lockHeld) { throw ex; } } catch (DuplicateKeyException ex) { lockHeld = true; } if (lockHeld) { throw new LockPersistenceException("Lock is held"); } }