Transaction in not-transactional environment

Some days ago I had problem strange problem in JavaEE application on Glassfish application server.

Let’s I explain it. I have two JPA entities Person and Address, which are linked via @ManyToOne relation. And I have service-method (in Stateless bean) – it updates Person and liked Address. The problem was that TopLink did not delete Address entities in this service-method.

First I thought that it deals with TopLink and its configuration, but later I found that entities are deleted after stopping server. So reason was in ending transaction!

In this application I use GWT and servelets. All this parts are build on Magnolia CMS community edition, which is responsible for configuration servelets and request handling chain. Community edition of Magnolia CMS does not support transaction! So all my request are working in not-transaction environment. It is reason of my problem!

Here I see several possible solutions:

  • add request filter, which will start and finish transaction before calling service methods – but not all requests should be run within transaction
  • use manual transactions (bean transaction management) – but it requires additional development efforts
  • use REQUIRES_NEW transaction type for all services, which requires transaction – it seams it is best way! So I use it!
So I have changed my code in accordance to it:

It is easy and it works!