Vaadin JPAContainer JDBC Connection Usage

后端 未结 1 903
一个人的身影
一个人的身影 2021-01-06 20:15

Once one JPAContainer is created like

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

Now I suppose that the \"users\" co

相关标签:
1条回答
  • 2021-01-06 20:55

    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.

    0 讨论(0)
提交回复
热议问题