go4it

just do it

有状态和无状态EJB的比较

image

有状态和无状态的会话bean都在客户端产生不同的代理实例

image

不同的是在服务器端,有状态的每次lookup都是新的独立的bean,而无状态的是单例bean。

public class StatelessEjbClient {

	/**
	 * @param args
	 * @throws NamingException 
	 */
	public static void main(String[] args) throws NamingException {
		  Hashtable env=new Hashtable();   
		  env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");   
		  env.put(Context.PROVIDER_URL,"localhost");   
		  env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");   
		  InitialContext cxt=new InitialContext(env);

		  //第一次会话
		  StatelessEjb se1=(StatelessEjb)cxt.lookup("StatelessEjbBean/remote");
		  System.out.println("刚开始"+se1.getCount());
		  se1.compute(1);
		  System.out.println(se1.getCount());
		  se1.compute(1);
		  System.out.println(se1.getCount());
		  se1.compute(1);
		 System.out.println(se1.getCount());
		  se1.compute(1);
		  System.out.println(se1.getCount());
		 
		  //第二次会话
		  StatelessEjb se2=(StatelessEjb)cxt.lookup("StatelessEjbBean/remote");
		  System.out.println("刚开始"+se2.getCount());
		  se2.compute(1);
		  System.out.println(se2.getCount());
		  se2.compute(1);
		  System.out.println(se2.getCount());
		  se2.compute(1);
		 System.out.println(se2.getCount());
		  se2.compute(1);
		  System.out.println(se2.getCount());
		  se2.compute(1);
		  System.out.println(se2.getCount());
		  
		  System.out.println("ejb1==ejb2"+(se1==se2));
		 
	}

}

第一次运行结果

刚开始0
1
2
3
4
刚开始4
5
6
7
8
9
ejb1==ejb2false

而第二次却不是递增了:

刚开始9
10
11
12
13
刚开始13
14
14
14
14
14
ejb1==ejb2false

什么问题???

posted on 2008-12-07 22:27  cxccbv  阅读(394)  评论(0)    收藏  举报

导航