RethinkDb Java连接超时

时间:2018-11-30 08:36:03

标签: java database-connection rethinkdb

我有大约20个Java线程,每5秒在同一db上打开一个连接,并在同一表上执行读写操作。每小时我都会收到大约4/5错误:com.rethinkdb.gen.exc.ReqlDriverError:连接超时。

public void myFunction(Object asset) {

    Connection connection = null;

    try {
        connection = r.connection().hostname(rethinkUrl).port(rethinkPort).timeout(rethinkTimeout)
                .user(rethinkUser, rethinkPassword).connect();

        // some operations 

        T assetOld = (T) gson.fromJson(r.db(getT().getDb()).table(getT().getTable()).get(asset.getId()).toJson()
                .run(connection).toString(), getT().getClass());

        /*
         * RethinkDB write operations will only throw exceptions if errors occur before
         * any writes. Other errors will be listed in first_error, and errors will be
         * set to a non-zero count. To properly handle errors with this term, code must
         * both handle exceptions and check the errors return value!
         */

        String jsonRes = r.db(getT().getDb()).table(getT().getTable())
                .insert(r.hashMap(QueryUtils.ID_FIELD, asset.getId())
                        .with(QueryUtils.LAST_TIMESTAMP_FIELD, asset.getLastTimestamp())
                        .with(QueryUtils.MANUFACTURER_FIELD, getT().getManufacturer())
                        .with(QueryUtils.MODEL_FIELD, getT().getModelName())
                        .with(QueryUtils.TC_FIELD, asset.getTC()).with(QueryUtils.SIGNALS_FIELD,
                                assetOld != null ? union(assetOld.getSignals(), asset.getSignals())
                                        : asset.getSignals()))
                .optArg("conflict", "update").toJson().run(connection);

        QueryResult queryRes = gson.fromJson(jsonRes, QueryResult.class);

        if (queryRes.getErrors() > 0) {
            String firstError = queryRes.getFirst_error();
            throw new RethinkException(firstError);
        }

    } catch (Exception e) {
        logger.error(e.getMessage(), e);

    } finally {
        if (connection != null) {
            connection.close();
        }
    }

}

rethinkdb是否允许最大数量的约定?您有什么建议的最佳做法吗?

0 个答案:

没有答案