Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL监控无数据--DataSource注入问题导致 #2394

Open
shubifeng opened this issue Mar 9, 2018 · 21 comments
Open

SQL监控无数据--DataSource注入问题导致 #2394

shubifeng opened this issue Mar 9, 2018 · 21 comments

Comments

@shubifeng
Copy link

image

@wenshao
Copy link
Member

wenshao commented Mar 10, 2018

https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter
是不是没有配置StatFilter?

@shubifeng
Copy link
Author

@wenshao 是不是这个配置?
我是springboot项目, stat值写上就可以了吗?

image

@lihengming
Copy link
Contributor

执行过SQL吗?

@lihengming
Copy link
Contributor

这一行是不用配的,默认就会启动stat filter,要启动其他filter请参考文档:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter#%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-filter

@lihengming
Copy link
Contributor

另外,建议使用最新版本,1.1.9

@shubifeng
Copy link
Author

shubifeng commented Mar 12, 2018

@lihengming 有执行SQL的,用的是最新版, 在这里可以看到调用了请求, 我在电脑的控制台也可以看到执行了SQL并返回了结果.

image

再咨询另外一个问题,
在A机器部署java应用, B机器是数据库应用. 把A机器网络关闭 再 打开网络, 再执行相关数据查询的时候 "connection has been closed." 这个需要怎么设置? 数据库使用的是gpsql.

@lihengming
Copy link
Contributor

https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8

配置validationQuery,结合testOnBorrow、testOnReturn、testWhileIdle

@shubifeng
Copy link
Author

@lihengming 配置validationQuery,结合testOnBorrow、testOnReturn、testWhileIdle , 但是还是没有效果. 怎么确定这些配置有没有生效呢?
image

@shubifeng
Copy link
Author

@lihengming https://github.com/lihengming/spring-boot-api-project-seed 你的这个项目跑起来后访问
http://localhost:8080/druid/ 也是看不到数据源和监控两项内容的. 有没有成功跑通过的demo发我一个?

@shubifeng
Copy link
Author

shubifeng commented Mar 12, 2018

@wenshao @lihengming
经测试,不能通过构造函数传入DataSource, 这种声明方式将导致无法使用数据库连接池的配置.

    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource oneDataSource() {
        log.info("dataSource 创建");
        return DataSourceBuilder.create().build();
    }

    @Bean
    public MyJdbcTemplate init(@Autowired DataSource dataSource) {
        log.info("MyJdbcDataSource 注入");
        return new MyJdbcTemplate(dataSource);
    }


注:
MyJdbcTemplate类中,使用构造函数注入

修改为注解注入DataSource后连接池生效

    @Bean
    public MyJdbcTemplate init() {
        log.info("MyJdbcDataSource 注入");
        return new MyJdbcTemplate();
    }

注:
在MyJdbcTemplate中使用注解注入 连接池配置生效

@Autowired
private DataSource dataSource;

为什么这里想做成构造函数注入呢,是想使用方可自行 设置任意数据源 , 不依赖spring默认的数据源.

@xjsunup
Copy link

xjsunup commented Mar 13, 2018

同问 问题解决没有, 我也是通过ioc手动管理datasource的,导致sql监控无法使用,只能监控到url

@shubifeng
Copy link
Author

@sunxuejian 我改回了spring自己提供的DataSource默认实例了, 是可以监控的. 具体什么问题需要翻druid源码才能知道. 按你的情况, sql无法监控 那么数据源连接池是没有正常生效的, 生产中会导致一些异常.

@xjsunup
Copy link

xjsunup commented Mar 13, 2018

@shubifeng 啊,之前一直没注意这个问题,如果数据源没生效,那么为什么我使用mybatis能正常访问db呢,帮忙看下我最新提交的issus,里面有我的截图

@xjsunup
Copy link

xjsunup commented Mar 13, 2018

@shubifeng 顺便能方便贴一下你最新的 代码或者配置吗,麻烦了

@shubifeng
Copy link
Author

@sunxuejian 将会导致这种问题: A机器部署java应用, B机器是数据库应用. 把A机器网络关闭再重启 或 B机器数据库重启 , 再执行A机器相关应用数据查询的时候 "connection has been closed." . mybatis能够正常使用说明数据库连接池中的连接没有问题, 但可能是连接池管理出了问题导致无法自动维系链接有效性.

@shubifeng shubifeng changed the title SQL监控执行后无数据 SQL监控无数据--DataSource注入问题导致 Mar 13, 2018
@xjsunup
Copy link

xjsunup commented Mar 13, 2018

@shubifeng 明天您的意思了,那说明我们的连接池生效了,我们上线之前测试过 , b机器上的数据库重启好了,我们a机器会自动重连,

@xiaoxiaobingguai
Copy link

我也遇到同样的问题,是配置多数据源之后。druid-spring-boot-starter用的版本是1.1.5,求高手解决。

@ChenJianzhao
Copy link

可能存在的情况之一
如果用 javaConf 或者 xml 的方式手动配置 Bean,需要指定开启的 Filters。

@Bean("mybatisPlusDataSource")
        public DataSource dataSource(Environment env) throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setFilters("stat");
        ......
        return dataSource;
}

如果不手动指定 Bean,使用配置指定 Druid 数据,会自动开启 statFilter

spring.datasource.url=your_url
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

@eshequ
Copy link

eshequ commented Jul 25, 2018

the same issue. any update for this ?

@davidhardson
Copy link

花时间调了下这个问题。在druid1.1.9和1.1.10中创建datasource的函数public DruidDataSource build(Environment env, String prefix) 引入了springframework的Environment作为参数。而这个玩意儿在我用过的springboot2.0.0以后的版本里应该是取不了多个filter的,比如你的filter写成filter=stat,wall,slf4j用env.getProperty("xxxFilter")获取值后出来的是个null,单独配置一个stat则是可以的。。。而且show slow sql这些参数写在配置文件里是不管用的,需要自定定义stat-filter

@wxmclub
Copy link

wxmclub commented Mar 27, 2019

我也遇到同样的问题了,我们是使用的自定义数据源,直接声明的DruidDataSource。
我看了一下druid-spring-boot-starter自动注册数据源部分,有个类DruidDataSourceWrapper,应该是因为没有给该数据源添加对应的StatFilter造成的。

    @Autowired(
        required = false
    )
    public void addStatFilter(StatFilter statFilter) {
        super.filters.add(statFilter);
    }

可以参照DruidDataSourceWrapper,再写一个数据源类,继承DruidDataSource,自定义数据源使用自己添加的这个类就可以了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants