Vishwanath Krishnamurthi's blog

A blog on Java EE, clean code, open source and TDD

Understanding Persistence Context

with 3 comments

Here’s something on Persistence Context !

This understanding below, is mainly based on what I had read ( and re-read ) in this great article by Sahoo

But before you read further, these are the short-forms used in this post.

PC – Persistence Context

BMT – Bean managed transactions

CMT – Container Managed Transactions

SFSB – Stateful session beans

OO – Object Oriented

What is a Persistence Context ? 

Imagine a circle that depicts the OO world. We will call this the PC circle (Persistence Context circle) Imagine another circle that depicts the DB world.

The DB circle depicts only one data-store. There can be multiple entity instances within this PC. For each entity within the PC there is definitely a corresponding row in the DB world. All entities in this PC are managed i.e. any updates (CRUD) done here will be reflected in the DB world.

Well, its a collection of entity instances. And changes to those instances gets reflected in the db rows. Is it anything more ?

Yes. You can think of it as a cache too, since this is an in-memory collection. Once an object is “attached” it is a part of this in-memory-collection.

The term, ‘Persistence Context’ looks pretty abstract. Something more concrete ?

Any operation on the collection of entity instances (PC) is managed by an EntityManager. One entity manager would be associated with a persistence context.. Something like this

And anything in the PC is modified (CRUD) only using an EM.

Fine ! So when is a PC created ?

  • A persistence context is created as soon as the entity manager is created, in the case of Application managed persistence.
  • In the case of container managed persistence the PC is created when the EM is invoked.

 I want to access this collection of entity instances in another component (bean / servlet.. ) How can I do that ?

There are two options.

Option A:  Pass around the EM instance associated with the PC

Option B:  Let the container propagate the PC. ( And associate it with the entity manager of the component that was propagated to )

Option B being much cleaner.

This is called as Persistence Context Propagation. But there is a catch here. It doesn’t do it every time. When using BMT it thinks that you know what you’re doing and doesn’t propagate the PC. PC propagation comes up only with CMT. We know that a transaction can be propagated. REQUIRED, SUPPORTS allow transaction propagation. A PC is propagated along with such a transaction propagation.  But note that there’s no propagation to a bean that already has a PC associated with it !


Written by Vishwanath Krishnamurthi

August 21, 2011 at 7:21 pm

3 Responses

Subscribe to comments with RSS.

  1. Really good one.


    September 1, 2011 at 9:43 pm

  2. Understanding Persistence Context « Vishwanath Krishnamurthi’s blog…

    Thank you for submitting this cool story – Trackback from JavaPins…


    July 18, 2012 at 8:12 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: