use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method queryOne.
@Override
public <R> R queryOne(String sql, Object[] args, RowMapper<R> mapper) {
try {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}, args : {}", sql, args);
byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_WITH_MAPPER_WITH_ARGS).sql(sql).args(args).className(mapper.getClass().getCanonicalName()).build());
final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
if (response.getSuccess()) {
return serializer.deserialize(response.getData().toByteArray(), ClassUtils.resolveGenericTypeByInterface(mapper.getClass()));
}
throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
} catch (Exception e) {
LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
}
}
use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method update.
@Override
public Boolean update(List<ModifyRequest> sqlContext, BiConsumer<Boolean, Throwable> consumer) {
try {
// Since the SQL parameter is Object[], in order to ensure that the types of
// array elements are not lost, the serialization here is done using the java-specific
// serialization framework, rather than continuing with the protobuff
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "modifyRequests info : {}", sqlContext);
// {timestamp}-{group}-{ip:port}-{signature}
final String key = System.currentTimeMillis() + "-" + group() + "-" + memberManager.getSelf().getAddress() + "-" + MD5Utils.md5Hex(sqlContext.toString(), Constants.ENCODE);
WriteRequest request = WriteRequest.newBuilder().setGroup(group()).setKey(key).setData(ByteString.copyFrom(serializer.serialize(sqlContext))).putAllExtendInfo(EmbeddedStorageContextUtils.getCurrentExtendInfo()).setType(sqlContext.getClass().getCanonicalName()).build();
if (Objects.isNull(consumer)) {
Response response = this.protocol.write(request);
if (response.getSuccess()) {
return true;
}
LogUtil.DEFAULT_LOG.error("execute sql modify operation failed : {}", response.getErrMsg());
return false;
} else {
this.protocol.writeAsync(request).whenComplete((BiConsumer<Response, Throwable>) (response, ex) -> {
String errMsg = Objects.isNull(ex) ? response.getErrMsg() : ExceptionUtil.getCause(ex).getMessage();
consumer.accept(response.getSuccess(), StringUtils.isBlank(errMsg) ? null : new NJdbcException(errMsg));
});
}
return true;
} catch (TimeoutException e) {
LogUtil.FATAL_LOG.error("An timeout exception occurred during the update operation");
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
} catch (Throwable e) {
LogUtil.FATAL_LOG.error("An exception occurred during the update operation : {}", e);
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
}
}
use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method queryMany.
@Override
public <R> List<R> queryMany(String sql, Object[] args, RowMapper<R> mapper) {
try {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryMany info : sql : {}, args : {}", sql, args);
byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_MANY_WITH_MAPPER_WITH_ARGS).sql(sql).args(args).className(mapper.getClass().getCanonicalName()).build());
final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
if (response.getSuccess()) {
return serializer.deserialize(response.getData().toByteArray(), List.class);
}
throw new NJdbcException(response.getErrMsg());
} catch (Exception e) {
LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
}
}
use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method queryOne.
@Override
public <R> R queryOne(String sql, Class<R> cls) {
try {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}", sql);
byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_NO_MAPPER_NO_ARGS).sql(sql).className(cls.getCanonicalName()).build());
final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
if (response.getSuccess()) {
return serializer.deserialize(response.getData().toByteArray(), cls);
}
throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
} catch (Exception e) {
LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
}
}
use of com.alibaba.nacos.config.server.exception.NJdbcException in project nacos by alibaba.
the class DistributedDatabaseOperateImpl method queryOne.
@Override
public <R> R queryOne(String sql, Object[] args, Class<R> cls) {
try {
LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}, args : {}", sql, args);
byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_NO_MAPPER_WITH_ARGS).sql(sql).args(args).className(cls.getCanonicalName()).build());
final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
if (response.getSuccess()) {
return serializer.deserialize(response.getData().toByteArray(), cls);
}
throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
} catch (Exception e) {
LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
}
}
Aggregations