0

我新的EJB,并尝试写EJB状态bean一个实现,但是当我尝试做交易的返回象一个无豆状态bean行为就像一个无状态的bean

package beanpackage; 

import javax.ejb.Stateful; 
//import javax.ejb.Stateless; 

/** 
* Session Bean implementation class bankbean 
*/ 
@Stateful 
public class bankbean implements bankbeanRemote, bankbeanLocal { 
    /** 
    * Default constructor. 
    */ 
    static int accountbalance; 
    public bankbean() { 
     accountbalance=10; 
    } 
    public int accountbalancecheck() 
    { 
     return accountbalance; 
    } 
    public int accountwithdraw(int amount) 
    { 
    return (accountbalance-amount); 
    } 
    public int accountdeposit(int amount) 
    { 
     return (accountbalance+amount); 
    } 
} 




import java.util.Properties; 

import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 

import beanpackage.bankbeanRemote; 


public class appclient { 
public static void main(String args[]) throws NamingException 
    { 
     Context c = appclient.getIntitialContext(); 
     bankbeanRemote bbr = (bankbeanRemote)c.lookup("bankbean/remote"); 
     int s = bbr.accountbalancecheck(); 
     System.out.print(s+" this is first ejb output"); 
     s=bbr.accountwithdraw(1); 
     System.out.print(s+" this is first ejb output"); 
     s=bbr.accountwithdraw(1); 
     System.out.print(s+" this is first ejb output"); 
    } 
public static Context getIntitialContext() throws NamingException 
    { 
     Properties prop = new Properties(); 
     prop.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); 
     prop.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); 
     prop.setProperty("java.naming.provider.url", "127.0.0.1:1099"); 
     return new InitialContext(prop); 
    } 
} 

输出是:

10 this is first ejb output 
9 this is first ejb output 
9 this is first ejb output 

我听不懂。它应该返回109,然后返回8..but 10 9 9..please帮助

回答

3

你忘了递减/递增accountbalance。我想这是你打算做:

public int accountwithdraw(int amount) 
{ 
    accountbalance = accountbalance-amount; 
    return accountbalance; 
} 

public int accountdeposit(int amount) 
{ 
    accountbalance = accountbalance-amount; 
    return accountbalance; 
} 

PS - 什么特别的原因,为什么你在EJB的定义,但不是查找(@EJB)使用注释?将是既容易和更便携IMO。

1

在FVU答案的顶部,你不应该做accountbalance静态的,或者这个值将由bean的所有实例共享。

只是声明它是这样的:

int accountbalance;