javaee7-samples icon indicating copy to clipboard operation
javaee7-samples copied to clipboard

JPA Extended Persistence Context

Open arun-gupta opened this issue 12 years ago • 4 comments

arun-gupta avatar Nov 09 '13 11:11 arun-gupta

Added jpa/extended-pc

arun-gupta avatar Nov 09 '13 12:11 arun-gupta

Needs further investigation. Entites are committed even when outside a transaction. Is that the expected behavior ?

arun-gupta avatar Nov 09 '13 12:11 arun-gupta

Hi Arun,

from my investigation: it looks like when we calling persistWithoutTransaction, method is without transaction context and employee entity is not persisted to the database, but stays in Persistence Context. When there is no JTA transaction, the Persistence Context is not propagated, outside of a transaction inserts, updates and deletes will be queued until you access the persistence context within a transaction. Extended Persistence Context is created once only and is used for every transaction within the scope of the class. Next when calling get where Entity Manager is invoked within transaction what means our Extended Persistence Context will be bound to this transaction and employee entity from previous call which is located in Persistence Context should be persisted into database.

Solution would be setting annotation @TransactionAttribute(TransactionAttributeType.NEVER) for class scope. In that case invoking get method has no influence for employee entity in Persistence Context because none JTA transaction will be bound with Extended Persistence Context.

I’ve created arquilian test for this sample and made some changes in your code. Tests for now works only with wildfly-managed-arquillian profile. What is the policy of writing arquillian tests for JPA samples ? Should tests cover every maven profile ? For example: glassfish-embedded profile needs a little different persistence.xml than wildfly-managed and defined datasource configuration.

https://github.com/nikom1337/javaee7-samples/commit/0caff5a5aaeb575863a3b7b26060944086f37093

Best regards, Korneliusz Rabczak

kornelrabczak avatar Dec 07 '13 16:12 kornelrabczak

@arun-gupta i have made some changes to make this more visual and straightforward

https://github.com/nikom1337/javaee7-samples/blob/jpa-extended-pc/jpa/extended-pc/

kornelrabczak avatar Dec 10 '13 18:12 kornelrabczak