Hibernate tools debugging
Now, how do we get all those benefits? Here are the steps I undertook to add logt4jdbc 1.1 to my existing project:ġ.1. Therefore, do not forget to set the e_sql_comments Hibernate property to true. Please, note that because Hibernate generates comments at the SQL level, log4jdbc can see and log them as well. For complex queries, which tend to be the interesting ones, this is a definite plus. However, Hibernate logging has a definite advantage: it logs human readable SQL. That is the “1” that appears in the first log, just before “/* insert…”.Īnd, even if it does show not in our example, log4jdbc allows us to log slow statements as warning or errors: we can set how many milliseconds make us consider a statement slow (and generate a warning log) or very slow (and generate an error log). That can be great to debug threading or pooling problems, because it helps us to know what statements belong together and what connection is really being used to execute a given statement.
HIBERNATE TOOLS DEBUGGING CODE
Of course, you need to compile your code enabling line number and file name information generation for your classes. It provides the name of the function, the file and the line number! This deserves to be repeated: log4jdbc can log the line in our code, instead of the one deep down in Hibernate. Log4jdbc can log the function in our code that caused the statement to be executed, in our case the persistAll method.Log4jdbc logs the SQL and writes the parameter values right there for prepared statements, whereas Hibernate logs them in separated lines, and is much more verbose.
Log4jdbc logs the time spent by every statement, 15 milliseconds in our example.If we call SQL directly in some place, we might miss some SQL being executed if we rely on Hibernate logs to tell us the whole truth. Log4Jdbc logs all SQL statements, whereas Hibernate only logs the SQL statements it generates.What does the log4jdbc log provide that the Hibernate log does not? Log4jdbc is an open source library that logs all JDBC calls for a given application. This last version is what Hibernate will log for us for a given SQL statement, while the first version is what log4jdbc will log for that same statement. TRACE: - "binding 'Article_1' to parameter: 3" TRACE: - "binding '27.2' to parameter: 2" Insert into Article (id, version, currentPrice, name)
Insert into Article (id, version, currentPrice, name) values (null, 0, 27.2, 'Article_1') "ĭEBUG: - "/* insert */
Know how much time it took to execute every SQL statement (sigh!).Get the SQL prepared statements with the parameter values right there, instead of getting each parameter in a different log line.Hibernate logs a lot of information we can exploit to get the SQL is it generating in the end. Using log4jdbc instead of Hibernate SQL logging This is the second part of Hibernate: logging SQL.