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;
}
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();
}
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);
}
}
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);
}
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;
}
Aggregations