public void insertData(String schemaName, String tableName, DBObject naive, DBObject complete) { int i = 0; DBObject logObj = (DBObject) ObjectUtils.clone(complete); //保存原始数据 try { String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.INSERT.getNumber(); i++; naiveMongoTemplate.getCollection(tableName).insert(naive); i++; SpringUtil.doEvent(path, complete); i++; } catch (MongoClientException | MongoSocketException clientException) { //客户端连接异常抛出,阻塞同步,防止mongodb宕机 throw clientException; } catch (Exception e) { logError(schemaName, tableName, 1, i, logObj, e); } }
public void updateData(String schemaName, String tableName, DBObject query, DBObject obj) { String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.UPDATE.getNumber(); int i = 0; DBObject newObj = (DBObject) ObjectUtils.clone(obj); DBObject logObj = (DBObject) ObjectUtils.clone(obj); //保存原始数据 try { obj.removeField("id"); i++; naiveMongoTemplate.getCollection(tableName).update(query, obj); i++; SpringUtil.doEvent(path, newObj); i++; } catch (MongoClientException | MongoSocketException clientException) { //客户端连接异常抛出,阻塞同步,防止mongodb宕机 throw clientException; } catch (Exception e) { logError(schemaName, tableName, 2, i, logObj, e); } }
public void deleteData(String schemaName, String tableName, DBObject obj) { int i = 0; String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.DELETE.getNumber(); DBObject newObj = (DBObject) ObjectUtils.clone(obj); DBObject logObj = (DBObject) ObjectUtils.clone(obj); //保存原始数据 try { i++; if (obj.containsField("id")) { naiveMongoTemplate.getCollection(tableName).remove(new BasicDBObject("_id", obj.get("id"))); } i++; SpringUtil.doEvent(path, newObj); } catch (MongoClientException | MongoSocketException clientException) { //客户端连接异常抛出,阻塞同步,防止mongodb宕机 throw clientException; } catch (Exception e) { logError(schemaName, tableName, 3, i, logObj, e); } }
private Error analyzeException(Exception e, final String otherwise, final String msg, boolean specialHandling) { if (e instanceof Error) { return (Error) e; } if (e instanceof MongoException) { MongoException me = (MongoException) e; if (me.getCode() == 18) { return Error.get(CrudConstants.ERR_AUTH_FAILED, e.getMessage()); } else if (me instanceof MongoTimeoutException || me instanceof MongoExecutionTimeoutException) { LOGGER.error(CrudConstants.ERR_DATASOURCE_TIMEOUT, e); return Error.get(CrudConstants.ERR_DATASOURCE_TIMEOUT, e.getMessage()); } else if (me instanceof DuplicateKeyException) { return Error.get(MongoCrudConstants.ERR_DUPLICATE, e.getMessage()); } else if (me instanceof MongoSocketException) { LOGGER.error(MongoCrudConstants.ERR_CONNECTION_ERROR, e); return Error.get(MongoCrudConstants.ERR_CONNECTION_ERROR, e.getMessage()); } else { LOGGER.error(MongoCrudConstants.ERR_MONGO_ERROR, e); return Error.get(MongoCrudConstants.ERR_MONGO_ERROR, e.getMessage()); } } else if (msg == null) { return Error.get(otherwise, e.getMessage()); } else if (specialHandling) { return Error.get(otherwise, msg, e); } else { return Error.get(otherwise, msg); } }