1

I'm a beginner in the world of Java EE, I have a problem with EJBs, in fact I just learned Stateless EJB, I use it for my DAOs with the EntityManager injected in it and doing some CRUD, but I don't know anything else about them, I've read that one of their major advantages is that they're "transactional" or something like this, I did a lot of research but I still can't understand what this means exactly.

Can you please tell why some people hate EJBs, what is so heavy about them that they don't like? what is the "transaction" thing? should I use Stateless everytime as DAOs instead of POJOs?

Or if you have any useful links for a beginner, thank you in advance.

Dwix
  • 1,139
  • 3
  • 20
  • 45

1 Answers1

2

Simply, to emulate a transaction, a program may need to perform more than one step. A bank program, for example, might transfer money from a one account to another account. It will consist from the steps listed in the following pseudocode:

begin transaction
    checking first account
    take money
    send money
    compute and save result
    update history log
commit transaction

Either all five of these steps must complete, or none of them at all. Otherwise, data integrity is lost.

A transaction can end in only two ways:

- with a commit

- with a rollback.

When a transaction commits, the data modifications made by its statements are all saved for ever. If a statement within a transaction fails, the transaction rolls back, undoing the effects of all statements in the transaction.

And EJB's are doing all this difficult stuff for us. :)

I've been studying for a long time here http://www.coreservlets.com/ .

UPDATE 1

Some links related to EJB transactions

EJB's going deeper

Controlling transactions

Bean managed transactions

Daniel Perník
  • 5,464
  • 2
  • 38
  • 46
  • Thanks for your answer. So you mean we will never need to begin,commit or rollback any transaction manually with EJBs ? If there are some special cases when we must do it manually, can you provide an example or a link ? Thanks again. – Dwix Oct 13 '15 at 13:03
  • It depends on many things. But IMHO in 90% you can rely on EJB container himself. My answer was updated, so look on your links there. – Daniel Perník Oct 14 '15 at 10:35
  • I see, thanks a lot! – Dwix Oct 14 '15 at 13:38