• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB Detached State

 
Vijaypal Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have been using ejb 3 for quite a few days and I have read the specs on "detached state" of an bean but I'am not sure when exactly an entity bean is in a "Detached State" .To clarify further what I want to understand is how can I create a situation when I can print say "In Detached state" .
Any help on the issue is most welcome...

Thanks in advance..
 
Mohammad Jaber
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
The Attached Status happened when the Bean belongs to a Persistence Context , hence the Bean Values are in sync with the associated DB values , but when the transaction end (for the transaction scoped) for example it becomes Deattached and its treated like any ordinary JavaBean ,

Regards ,
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this to the ORM forum.

Mark
 
Debu Panda
author
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An entity becomes detached when it is out of scope, removed, serialized,
or cloned. For example, an instance of an entity will become detached when the underlying transaction commits.

Take an example, you find an entity instance by PK within a transactional session bean method as soon as the bean method returns (transaction commits or rollbacks), the entity instance will be detached.

regards
Debu
 
Vijaypal Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,so that means whenever we are executing a Sessin bean method the the Entity bean is not in a detached state,but attached state so that would mean I can never call a method on a detached bean.I think I'am not getting it properly can you please clarify.
 
Debu Panda
author
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the bean is attached .. you are sure that the state of the entity is in sync with the database. The JPA provider will synchronize the state with the DB if you make any change in the entity.

You can invoke methods on a detached entity state but the JPA provider will not sync its state with the DB unless you merge the detached entity

Hope this clarifies!

-Debu
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You "detach" from a persistency mechanism. Usually that means at its base, a JDBC Connection. Normal ORM for EJB - and JDO retrieves the data into persistent beans, but leaves them attached. Because they're attached, you don't have to explicitly reference their persistence mechanism - it's, well, it's attached. To the persistent object. Using the appropriate methods, a persistency mechanism can save the time and effect of establishing a database connection to the object, by drawing on that attachment to retrieve the connection info.

However, java.sql.Connection - not to mention various other popular constructs - is not a serializable class, it's an Interface. So if you have a need to store or copy the persistent object somewhere outside its current context, you have to detach the non-Serializable stuff. You also do this if you want to be able to disconnect the persistence manager and return its resources to the pool. EJB3 calls the PM something different, but same idea.

If you're attempting to capture a set of objects related to each other and detach them, you may have to do something to ensure that all the dependent objects have been populated before detaching. Otherwise they'll come back as NULL later, since without the persister connection it's impossible to get anything more from the database.

There's also another pitfall. If you're re-attaching, not all ORM systems literally re-attach. I ran into a lot of trouble using JDO on that account, because the returned item(s) from a re-attach weren't the same items that I'd passed into the re-attach method call. They were cloned copies of those items, except that the copies had persistency and the original items did not.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic