• Post Reply Bookmark Topic Watch Topic
  • New Topic

When unsetEntityContext() is called?  RSS feed

 
prashant wath
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I made bean managed entity bean on weblogic.I am calling ejbFindByPrimaryKey method from jsp client.My bean is invoking unsetEntityContext() before ejbFindByPrimaryKey()& after setEntityContext().Again it calls setEntityContext() & then ejbFindByPrimaryKey().In this method i am populating the instance with desired data.But when I am calling bussiness method on the same instance,the value of public variable is 0 instead of the populated value.
Is it normal to call unsetEntityContext() initially before ejbFindByPrimaryKey().What can be the problem.
Pls advise me.
sample code
public myBean implements EntityBean
{
priavate EntityContext ctx;
public int balance;
public void setEntityContext(EntityContext ctx)
{
System.out.println("Inside setContext");
this.ctx=ctx;
}
public void unsetEntityContext()
{
System.out.println("Inside unsetContext");
ctx=null;
}
public myPK ejbFindByPrimaryKey(myPK pk)
{
//making database connection through DataSource.Taking
//balance from database
System.out.println("Inside PrimaryKey");
this.balance=rs.getInt(1);
System.out.println("Balance is----="+balance);
//here i am getting balance 5000 from database
}
//BussinessMethod
public int getBalance()
{
System.out.println("Inside getBalance");
return balance;
}
}
the output i am getting is
inside setContext//here context shows primarykey=null
inside unsetContext
inside primarykey
balance is 5000
inside ejbload
inside getBalance
//here i am getting balance=0 ? Why i am not getting.It should be 5000.
if anybody can find solution pls guide me.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please show your implementation of ejbLoad()? That is the crucial missing piece of this puzzle.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
prashant wath
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mr.Brown,
In ejbLoad(),i had retrived data simply with primary key variable.When i retrived data by primarykey object i am getting the desired results.Is it nessary to bring primarykey object in focus while loading the data?
One more problem i am facing regarding transactions.We can't use UserTransactions in entity bean?If so why?
pls guide me.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is normal behavior. There are a set of interaction diagrams in the spec that describe the order in which these methods are called. Basically, each bean instance is pulled from a pool -- you are NOT guaranteed that the same bean instance on which ejbFindByPrimaryKey() is called will be the same one in which ejbLoad() will be called -- in fact you are almost guaranteed that it will NOT be the same instance.
That's why you must pull the primary key used in your ejbLoad() from the Entity context -- you can't trust that it will be anywhere else. The framework guarantees that it will be placed in the Entity context in the right time.
As to why you can't use UserTransactions -- the idea is that you're supposed to be using declarative transactions. Your bean's DD should already have been set up to use TX_REQUIRES or TX_REQUIRES_NEW so that a transaction will already be in progress when you get to your methods. It doesn't make sense to start UserTransactions in an Entity bean -- when would they start and end? Surely you don't want your ejbLoad() and ejbStore() to be in different transactions, do you?
I would recommend you sit down and read Richard Monson-Haefel's "Enterprise JavaBeans" book. It explains this in some significant detail. Reading the specification would be good too.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!