In previous articles we have learned about Transaction scoped, Extended and Application managed entitymanagers. This article will simply help out choosing entitymanager for the scenarios in which these can be used.
Java can develop different types of applications and all can use JPA, but choosing entitymanager of proper type can be little bit confusing. Different entitymanagers have different lifecycle and they can be used for different purposes. Choosing such a type is based on what kind of application you are going to develop.
- Container managed, transaction scoped entitymanagers are best suited for most of the applications.
- Java EE applications need combination of flexible transaction propogation.
- So while choosing entitymanager for java EE applications, transaction scoped entitymanagers are the best choice.
- Consider a scenario in which you want the persistence context to be tied to a stateful session bean.
- The advantage of this persistence context is that the entities are managed even if transaction is committed.
- Choosing entitymanager of type extended becomes an obvious choice for such scenario.
- Application managed entitymanagers are rarely used in enterprise applications.
- Those are not propogated and need to be shared to be able to access throughout the application.
- Choosing entitymanager of this type is suitable for javaSE applications where entitymanager lifecycle is managed by the application.