我是 EJB 新手并尝试为 EJB 有状态 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
我无法理解。它应该返回 10 9 然后 8..但返回 10 9 9..请帮助