*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes When did the Stateful Sessionbean created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "When did the Stateful Sessionbean created?" Watch "When did the Stateful Sessionbean created?" New topic
Author

When did the Stateful Sessionbean created?

Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
IN Enterprise JavaBeans3.0(O'REILLY),has following descriptions:

1,When a client invokes the first method on the stateful session bean reference, the bean's life cycle begins.

2,Every time you look up a stateful session bean in JNDI, a new session is created.
____________________
So,I want to kown,When did the Stateful Sessionbean created?
Thanks in advance!


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Everytime, the client invokes a method, a new Stateful Session Bean is created. So each time it goes through it's lifecycle.

As Stateful Session Bean's cannot be pooled.

So everytime invoking the method's create's new Stateful Session Bean's.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Remember, the container may not manage a pool for stateful bean instances. Stateful beans are always associated with a client. When JNDI lookup (or dependency injection) is performed for a stateful bean, an instance of the stateful bean will be created.
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
But,what did that?
"the client invokes a method, a new Stateful Session Bean is created" OR "When JNDI lookup (or dependency injection) is performed for a stateful bean, an instance of the stateful bean will be created"
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9344
    
    2

Originally posted by Kenneth Lomvey:
Remember, the container may not manage a pool for stateful bean instances. Stateful beans are always associated with a client. When JNDI lookup (or dependency injection) is performed for a stateful bean, an instance of the stateful bean will be created.


I guess as per the Specs, a container should not manage a pool for Stateful Session Beans. Please correct me if I'm wrong.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
Amandeep

Everytime, the client invokes a method, a new Stateful Session Bean is created. So each time it goes through it's lifecycle.


your explanation certainly not correct

Container will create if client has not a reference. If it create for every method call what is the usefulness of SF over SL. We you SFs when client can't keep his previous status like for http client (But http session would be lightweight, efficient than SFSB in general)


Jothi


I guess as per the Specs, a container should not manage a pool for Stateful Session Beans. Please correct me if I'm wrong.


yes you are correct
[ November 25, 2008: Message edited by: Chaminda Amarasinghe ]
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
your explanation certainly not correct

Container will create if client has not a reference. If it create for every method call what is the usefulness of SF over SL. We you SFs when client can't keep his previous status like for http client (But http session would be lightweight, efficient than SFSB in general)



Chaminda, what do you understand by this line, when i say, when a client invokes a method, SFB is created.

I mean to say here, when everytime the client invokes a first method on SFB for which it has no reference, it creates SFB everytime.

So your this reply turns out to be incorrect response.

your explanation certainly not correct

Container will create if client has not a reference. If it create for every method call what is the usefulness of SF over SL. We you SFs when client can't keep his previous status like for http client (But http session would be lightweight, efficient than SFSB in general)


Jothi
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Amandeep,

You are correct by saying...
...when everytime the client invokes a first method on SFB for which it has no reference, it creates SFB everytime.



But, as the second post of this topic, you said...
Everytime, the client invokes a method, a new Stateful Session Bean is created. So each time it goes through it's lifecycle.


You are wrong by this. Because only the first invocation on a SFB (by a client) may create a new instance.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Kenneth thanks for your reply...

Let's have a scenario, where a client calls this SFB.

-------------
package Pack;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

/**
* Session Bean implementation class CarCreatorBean
*/
@Stateful(name="StatelessSessionBean", mappedName="ejb/SimpleJNDIName")
public class CarCreatorBean implements CarCreator {

/**
* Default constructor.
*/
public CarCreatorBean() {
// TODO Auto-generated constructor stub
}

@PersistenceContext(unitName ="actionBazaar")
private EntityManager entityManager;

public void addCarComponents(String horn, String steering, String wheel){
Car car = new Car("horn", "steering", "wheel");
entityManager.persist(car);
entityManager.setFlushMode(FlushModeType.COMMIT);
}
public void addMergeCarComponents(String horn, String steering, String wheel){
Car car = new Car("horn", "steering", "wheel");
entityManager.merge(car);
}
public void undoCarComponents(String horn, String steering, String wheel){
Car car = new Car("horn", "steering", "wheel");
entityManager.refresh(entityManager.merge(car));
}
public void deleteCarComponents(Long id){
entityManager.remove(entityManager.find(Car.class, id));

}

public Car retrieveItem(Long id){
Car car = entityManager.find(Car.class, id);
return car;

}


}
-----------------------------

Suppose a client calls this bean, so it will add car new components.

What do you think, even if the client calls this SFB 2nd time or 3rd time..., will it make a new instance of SFB?
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Originally posted by Amandeep Singh:
What do you think, even if the client calls this SFB 2nd time or 3rd time..., will it make a new instance of SFB?


No, directly...!
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
I'm not sure, if it is specified, wether the bean instance is created when "lookup" is called or when the first "addCar" call occurs. Glassfish seems to create the instance with the lookup call.

By the way, you can check if the bean instances are equal by "cc1.equals(cc2);" (see core spec 3.4.5.1)

Hope this helps.
[ November 25, 2008: Message edited by: Ralph Jaus ]

SCJP 5 (98%) - SCBCD 5 (98%)
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
So that's what i mean, whenever the client is doing lookup, a new SFB instance is created. Right.

So what i meant in previous post's is that, whenever the client invokes the method, a new instance will be created.

See everytime, the addCar method is called, it means a new transaction starts and when i commit in the database, the transaction end's.

SO now you agree, the SFB can have only 1 transaction.

if SFB has only 1 transaction then also it mean's a new intance is created for every new transaction.

So again the new transaction is created only, when the client inokes the method or do lookup for SFB.


Hope this clears!
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
You're mixing two things:

1. creating a bean instance
2. starting / ending transactions

if SFB has only 1 transaction then also it mean's a new intance is created for every new transaction.

So again the new transaction is created only, when the client inokes the method or do lookup for SFB.
No, that's definitively wrong. In my CarClient example each cc1.addCar(..) call will start a new transaction but will not lead to the creation of a new bean instance: Just insert some println into the constructor of your bean class. The number of outputs will correspond to the number of lookup's not to the number of addCar calls resp. number of performed transactions.
Tomaszz Lewandowski
Ranch Hand

Joined: Sep 07, 2007
Posts: 30
Hi,

In my opinion specs are clear in this matter. Core spec says (p.74):

The following steps describe the life cycle of a stateful session bean instance:
- A session bean instance's life starts when a client obtains a reference to a stateful session bean instance through dependency injection or JNDI lookup, or when the client invokes a create<METHOD> method on the session bean's home interface. This causes the container to invoke newInstance on the session bean class to create a new session bean instance.


Regards<br />Tomasz Lewandowski<br />SCJP5 (97%) | SCWCD5 (98%)
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
this only i meant to say

The following steps describe the life cycle of a stateful session bean instance:
- A session bean instance's life starts when a client obtains a reference to a stateful session bean instance through dependency injection or JNDI lookup, or when the client invokes a create<METHOD> method on the session bean's home interface. This causes the container to invoke newInstance on the session bean class to create a new session bean instance.
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
Thanks everyone!

The following steps describe the life cycle of a stateful session bean instance:
- A session bean instance's life starts when a client obtains a reference to a stateful session bean instance through dependency injection or JNDI lookup, or when the client invokes a create<METHOD> method on the session bean's home interface. This causes the container to invoke newInstance on the session bean class to create a new session bean instance.

That to say,when a client obtains a reference to a stateful session bean instance through dependency injection or JNDI lookup,A session bean instance's life starts.
But what is that means:"when the client invokes a create<METHOD> method on the session bean's home interface".For ejb3,there has not a home interface.
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131

package com.jfd.clients;

import com.jfd.test.TestRemote;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class Client {
public static void main(String [] args) {
try {
Context jndiContext = getInitialContext( );
Object ref = jndiContext.lookup("TestBean/remote");//site1
TestRemote dao = (TestRemote)
PortableRemoteObject.narrow(ref,TestRemote.class);
dao.createPro();//site2
...
}}}

So,the stateful session bean is created at site1 or site2?
if the stateful session bean is created at site1,then i can say the description:
1,When a client invokes the first method on the stateful session bean reference, the bean's life cycle begins.

is incorrectly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When did the Stateful Sessionbean created?
 
Similar Threads
Amusing...
isIdentical
how to handle session in EJB
Problem in understanding session beans
Sessions