场景:高并发情况下mybatis 动态sql 解析 锁问题优化
-
优化前 并发测试 XMLLanguageDriver 类 的 createSqlSource 方法有锁 而且 每次执行时都会走该方法
-
优化前 : 线程有Block
-
优化后的 LanguageDriver
public class CustomXMLLanguageDriver implements LanguageDriver {
private final Map<String, SqlSource> sqlSourceCache = new ConcurrentHashMap<>();
@Override
public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
}
@Override
public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
return builder.parseScriptNode();
}
@Override
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
if (script.startsWith("<script>")) {
SqlSource cachedSqlSource = sqlSourceCache.get(script);
if (cachedSqlSource != null) {
return cachedSqlSource;
}
XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver());
SqlSource sqlSource = this.createSqlSource(configuration, parser.evalNode("/script"), parameterType);
sqlSourceCache.put(script, sqlSource);
return sqlSource;
} else {
script = PropertyParser.parse(script, configuration.getVariables());
TextSqlNode textSqlNode = new TextSqlNode(script);
return (SqlSource) (textSqlNode.isDynamic() ? new DynamicSqlSource(configuration, textSqlNode) : new RawSqlSource(configuration, script, parameterType));
}
}
}
优化后使用相同的压测条件 发现线程没有Block 的 而且调用栈 中只有Druid 的 初始化线程池 有Block XMLLanguageDriver Block 消失
-
此时的 线程没有阻塞