首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有状态bean的行为类似于无状态bean

有状态bean的行为类似于无状态bean
EN

Stack Overflow用户
提问于 2013-03-09 09:28:03
回答 2查看 152关注 0票数 0

我是EJB的新手,并且尝试为EJB有状态bean编写一个实现,但是当我尝试执行事务时,它返回的结果就像一个无状态bean

代码语言:javascript
复制
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);
    }
}

输出为:

代码语言:javascript
复制
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

EN

Stack Overflow用户

回答已采纳

发布于 2013-03-09 09:32:20

您忘记了递减/递增accountbalance。我想这就是你想要做的:

代码语言:javascript
复制
public int accountwithdraw(int amount)
{
    accountbalance = accountbalance-amount;
    return accountbalance;
}

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

ps -为什么在ejb定义中使用注释而不是用于查找(@EJB),有什么特殊的原因吗?将会更容易,也更便于移植。

票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15306125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档