Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Aug 15, 2013
1 parent d9742ec commit c761650
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,13 @@ private void loadHashValue(FieldInfo hashField, List<?> list, Map<String, Object

for (Object statValue : list) {
try {
String sql = "select hash, value from druid_const where domain = ? AND app = ? and type = ? and value = ?";
Long hash = (Long) hashField.field.get(statValue);
String value = hashField.getCacheValue(hash);

if (value == null) {
value = getConstValueFromDb(domain, app, hashField.getHashForType(), hash);
}
hashField.getHashFor().set(statValue, value);
} catch (IllegalArgumentException e) {
throw new DruidRuntimeException("set field error" + hashField.getField(), e);
} catch (IllegalAccessException e) {
Expand All @@ -383,6 +388,36 @@ private void loadHashValue(FieldInfo hashField, List<?> list, Map<String, Object
}
}

protected String getConstValueFromDb(String domain, String app, String type, Long hash) {
String sql = "select value from druid_const where domain = ? AND app = ? and type = ? and hash = ?";

Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
stmt = conn.prepareStatement(sql);

stmt.setString(1, domain);
stmt.setString(2, app);
stmt.setString(3, type);
stmt.setLong(4, hash);

rs = stmt.executeQuery();
if (rs.next()) {
return rs.getString(1);
}
} catch (SQLException ex) {
LOG.error("save const error error", ex);
} finally {
JdbcUtils.close(rs);
JdbcUtils.close(stmt);
JdbcUtils.close(conn);
}

return null;
}

private void saveHash(FieldInfo hashField, MonitorContext ctx, List<?> list) {
final String hashType = hashField.getHashForType();

Expand Down Expand Up @@ -703,13 +738,17 @@ public void hashCacheAdd(long hash, String value) {
if (hashCache.size() > 1000) {
return;
}

hashCache.put(hash, value);
}

public boolean hashCacheContains(long hash) {
return hashCache.containsKey(hash);
}

public String getCacheValue(long hash) {
return hashCache.get(hash);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class MonitorDaoJdbcImplTest extends TestCase {
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:h2:mem:test");
dataSource.setInitialSize(2);
dataSource.setMinIdle(2);
dataSource.setInitialSize(1);
dataSource.setMinIdle(1);
dataSource.setFilters("wall,stat,log4j");
dataSource.init();
}
Expand Down

0 comments on commit c761650

Please sign in to comment.