David Hofmann
David Hofmann

Reputation: 5775

Vaadin JPAContainer JDBC Connection Usage

Once one JPAContainer is created like

JPAContainer users = JPAContainerFactory.make(User.class, "persistenceUnitName");

Now I suppose that the "users" container will use one EntityManager which in turn uses one JDBC connection from the connection pool.

That resource utilization (jdbc Connection attached to EntityManager attached to JPAContainer) happens during the lifetime of the httprequest or the usage of the entityManager/connection has another lifespan ?

Can you please help me understand the gap between one JPAContainer instance and when and how jdbc connections are used through the EntityManager ?

I read the vaadin jpa container tutorial and I don't find this information there. Thank you.

https://vaadin.com/forum/-/message_boards/view_message/1601953

Upvotes: 3

Views: 2820

Answers (1)

Milan Baran
Milan Baran

Reputation: 4232

JPAContainer initialization via:

JPAContainerFactory.make(User.class, "persistenceUnitName"); 

uses one and only one EntitiyManager during whole application life span, even other sessions use the same EntityManager. Also, this EntityManager has open one DB connection and it appears to be busy all the time. This approach is not very optimal and it can be bottleneck for your application performance.

Well, JPAContainer can be initialized via:

JPAContainerFactory.make(User.class, (EntityManager)enityManager); 

in this approach you have to handle when to create new EntityManager (EM). You can create new EM per User/Session or per User/Sesssion and Entity it is up to you. This looks promising but JPAContainer has other bottleneck. JPAContainer use one busy connection per EntityManager. So if you create 100 JPAContainer with its own entityManager your connection pool will contain 100 busy connection and this is a big problem. So, you have to set connection release mode to "after_transaction" this will force JPAContainer to release connection after each query.

persistence.xml

<property name="hibernate.connection.release_mode" value="after_transaction" />

Anyway, these are just trick, which make JPAContainer quite usable, but do not expect magic. JPAContainer has much more other issues

  • do not support lazy loading
  • do not support batch loading, each entry is loaded by one query + one query for each relation
  • if you want to refresh JPAContainer it cycle itself and takes forever to refresh

Look at this post. It's better to use plain JPA or Hibernate namedQuery or CriteriaBuilder with BeanItemContainer. Save changes to database vaadin.

Upvotes: 6

Related Questions