문제

I'm new to EJB and try to write a implementation for EJB stateful bean but when I try to do the transaction its returning like a stateless 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);
    }
}

The output is:

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

I could not understand. It should return 10 9 then 8..but returning 10 9 9..please help

도움이 되었습니까?

해결책

You're forgetting to decrement/increment accountbalance. I think this is what you intended to do:

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

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

ps - any particular reason why you're using an annotation in the ejb definition but not for lookup (@EJB) ? Would be both easier and more portable IMO.

다른 팁

On top of fvu answer, you should not make accountbalance static, or this value will be shared by all instances of the bean.

Just declare it like this:

int accountbalance;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top