Recently in our project we were reported a strange bug. In a one report where we display historical data provided by Hibernate Envers, users encountered duplicated records in dropdown used for filtering. We tried to find a source of this bug, but after spending a few hours looking at the code responsible for this functionality we had to give up and ask for a dump from production database to check what actually is stored in one table. And when we got it and started investigating, it turned out that there is a bug in Hibernate Envers 3.6 that is a cause of our problems. But luckily after some investigation and invaluable help from Adam Warski (author of Envers) we were able to fix this issue.

Bug itself

Let’s consider following scenario:

  1. Transaction is started. We insert some audited entities during it and then it is rolled back.
  2. The same EntityManager is reused to start another transaction
  3. Second transaction is committed

But when we check audit tables for entities that were created and then rolled back in step one we will notice that they are still there and were not rolled back as we could expect. We were able to reproduce it in a failing test in our project so next step was to prepare failing test in Envers so we could verify if our fix is working.

Failing test

The simplest test cases already present in Envers are located in Simple.java class and they look quite straightforward:

so preparing my failing test executing scenario described above wasn’t a rocket science:

Now I could verify that tests is failing on forked 3.6 branch and check if fix that we had is making this test green.

The fix

After writing failing test in our project, I placed several breakpoints in Envers code to understand better what is wrong there. But imagine being thrown in a project developed for a few years by many programmers smarter than you. I felt overwhelmed and have no idea where fix should be applied and what exactly is not working as expected. Luckily in my company we have Adam Warski on board. He is the initial author of Envers and actually he pointed us the solution.

The fix itself contains only check that registers audit process that will be execute on transaction completion only when such process is still in the Map<Transaction, AuditProcess> for given transaction. It sounds complicated, but if you look at the class AuditProcessManager in this commit it should be more clear what is happening there.

Official path

Besides locating problem and fixing it, there are some more official steps that must be performed to have fix included in Envers.

Step 1. Create JIRA issue with bug – https://hibernate.onjira.com/browse/HHH-7682

Step 2: Create local branch Envers-bugfix-HHH-7682 of forked Hibernate 3.6

Step 3: Commit and push failing test and fix to your local and remote repository on Github

Step 4: Create pull request – https://github.com/hibernate/hibernate-orm/pull/393

Step 5: Wait for merge

And that’s all. Now fix is merged into main repository and we have one bug less in the world of open source 🙂