我是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..but,返回10 9 9..please help
发布于 2013-03-09 09:32:20
您忘记了递减/递增accountbalance。我想这就是你想要做的:
public int accountwithdraw(int amount)
{
accountbalance = accountbalance-amount;
return accountbalance;
}
public int accountdeposit(int amount)
{
accountbalance = accountbalance-amount;
return accountbalance;
}ps -为什么在ejb定义中使用注释而不是用于查找(@EJB),有什么特殊的原因吗?将会更容易,也更便于移植。
https://stackoverflow.com/questions/15306125
复制相似问题