2

I am implementing a Vaadin web application that connects with a database (using jpa hibernate). The aplication looks like a java-swing, but in the web.

I would like to know the best practices on how to handle the JPA Entity Manager lifecycle.

The book of vaadin explicit tells you to use EntityManager-per-Request.

However, I have seen others advocating the use of Entity-per-session, and so on.

My question is: what is the best practice and what would be the benefits and drawbacks of each choice?

Community
  • 1
  • 1
guilhermecgs
  • 2,913
  • 11
  • 39
  • 69

1 Answers1

2

EntityManager (EM) should not be "reused" according to JPA best practices. There is no expensive cost to create one to each request.

If you use one EM per session you will need to pay attention if you are removing every entity from the persistence context at the end of every request. If you do not remove entities from the persistence context, the EM will occupy a huge amount of ram.

If Vaadin has an automatic way to handler one EM per session, go ahead and use it. If you need to implement it, I think that the best way is one EM per request.

Aj Otto
  • 124
  • 1
  • 16
uaiHebert
  • 1,882
  • 11
  • 21