Search in sources :

Example 1 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class DbaseOps method fetchTablesAutoIncrementColumns.

// get auto increment column per table
// 
private Map<String, Object> fetchTablesAutoIncrementColumns(Context context) {
    if (databaseType.equals("mysql")) {
        Map<String, Object> autoIncMap = new LinkedHashMap<>();
        JdbcTemplate jdbcTemplate = AppCtx.getJdbcTemplate();
        fetchMysqlTableAutoIncrementColumns(context, jdbcTemplate, autoIncMap);
        JdbcTemplate systemJdbcTemplate = AppCtx.getSystemJdbcTemplate();
        if (jdbcTemplate != systemJdbcTemplate) {
            fetchMysqlTableAutoIncrementColumns(context, systemJdbcTemplate, autoIncMap);
        }
        return autoIncMap;
    }
    if (databaseType.equals("h2")) {
        Map<String, Object> autoIncMap = new LinkedHashMap<>();
        JdbcTemplate jdbcTemplate = AppCtx.getJdbcTemplate();
        fetchH2TableAutoIncrementColumns(context, jdbcTemplate, autoIncMap);
        JdbcTemplate systemJdbcTemplate = AppCtx.getSystemJdbcTemplate();
        if (jdbcTemplate != systemJdbcTemplate) {
            fetchH2TableAutoIncrementColumns(context, systemJdbcTemplate, autoIncMap);
        }
        return autoIncMap;
    }
    throw new ServerErrorException("database type not supported");
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 2 with ServerErrorException

use of doitincloud.commons.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(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) EventListener(org.springframework.context.event.EventListener)

Example 3 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class RedisRepoImpl method update.

@Override
public boolean update(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("update pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    }
    boolean foundAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        if (enableDataCache) {
            AppCtx.getCacheOps().updateData(pair);
        }
        String key = pair.getId();
        String type = pair.getType();
        String hashKey = hdataPrefix + "::" + type + ":" + key;
        Map<String, Object> map = pair.getData();
        StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
        try {
            hashOps.putAll(hashKey, map);
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.debug("update redis for " + key);
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            foundAll = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            e.printStackTrace();
            throw new ServerErrorException(context, msg);
        }
    }
    LOGGER.debug("update returns " + foundAll);
    return foundAll;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 4 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class RedisRepoImpl method update.

@Override
public boolean update(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("update: : " + pair.printKey() + " " + keyInfo.toString());
    }
    if (enableDataCache) {
        AppCtx.getCacheOps().updateData(pair);
    }
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    Map<String, Object> map = pair.getData();
    StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
    try {
        hashOps.putAll(hashKey, map);
        if (stopWatch != null)
            stopWatch.stopNow();
        LOGGER.debug("update redis for " + key);
    } catch (Exception e) {
        if (stopWatch != null)
            stopWatch.stopNow();
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        e.printStackTrace();
        throw new ServerErrorException(context, msg);
    }
    LOGGER.debug("update returns true");
    return true;
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 5 with ServerErrorException

use of doitincloud.commons.exceptions.ServerErrorException in project rdbcache by rdbcache.

the class RedisRepoImpl method findAndSave.

@Override
public boolean findAndSave(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("findAndSave: " + pair.printKey() + " " + keyInfo.toString());
    }
    boolean allOk = true;
    String key = pair.getId();
    String type = pair.getType();
    String hashKey = hdataPrefix + "::" + type + ":" + key;
    Map<String, Object> map = pair.getData();
    Map<String, Object> fmap = null;
    if (enableDataCache) {
        fmap = AppCtx.getCacheOps().getData(pair.getIdType());
        if (fmap != null && fmap.size() > 0) {
            LOGGER.debug("findAndSave - found from cache " + key);
        }
    }
    StopWatch stopWatch = null;
    if (fmap == null) {
        try {
            stopWatch = context.startStopWatch("redis", "hashOps.entries");
            fmap = hashOps.entries(hashKey);
            if (stopWatch != null)
                stopWatch.stopNow();
            if (fmap != null && fmap.size() > 0) {
                LOGGER.debug("findAndSave - found from redis " + key);
            }
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            e.printStackTrace();
            throw new ServerErrorException(context, msg);
        }
    }
    if (enableDataCache) {
        AppCtx.getCacheOps().putData(pair, keyInfo);
    }
    pair.setData(fmap);
    try {
        stopWatch = context.startStopWatch("redis", "hashOps.putAll");
        hashOps.putAll(hashKey, map);
        if (stopWatch != null)
            stopWatch.stopNow();
        LOGGER.debug("findAndSave - save " + key);
    } catch (Exception e) {
        if (stopWatch != null)
            stopWatch.stopNow();
        if (enableDataCache) {
            AppCtx.getCacheOps().removeData(pair.getIdType());
        }
        allOk = false;
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        e.printStackTrace();
    }
    if (allOk) {
        allOk = fmap != null && fmap.size() > 0;
    }
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("findAndSave returns " + allOk);
    return allOk;
}
Also used : ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Aggregations

ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)29 StopWatch (doitincloud.rdbcache.models.StopWatch)12 KeyInfo (doitincloud.rdbcache.models.KeyInfo)9 KvPair (doitincloud.rdbcache.models.KvPair)9 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 AnyKey (doitincloud.rdbcache.supports.AnyKey)4 KvPairs (doitincloud.rdbcache.supports.KvPairs)4 SQLException (java.sql.SQLException)4 Query (doitincloud.rdbcache.queries.Query)1 Context (doitincloud.rdbcache.supports.Context)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