You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Data Source Decorator集成P6Spy无法记录Insert语句

排查P6Spy无法记录Insert语句的问题

问题场景

使用Spring Boot+Hibernate作为ORM框架,通过数据源装饰器集成P6Spy记录SQL语句,但spy.log中仅能记录Select语句,执行Insert操作时仅显示Commit操作,Insert语句未被记录。

日志示例:

|connection|commit||
|connection|statement | select * from emp_id where id=1234

当前配置:

# Register P6LogFactory to log JDBC events
decorator.datasource.p6spy.enable-logging=true
# Use com.p6spy.engine.spy.appender.MultiLineFormat instead of com.p6spy.engine.spy.appender.SingleLineFormat
decorator.datasource.p6spy.multiline=true
# Use logging for default listeners [slf4j, sysout, file, custom]
decorator.datasource.p6spy.logging=file
# Log file to use (only with logging=file)
decorator.datasource.p6spy.log-file=spy.log
# Class file to use (only with logging=custom). The class must implement com.p6spy.engine.spy.appender.FormattedLogger
decorator.datasource.p6spy.custom-appender-class=my.custom.LoggerClass
# Custom log format, if specified com.p6spy.engine.spy.appender.CustomLineFormat will be used with this log format
decorator.datasource.p6spy.log-format=
# Use regex pattern to filter log messages. If specified only matched messages will be logged.
decorator.datasource.p6spy.log-filter.pattern=
# Report the effective sql string (with '?' replaced with real values) to tracing systems.
# NOTE this setting does not affect the logging message.
decorator.datasource.p6spy.tracing.include-parameter-values=true

排查步骤与解决方法

1. 确认Hibernate是否实际执行了Insert语句

先排除ORM层面的问题:

  • 在application.properties中开启Hibernate的SQL日志:
    logging.level.org.hibernate.SQL=DEBUG
    logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
    
  • 执行Insert操作,观察控制台是否有Insert语句输出。如果没有,说明是Hibernate未执行Insert,可能原因:
    • 方法未添加@Transactional注解导致事务未提交
    • 未调用save()/saveAndFlush()方法触发持久化
    • 实体主键生成策略异常或事务被回滚

2. 检查自定义日志Appender的实现

你配置了custom-appender-class=my.custom.LoggerClass,需确认该类是否覆盖了所有JDBC操作场景:

  • 自定义Appender必须实现com.p6spy.engine.spy.appender.FormattedLogger接口
  • 检查logMessage()方法是否处理了prepared、batch类型的日志事件(Insert通常通过PreparedStatement或批量操作执行)
  • 排查是否存在代码逻辑过滤了Insert、Update等非Select语句

3. 启用P6Spy批量语句日志记录

Hibernate默认可能开启批量插入(如配置hibernate.jdbc.batch_size>0),此时JDBC调用executeBatch()批量执行SQL,P6Spy默认不记录批量语句。需添加配置:

decorator.datasource.p6spy.log-batch-statements=true

该配置会输出批量执行的每一条SQL语句。

4. 调整P6Spy日志格式

空的log-format可能导致部分操作信息丢失,建议显式配置包含必要字段的格式:

decorator.datasource.p6spy.log-format=%(connectionId)|%(timestamp)|%(category)|%(sqlSingleLine)|%(batch)|%(error)

其中%(batch)标记是否为批量操作,%(sqlSingleLine)输出完整的SQL语句内容。

5. 检查版本兼容性

确保数据源装饰器、P6Spy、Spring Boot三者版本匹配:

  • 查看数据源装饰器的官方文档,确认支持的Spring Boot和P6Spy版本范围
  • 尝试升级到最新稳定版,避免版本不兼容导致的SQL拦截失效

6. 确认数据源被正确装饰

检查Spring Boot是否正确加载了数据源装饰器:

  • 确保依赖中包含对应的starter包(如com.github.gavlyukovskiy:p6spy-spring-boot-starter)
  • 排查是否存在自定义DataSource Bean覆盖了装饰器的自动配置,需保证装饰器正确包装了实际数据源

内容的提问来源于stack exchange,提问作者user739115

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 04:25:39