Search in sources :

Example 16 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class Query method executeDelete.

public boolean executeDelete() {
    boolean allOk = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.getAny(i);
        String table = keyInfo.getTable();
        if (!keyInfo.getIsNew() && !keyInfo.hasParams() && keyInfo.ifJustCreated()) {
            waitForParamsUpdate(pair.getId(), keyInfo);
        }
        if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
            String msg = "executeDelete failed when calling prepareStandardClauseParams for " + pair.getId();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
            continue;
        }
        params = keyInfo.getParams();
        String clause = keyInfo.getClause();
        sql = "delete from " + table + " where " + clause + " limit 1";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.delete");
        try {
            if (jdbcTemplate.update(sql, params.toArray()) > 0) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                LOGGER.trace("delete " + pair.getId() + " from " + table);
                continue;
            } else {
                if (stopWatch != null)
                    stopWatch.stopNow();
                allOk = false;
            }
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        keyInfo.setQueryKey(null);
    }
    return allOk;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch)

Example 17 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class Query method executeSelect.

public boolean executeSelect() {
    KeyInfo keyInfo = anyKey.getKeyInfo();
    String table = keyInfo.getTable();
    LOGGER.trace("sql: " + sql);
    LOGGER.trace("params: " + (params != null ? params.toString() : "null"));
    List<Map<String, Object>> list = null;
    StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.queryForList");
    try {
        if (params != null) {
            list = jdbcTemplate.queryForList(sql, params.toArray());
        } else {
            list = jdbcTemplate.queryForList(sql);
        }
        if (stopWatch != null)
            stopWatch.stopNow();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                KvPair pair = pairs.getAny(i);
                keyInfo = anyKey.getAny(i);
                pair.setData(list.get(i));
                // 
                if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
                    String msg = "executeSelect failed when prepareStandardClauseParams for " + pair.getId();
                    LOGGER.error(msg);
                    context.logTraceMessage(msg);
                    if (context.isSync()) {
                        throw new ServerErrorException(context, msg);
                    }
                }
                LOGGER.trace("found " + pair.getId() + " from " + table);
            }
            return true;
        }
    } catch (Exception e) {
        if (stopWatch != null)
            stopWatch.stopNow();
        e.printStackTrace();
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        context.logTraceMessage(msg);
        if (context.isSync()) {
            throw new ServerErrorException(context, msg);
        }
    }
    return false;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) KvPair(com.rdbcache.models.KvPair) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch)

Example 18 with ServerErrorException

use of com.rdbcache.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class Request method process.

public static AnyKey process(Context context, HttpServletRequest request, KvPairs pairs, Optional<String> opt1, Optional<String> opt2) {
    LOGGER.info("URI: " + request.getRequestURI());
    if (PropCfg.getEnableMonitor())
        context.enableMonitor(request);
    AnyKey anyKey = new AnyKey();
    if (pairs == null) {
        return anyKey;
    }
    // find the keyinfo for the first key
    if (pairs.size() > 0) {
        AppCtx.getKeyInfoRepo().find(context, new KvPairs(pairs.getPair()), anyKey);
    }
    KeyInfo keyInfo = anyKey.getAny();
    Map<String, String[]> params = request.getParameterMap();
    if ((params != null && params.size() > 0) || opt1 != null || opt2 != null) {
        processOptions(context, keyInfo, params, opt1, opt2);
    }
    if (pairs.size() == 0 || context.getAction().startsWith("select_")) {
        return anyKey;
    }
    // find keyinfo for the second key and after
    if (pairs.size() > 1) {
        AppCtx.getKeyInfoRepo().find(context, pairs, anyKey);
    }
    for (int i = 0; i < pairs.size() && i < anyKey.size(); i++) {
        keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            keyInfo.setIsNew(false);
            String key = pairs.get(i).getId();
            AppCtx.getLocalCache().putKeyInfo(key, keyInfo);
            keyInfo.setIsNew(true);
        }
    }
    if (anyKey.size() != 1 && pairs.size() != anyKey.size()) {
        throw new ServerErrorException(context, "case not supported, anyKey size(" + anyKey.size() + ") != 1 && pairs size(" + pairs.size() + ") != anyKey size(" + anyKey.size() + ")");
    }
    return anyKey;
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Aggregations

ServerErrorException (com.rdbcache.exceptions.ServerErrorException)18 KeyInfo (com.rdbcache.models.KeyInfo)9 KvPair (com.rdbcache.models.KvPair)8 StopWatch (com.rdbcache.models.StopWatch)8 SQLException (java.sql.SQLException)4 AnyKey (com.rdbcache.helpers.AnyKey)1 KvPairs (com.rdbcache.helpers.KvPairs)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EventListener (org.springframework.context.event.EventListener)1 PreparedStatementCreator (org.springframework.jdbc.core.PreparedStatementCreator)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1