Search in sources :

Example 11 with ServerErrorException

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

the class RedisRepoImpl method save.

@Override
public boolean save(Context context, KvPairs pairs, AnyKey anyKey) {
    LOGGER.trace("save pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    boolean savedAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + key;
        Map<String, Object> map = pair.getData();
        if (enableDataCache) {
            AppCtx.getLocalCache().putData(pair, keyInfo);
        }
        StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
        try {
            hashOps.putAll(hashKey, map);
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.debug("save to redis for " + key);
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            if (enableDataCache) {
                AppCtx.getLocalCache().removeData(key);
            }
            savedAll = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    }
    LOGGER.trace("save returns " + savedAll);
    return savedAll;
}
Also used : KvPair(com.rdbcache.models.KvPair) KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) StopWatch(com.rdbcache.models.StopWatch)

Example 12 with ServerErrorException

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

the class DbaseOps method handleApplicationReadyEvent.

@EventListener
public void handleApplicationReadyEvent(ApplicationReadyEvent event) {
    try {
        String driverName = AppCtx.getJdbcDataSource().getConnection().getMetaData().getDriverName();
        if (driverName.indexOf("MySQL") >= 0) {
            databaseType = "mysql";
        } else if (driverName.indexOf("H2") >= 0) {
            databaseType = "h2";
        } else {
            throw new ServerErrorException("database drive not support");
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new ServerErrorException(e.getCause().getMessage());
    }
    setDefaultToDbTimeZone();
    cacheAllTablesInfo();
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) EventListener(org.springframework.context.event.EventListener)

Example 13 with ServerErrorException

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

the class LocalCache method put.

public Map<String, Object> put(String key, Long timeToLive, Callable<Map<String, Object>> refreshable) {
    try {
        Map<String, Object> map = refreshable.call();
        if (map == null) {
            return null;
        }
        Cached cached = new Cached(map, timeToLive);
        cached.refreshable = refreshable;
        cache.put(key, cached);
        return map;
    } catch (Exception e) {
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        throw new ServerErrorException(msg);
    }
}
Also used : ServerErrorException(com.rdbcache.exceptions.ServerErrorException) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 14 with ServerErrorException

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

the class AnyKey method getAny.

public KeyInfo getAny(int index) {
    int size = size();
    if (index > size) {
        throw new ServerErrorException("getAny index out of range");
    }
    if (size == 0 || index == size) {
        KeyInfo keyInfo = null;
        if (size == 0) {
            keyInfo = new KeyInfo();
            keyInfo.setIsNew(true);
        } else {
            keyInfo = get(0).clone();
            keyInfo.setIsNew(true);
            keyInfo.clearParams();
        }
        add(keyInfo);
    }
    return get(index);
}
Also used : KeyInfo(com.rdbcache.models.KeyInfo) ServerErrorException(com.rdbcache.exceptions.ServerErrorException)

Example 15 with ServerErrorException

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

the class Query method executeInsert.

public boolean executeInsert(boolean enableLocal, boolean enableRedis) {
    params = new ArrayList<>();
    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();
        Map<String, Object> map = pair.getData();
        String autoIncKey = AppCtx.getDbaseOps().getTableAutoIncColumn(context, table);
        boolean cacheUpdate = false;
        if (!map.containsKey(autoIncKey) && keyInfo.getParams() != null && keyInfo.getParams().size() == 1) {
            String stdClause = "(" + autoIncKey + " = ?)";
            if (stdClause.equals(keyInfo.getClause())) {
                map.put(autoIncKey, keyInfo.getParams().get(0));
                cacheUpdate = true;
            }
        }
        Map<String, Object> columns = keyInfo.getColumns();
        AppCtx.getDbaseOps().convertDbMap(columns, map);
        String fields = "", values = "";
        params.clear();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            params.add(entry.getValue());
            if (fields.length() != 0) {
                fields += ", ";
                values += ", ";
            }
            fields += entry.getKey();
            values += "?";
        }
        sql = "insert into " + table + " (" + fields + ") values (" + values + ")";
        LOGGER.trace("sql: " + sql);
        LOGGER.trace("params: " + params.toString());
        int rowCount = 0;
        KeyHolder keyHolder = new GeneratedKeyHolder();
        StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
        try {
            rowCount = jdbcTemplate.update(new PreparedStatementCreator() {

                @Override
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps;
                    ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                    int i = 1;
                    for (Object param : params) {
                        ps.setObject(i++, param);
                    }
                    return ps;
                }
            }, keyHolder);
            if (stopWatch != null)
                stopWatch.stopNow();
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            allOk = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
        if (rowCount > 0) {
            if (autoIncKey != null && keyHolder.getKey() != null) {
                String keyValue = String.valueOf(keyHolder.getKey());
                map.put(autoIncKey, keyValue);
                cacheUpdate = true;
            }
            if (cacheUpdate) {
                if (enableLocal) {
                    AppCtx.getLocalCache().putData(pair, keyInfo);
                }
                if (enableRedis) {
                    AppCtx.getRedisRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
                }
            }
            if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
                String msg = "executeInsert failed when prepareStandardClauseParams for " + pair.getId();
                LOGGER.error(msg);
                context.logTraceMessage(msg);
                if (context.isSync()) {
                    throw new ServerErrorException(context, msg);
                }
            }
            LOGGER.trace("inserted " + pair.getId() + " into " + table);
        } else {
            allOk = false;
            LOGGER.warn("failed to insert " + pair.getId() + " into " + table);
        }
    }
    return allOk;
}
Also used : AnyKey(com.rdbcache.helpers.AnyKey) KvPair(com.rdbcache.models.KvPair) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) KvPairs(com.rdbcache.helpers.KvPairs) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) ServerErrorException(com.rdbcache.exceptions.ServerErrorException) SQLException(java.sql.SQLException) StopWatch(com.rdbcache.models.StopWatch) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) KeyInfo(com.rdbcache.models.KeyInfo) PreparedStatementCreator(org.springframework.jdbc.core.PreparedStatementCreator) 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