[hibernate-dev] Usage of Session#getTransaction() being banned from the Hibernate Search code

Sanne Grinovero sanne at hibernate.org
Tue Sep 13 10:29:20 EDT 2016


Since Hibernate ORM 5.2, the method getTransaction() on Session needs
to behave according to EntityManager spec, which implies that it has
to throw an exception in certain circumstances which depend on the
configuration.

Hibernate Search used this method in various places, for example to
integrate with the current transaction's events, or even to control
the transaction explicitly in the case of the MassIndexer.

Since we want Hibernate Search to work fine with Hibernate ORM no
matter what configuration is being used, we need to avoid invoking
this method.
The solution is extremely simple: use its SPI level replacement, which
is SessionImplementor#accessTransaction().

Unfortunately most of our Search/ORM tests happen to run without a
Transaction Manager so if you happen to use the old method, the tests
would pass and everything would seem fine - however your shiny new
feature would not work in certain configurations.

One solution to verify we're not using it, is to ban this method using
the "forbiddenapi" plugin:
 - https://github.com/Sanne/hibernate-search/commit/a980ee5dca0c7a58dd79ba98acd8a354bc5601e6#diff-600376dffeb79835ede4a0b285078036R1036

A more comprehensive integration test would be to re-run all tests
from the Search/ORM using a proper JTA configuration; not rushing to
refactor our testsuite now since we have the forbidden-apis plugin but
opening a JIRA task for 5.7, as this version will support ORM 5.7:
 - https://hibernate.atlassian.net/browse/HSEARCH-2344

Thanks,
Sanne


More information about the hibernate-dev mailing list