【问题标题】:p6spy outputs sql messages twice with spring bootp6spy 使用 spring boot 两次输出 sql 消息
【发布时间】:2019-07-04 18:30:35
【问题描述】:

我想在 spring boot 中通过 p6spy 显示 sql 参数,因为 hibernate 显示的 sql 参数非常庞大。但由于某种原因,p6spy 记录器输出 sql 消息两次,尽管实际上对 db 的查询执行一次。通常的 spring 应用程序可以正常使用我的 p6spy 配置。 Spring Boot 应用程序正常使用休眠输出。

spy.properties:

driverlist=org.postgresql.Driver
appender=com.p6spy.engine.spy.appender.Slf4JLogger
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=time %(executionTime)|con %(connectionId)|%(sqlSingleLine)

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="layoutPattern">
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
        </Property>
    </Properties>

<Appenders>
    <Console name="stdout">
        <PatternLayout pattern="${layoutPattern}"/>
    </Console>


</Appenders>

<Loggers>
    <Root level="info">
        <AppenderRef ref="stdout" />
    </Root>
</Loggers>

</Configuration>

休眠配置:

@Configuration
@EnableTransactionManagement
public class HibernateConfig {

@Bean
public LocalSessionFactoryBean getSessionFactory(){

    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setPackagesToScan("bel.rdigital.p6spy.boot.test.model");

    Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty(DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
    hibernateProperties.setProperty(HBM2DDL_AUTO, "create");
    hibernateProperties.setProperty(SHOW_SQL, "true");

    sessionFactory.setDataSource(dataSource());
    sessionFactory.setHibernateProperties(hibernateProperties);

    return sessionFactory;
}

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.p6spy.engine.spy.P6SpyDriver");
    dataSource.setUrl("jdbc:p6spy:postgresql://localhost:5432/p6spy");
    dataSource.setUsername("postgres");
    dataSource.setPassword("postgres");
    return new P6DataSource(dataSource);
}

@Bean
public HibernateTransactionManager getTransactionManager() {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    transactionManager.setSessionFactory(getSessionFactory().getObject());
    return transactionManager;
}

}

我希望有一个 sql 输出,但得到两个具有不同 p6spy 连接的相似 sql 输出:

1) p6spy - 时间 1|con 2|插入 serverstartups (applicationName, hostName, ip, startDate, stopDate, startupId) 值 ('', 'DIMON-LAPTOP', '192.168.88.244', '2019-02' -11T12:00:46.989+0300', NULL, 1)

2) p6spy - 时间 1|con 5|插入 serverstartups (applicationName, hostName, ip, startDate, stopDate, startupId) 值 ('', 'DIMON-LAPTOP', '192.168.88.244', '2019-02 -11T12:00:46.989+0300', NULL, 1)

如您所见,除了连接(2 和 5)之外,这些输出是相似的

【问题讨论】:

    标签: java hibernate spring-boot p6spy


    【解决方案1】:

    Spring Boot 不会自动为 DataSource 创建包装器。但是 p6spy 有2 ways to intercept:

    使用 P6DataSource 包装您的 DataSource 或修改您的连接 URL 添加“p6spy:”。

    你不应该同时使用两者。

    【讨论】:

      【解决方案2】:

      原来spring boot会自动为p6spy创建wrapper,所以换行解决了这个问题

      return new P6DataSource(dataSource)
      

      上线

      return dataSourse;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-12
        • 1970-01-01
        • 2021-11-12
        • 1970-01-01
        • 2016-02-01
        • 2012-03-18
        • 1970-01-01
        • 2020-04-05
        相关资源
        最近更新 更多