3

How do I get hibernate to fully log all details when an exception is thrown? I would like to see the full (not truncated) sql and the full (not truncated) parameters.

ie Hibernate is throwing:

Caused by: org.hibernate.exception.DataException: could not execute statement
long stack trace
Caused by: java.sql.SQLDataException: Incorrect string value:
Query is: blah blah blah ... 

I don't want to see any "..." there. I want to see the full sql and the full parameter list.

This is an unchecked exception. What is the configuration needed for this (and any other hibernate exceptions) to be fully logged?

As this is production code I would prefer not changing the jdbc driver to net.sf.log4jdbc.DriverSpy

(Left out the truncated sql + parameters for clarity)

bharal
  • 15,461
  • 36
  • 117
  • 195
  • Use a debugger and let is catch the unhandled exception? – Thomas Weller Jun 26 '20 at 17:31
  • you've never worked in production, huh? – bharal Jun 28 '20 at 08:56
  • You never used a debugger in production? Good luck... Since it crashes anyway, you could take a crash dump using [WER LocalDumps](https://learn.microsoft.com/de-de/windows/win32/wer/collecting-user-mode-dumps?redirectedfrom=MSDN). You can then do a [strings](https://learn.microsoft.com/en-us/sysinternals/downloads/strings) search with the partial string you have. – Thomas Weller Jun 28 '20 at 10:15
  • I cannot slow mine down hoping to catch a one off user error that happens randomly. this is a caught exception, the prod instance isn't crash and burning, so a crash dump isn't viable either. – bharal Jun 28 '20 at 15:39
  • 1
    Please add to question your current `hibernate.properties` or `hibernate.cfg.xml` (or equivalent) settings and the logging type (e.g. log4j) and logging level used for Hibernate. – Steve Chambers Jun 28 '20 at 22:04
  • Something to try: Set `-XX:MaxJavaStackTraceDepth=-1` as a JVM parameter, as per [this answer](https://stackoverflow.com/questions/437756#answer-50128144). Can't say whether this will actually fix this issue but it's worth ruling in or out... – Steve Chambers Jun 30 '20 at 15:14

2 Answers2

1

You can use p6spy library.

<dependency>
  <groupId>p6spy</groupId>
  <artifactId>p6spy</artifactId>
  <version>3.9.0</version>
</dependency>

The idea is wrapping the data source you use for Hibernate with p6spy proxy which logs everything as you configure it.

It may look like the following code if you use Hikari pool:

DataSource ds = new P6DataSource(new HikariDataSource(dsConfig));

The configuration of p6spy lays in spy.properties file which should be found in the root of your classpath (in the root of the jar file).

Here is the example of the spy.properties:

# suppress inspection "UnusedProperty" for whole file
#appender=com.p6spy.engine.spy.appender.StdoutLogger
appender=com.p6spy.engine.spy.appender.Slf4JLogger
excludecategories=info,debug,result,resultset
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(category) | connection %(connectionId) | \u001b[33m%(sqlSingleLine)\u001b[0m | %(executionTime) ms

As you can see, it is possible to configure how p6spy writes logs with the chosen appender.

If everything configured well, you see SQL queries and corresponding parameters in the logs.

Elena Bondareva
  • 178
  • 2
  • 6
0

Generally,(...) is used to indicate repeated logs lines in the stack trace, try increasing the value of
-XX:MaxJavaStackTraceDepth JVM parameter

Ahmed Maher
  • 1,521
  • 17
  • 22