Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When did the Stateful Sessionbean created?

 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kenneth Lomvey
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chaminda Amarasinghe
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic