What is the scope of transactions in a typical EJB3 / JPA / JSF?

Suppose you have a web application with an EJB3 / JPA and JSF stack. AFAIK, you can design your screens using different managed beans, for example, let's say HeaderBean and ListingBean . Since there is no OSIF pattern in EJB3 AFAIK, how many different transactions are performed in the following pseudo-code:

 @ManagedBean class HeaderBean { @PreConstruct load(){ // enters transaction boundary, probably will create a new tx headerInfo = ejb.loadFromDb(); } } @ManagedBean class ListingBean{ @PreConstruct() list(){ // enters transaction boundary, probably will NOT join the headerBean tx List<Data> listing = eao.loadFromDb(0, 20); } } 

AFAIK, when you leave the EJB level, all transactions are completed; so if I call two different SLSBs from the presentation layer, will it work in two different transactions (and possibly abort my ACID expectations)?


Clarification . I know about EJB3 transactional behavior like required, never, requires_new and so on. My question is how View-First (like JSF) promotes a design where screen data can potentially span multiple transactions and therefore potentially inaccurate.

I prefer longer transactions, but correct data, than short transactions, but incorrect data. I was wondering if new frameworks, such as jBoss Seam, somehow contribute to this or offer an alternative design (for example, the Open-Session-In-View template).

+4
source share
1 answer

There are options for controlling the transactional behavior of an EJB. Usually they have the “Transaction required” parameter, therefore, if a bean call is called with a transaction, then the bean is included in the already established transaction, otherwise the transaction will be started and completed when the bean is returned.

In your code, when entering the EJB, the transaction does not occur, since you are talking about the return from the EJB, any transaction has been resolved.

While this seems potentially problematic in that you potentially get conflicting views about data, I think this behavior is desirable. We want the time spent in the transaction to be short - otherwise the database locks persist for long periods of time, and therefore concurrency suffers.

The EJB layer should be seen as the provision of atomic services, and is also designed and used accordingly. I don't know if I am reading your code correctly, but having separate access methods for the header and body may not be the best design. If you need consistency between header and body retrieval, all data in one call can be preferable and actually more efficiently executed in a single interaction with the database.

- added --- You found out in your question that you are really concerned about the consistency between the different parts of the screen that will use separate transactions when encoding using simple JSF methods.

In my opinion, the default JSF approach is consistent when such inconsistencies are either unlikely or unavoidable. Examples: 1). It is unlikely: to request historical data, the total number of yesterday's transactions and a list of yesterday's transactions. In a system where history cannot change, such individual requests will be consistent. 2). Inevitability: a summary comes from one system, details from another system, there is no transactional coordination between the two systems, we cannot ensure consistency. We just need to provide the user with indications that these two points of view may be slightly inoperative.

If you really need consistency, use a different approach, get all the data in one request and save it (say, in a session or request), then use it in two views - the views should not retrieve their own data if you like it about such things .

I think you will find that trying to use transactions to ensure consistency can lead to significant complexity as well as reduced throughput. The problem with coordinating transactions between views is that there is no simple "owner", if you recompile the page, you will need to change the logic

+2
source

Source: https://habr.com/ru/post/1387759/


All Articles