Steve Chambers Steve Chambers - 1 year ago 43
MySQL Question

Debugging: Inspecting data within a database transaction

I'm running an integration test that executes some Hibernate code within a single transaction (managed by Spring). The test is failing with a duplicate key violation and I'd like to hit a breakpoint just before this and inspect the table contents. I can't just go into MySQL Workbench and run a

query as it would be outside the transaction. Is there another way?

Answer Source

Assuming Apache Commons is available, the following can be run from Eclipse's "Display" view:

new org.apache.commons.lang3.builder.ReflectionToStringBuilder(
  entityManager.createNativeQuery("SELECT mystuff FROM mytable").getResultList().toArray(),
    new org.apache.commons.lang3.builder.RecursiveToStringStyle()).toString();

The output is an array of objects so toString() would only include the object references rather than contents - hence it is being "pretty printed" instead, using a recursive variant of the toString builder - see this SO answer. It can then be copied into a JSON formatter such as or Notepad++ with the JSTool plugin. This displays all the data, albeit not in a very user-friendly way - e.g. will need to work out which columns the comma-separated fields correspond to.