File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and Other Java EE Technologies and the fly likes When unsetEntityContext() is called? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » EJB and Other Java EE Technologies
Reply Bookmark "When unsetEntityContext() is called?" Watch "When unsetEntityContext() is called?" New topic
Author

When unsetEntityContext() is called?

prashant wath
Greenhorn

Joined: Oct 19, 2001
Posts: 9
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

Joined: Aug 10, 2001
Posts: 3878
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.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
prashant wath
Greenhorn

Joined: Oct 19, 2001
Posts: 9
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

Joined: Aug 10, 2001
Posts: 3878
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.
 
 
subject: When unsetEntityContext() is called?
 
Threads others viewed
EJB Question?
CMP 2.0 doesn't write into db
MS Access, Jboss, BMP
Problem in bmp entity bean while running client?
ejbLoad is not getting called
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com