This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Who can access a session bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Who can access a session bean?" Watch "Who can access a session bean?" New topic
Author

Who can access a session bean?

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
We have a trading game in which users will be trading stocks with each other. They will be using full Java applications as clients (i.e. not web clients), and they will connect to an EJB server.
It makes sense to represent the portfolios as stateful session beans. Now here's where we start to get questions...
To make a trade, we transfer money from portfolio A to portfolio B, and at the same time, transfer stocks from B to A. Clearly this needs to be as a transaction. For that reason, and others, we were thinking of having an object devoted to making trades, e.g. a TradeManager. A TradeManager would take two portfolios, and the terms of the trade an execute it.
How do we do this?
We were thinking of having the TradeManager as a stateless session bean, that gets a reference to two portfolios (stateful session beans). Can we do this? I've never seen anything to suggest that we can't. Do we pass a Java reference to portfolios in the call to the TradeManager, or can we simply pass some unique identifier (e.g. portfolio owner/username) to the TradeManager and have it perform some sort of lookup to get the reference to the portfolio beans?
The reason we ask is because often stateful session beans are used to hold private data shared between the client and server. Effectively, we are asking client A to be able to gain access to client B's session bean. Can this be done? If it can be done, is this the recommended way to do it?
Why aren't we using entity beans? Well, this information is never written to the database, and I was under the impression entity beans were really only designed for persistent information. Keep in mind that the portfolio state may be changing every few seconds, or even faster. Should we be using entity beans for this?
For the record, the clients will not be modifying their portfolios directly--ever! Clients will be receiving JMS updates to their accounts/portfolios, but, at best, only see information about them (e.g. their value and holdings). The actual portfolio object only resides on the server; on the client, the information is read only.
To sum it all up: we're trying to get a stateless session bean modify two stateful session beans. Is this the right thing to do? Is there an easier way?
Help would be greatly appreciated. Thanks.
--Mark
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3879
Mark,
There is no lookup for Stateful session beans. You'd have to store the bean handles externally, and that'd be a pain. What's more, concurrent access to the same stateful bean is prohibited, making it difficult to avoid some failure cases.
So, I would NOT do this with stateful session beans. And actually, if you use BMP Entity beans with Option A (Always in Memory) caching you should be able to make this work without ever having to have a database at all. (e.g. you can leave your implementation of ejbLoad() and ejbStore() blank -- just don't do anything). Likewise ejbCreate() will just sets the variables passed in and ejbFindByPrimaryKey() will simply return a copy of the Primary key handed in.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Thank you for a very helpful and promt reply!
--Mark
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I agree with Kyle. I don't see how you could do this with session beans since a session is private.
You seem to want to people to participate in the "same" transaction? You want real-time transactions? That is interesting.
You definitely need BMP Entity beans for the portfolios.
What I would do is have each client "authorize" a trade. Then have some other trade stateless session bean that looks for doubly authorized trades every 30 seconds or so. And at that time processes them.
or you could just process the transaction if both sides agree. Like you could have client A setup a Xaction. Send it to client B. (an entity bean would store transaction requests and the participants involved, cancellable by either participant.) Then when client B looks up his pending transaction requests, he can say to complete it, and his session would complete the Xaction.
Its going to ultimately be processes by a client A session, a client B session, or an automatically running session. But their is no combination session.
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Originally posted by CL Gilbert:
You seem to want to people to participate in the "same" transaction? You want real-time transactions? That is interesting.
...
What I would do is have each client "authorize" a trade. Then have some other trade stateless session bean that looks for doubly authorized trades every 30 seconds or so. And at that time processes them.

Why is this interesting? This seems like a very common type of transaction. Is this hard to do?
Here's some more info about what we're trying to do. This is a trading system.
People will be placing limit orders. A limit order is a standing ordering saying "I'll buy 500 shares at $43 per share." The price may currently be at $45, but this way if the market ever moves down and people do start offering at $43 per share, this standing order will be executed.
People will also be placing market orders. A market order is "Give me 300 shares at the current market price." The way market orders are implamented is that when a market order is placed, it is executed against the best limit order (highest or lowest price for selling or buying, respectively).
What this means is that client B will have a standing limit order. Client A will place a market order which gets crossed with client B's limit order. A and B both have portfolios on the server (and BMP entity beans as per Kyle's recommendation), and some code, upon receiving the market order, needs to modify those accounts appropriately. Clearly this needs to be transactional, to avoid data inconsistency. However, client B has this as a standaing order and shouldn't have to be actively involved. B will only be notified if the trade happens (e.g. "Congratuations, you just bought 500 shares at $43 per share, as specified in limit order #748502.")
It seems as though we can simply have a stateless session bean that, upon recieving the market order, can look up both protfolios and execute the transactional trade, right?

--Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Who can access a session bean?
 
Similar Threads
Stateless Session Bean/Entity Bean Creation...
Stateful session beans or Stateless combined with entity beans
Asynchronous communication among clients?
New to EJB.. Session Bean
Question on exercise pg 108 HFE