• Post Reply Bookmark Topic Watch Topic
  • New Topic

Freeing entity bean's EJBObject

 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I call findByPrimaryKey on an entity bean's home, do I need to free the returned EJBObject in any way, or just let it get garbage collected?
What happens if I retain a reference to it and it never gets collected?
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick DeBay:
If I call findByPrimaryKey on an entity bean's home, do I need to free the returned EJBObject in any way, or just let it get garbage collected?

Unlike Session Beans, with Entity Beans you don't have to do anything. In fact, if you call remove() on the Entity Bean (like you would on a Session Bean) then it will cause the entity to be deleted from the database. Just let the reference go out of scope.

Originally posted by Rick DeBay:
What happens if I retain a reference to it and it never gets collected?

Don't.
[ June 08, 2004: Message edited by: Chris Mathews ]
 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply. I ask because I've inherited some code that doesn't clean up anything, and I have a fair amount of work ahead of me and want to make sure I don't try to fix anything that doesn't need immediate fixing.
I'm going to get rid of the persistent EJBObjects, they seem to be causing stale database connections.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick DeBay:
I'm going to get rid of the persistent EJBObjects, they seem to be causing stale database connections.

I am confused by the above statement. What is a "persistent EJBObject"? Are you referring to Entity Beans?
 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm referring to code that looks like:



The variable myEntity is created and just hangs around, not getting garbage collected unless the session bean is collected, or another call to bar() on the same bean reassigns the variable. And no, I have no idea why it was coded that way.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your best bet is simply to make it a temporary variable instead of an instance variable.

Kyle
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, your statement makes more sense now. BTW, I concur with Kyle (as if that is needed).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!