datacap icon indicating copy to clipboard operation
datacap copied to clipboard

🐛 [Bug] [SPI] Data source concurrency exception

Open Smilewh888 opened this issue 3 years ago • 5 comments

Contribution guidelines

I've found a bug and checked that ...

  • [X] ... the problem doesn't occur with the server or web themes
  • [ ] ... the documentation does not mention anything about my problem
  • [ ] ... there are no open or closed issues that are related to my problem

Description

当创建多个同类型数据源的时候,这种情况是比较常见的。 例如:mysql1 与mysql2 ,每1s重新加载数据源 mysql2的连接信息很可能覆盖掉mysql1的连接,导致执行sql在其他连接导致不可预知的错误。

成员变量: image 方法引用变量: image

Expected behaviour

.

Actual behaviour

.

Steps to reproduce

1.准备俩个不同的连接,mysql1,mysql2

2.mysql1 执行慢查询,mysql2 执行将会覆盖 mysq1 的成员属性,MySQLConnection mySQLConnection; Response response;

3.此时mysql1 在执行一条sql语句,导致错误。

Package versions

  • Java: java --version
  • Node: node --version
  • Npm: npm -v
  • Server: ./mvnw -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec -Dorg.slf4j.simpleLogger.defaultLogLevel=WARN -Dorg.slf4j.simpleLogger.log.org.apache.maven.plugins.help=INFO | tail -1

Configuration

.

System information

  • Operating system: ...
  • Browser: ...

Smilewh888 avatar Nov 13 '23 03:11 Smilewh888

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Title: 🐛 [Bug] Data source concurrency exception

Issues-translate-bot avatar Nov 13 '23 03:11 Issues-translate-bot

Message that will be displayed on users' first issue

qianmoQ avatar Nov 13 '23 03:11 qianmoQ

感谢您的反馈,我们将会按照您提供的步骤进行测试并修复它


Thanks for your feedback, we will follow the steps you provided to test and fix it

qianmoQ avatar Nov 13 '23 04:11 qianmoQ

代码中存在大量的插件,这是否是一种好的修改方式? 或者修改为原型模式可能不需要修改这么多代码,作为底层插件代码修改是否合理? @Slf4j public class MySQLPlugin implements Plugin { private final ThreadLocal mySQLConnectionThreadLocal =new ThreadLocal<>();

@Override
public String name()
{
    return "mysql";
}

@Override
public String description()
{
    return "Integrate MySQL data sources";
}

@Override
public PluginType type()
{
    return PluginType.JDBC;
}

@Override
public void connect(Configure configure)
{
    try {
        Response response = new Response();
        JdbcConfigure jdbcConfigure = new JdbcConfigure();
        BeanUtils.copyProperties(jdbcConfigure, configure);
        jdbcConfigure.setJdbcDriver("com.mysql.jdbc.Driver");
        jdbcConfigure.setJdbcType("mysql");
        this.mySQLConnectionThreadLocal.set(new MySQLConnection(jdbcConfigure, response));
    }
    catch (Exception ex) {
        log.error("连接失败:",ex);
    }
}

@Override
public Response execute(String content)
{
    Response response=null;
    if (ObjectUtils.isNotEmpty(this.mySQLConnectionThreadLocal.get())) {
        log.info("Execute mysql plugin logic started");
        MySQLAdapter processor = new MySQLAdapter(this.mySQLConnectionThreadLocal.get());
        response = processor.handlerExecute(content);
        log.info("Execute mysql plugin logic end");
    }
    return response;
}

@Override
public void destroy()
{
    if (ObjectUtils.isNotEmpty(this.mySQLConnectionThreadLocal)) {
        this.mySQLConnectionThreadLocal.get().destroy();
        this.mySQLConnectionThreadLocal.remove();
    }
}

}

Smilewh888 avatar Nov 13 '23 06:11 Smilewh888

代码中存在大量的插件,这是否是一种好的修改方式? 或者修改为原型模式可能不需要修改这么多代码,作为底层插件代码修改是否合理? @slf4j public class MySQLPlugin implements Plugin { private final ThreadLocal mySQLConnectionThreadLocal =new ThreadLocal<>();

@Override
public String name()
{
    return "mysql";
}

@Override
public String description()
{
    return "Integrate MySQL data sources";
}

@Override
public PluginType type()
{
    return PluginType.JDBC;
}

@Override
public void connect(Configure configure)
{
    try {
        Response response = new Response();
        JdbcConfigure jdbcConfigure = new JdbcConfigure();
        BeanUtils.copyProperties(jdbcConfigure, configure);
        jdbcConfigure.setJdbcDriver("com.mysql.jdbc.Driver");
        jdbcConfigure.setJdbcType("mysql");
        this.mySQLConnectionThreadLocal.set(new MySQLConnection(jdbcConfigure, response));
    }
    catch (Exception ex) {
        log.error("连接失败:",ex);
    }
}

@Override
public Response execute(String content)
{
    Response response=null;
    if (ObjectUtils.isNotEmpty(this.mySQLConnectionThreadLocal.get())) {
        log.info("Execute mysql plugin logic started");
        MySQLAdapter processor = new MySQLAdapter(this.mySQLConnectionThreadLocal.get());
        response = processor.handlerExecute(content);
        log.info("Execute mysql plugin logic end");
    }
    return response;
}

@Override
public void destroy()
{
    if (ObjectUtils.isNotEmpty(this.mySQLConnectionThreadLocal)) {
        this.mySQLConnectionThreadLocal.get().destroy();
        this.mySQLConnectionThreadLocal.remove();
    }
}

}

建议通过 https://github.com/devlive-community/datacap/blob/dev/CONTRIBUTING.md 这种方式提交代码

qianmoQ avatar Nov 13 '23 07:11 qianmoQ