Search code examples
jakarta-eetransactionsejbtransactionalstateless

Java EE, what does Transactions mean in EJBs?


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.


Solution

  • 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