What is the difference between a session and a transaction in JPA 2.0?

34,050

Solution 1

You go to the bank to deposit 2 checks, and withdraw a small sum.

So you stand in line until a teller opens.

You make your first deposit.
Then your second.
Then you make your withdrawal.

Now you're done, you leave the teller line.

Getting to the teller is like creating your session, now you're in the bank, ready to work.

Each deposit and withdrawal are their own contained set of work, these are your transactions.

When you're done with your work and leave, you're closing or abandoning your session.


So, in essence, a session contains your transactions, after all you can't make a bank deposit if you never go to the bank right?

Solution 2

em = SessionFactory.startSession();

In JPA, there is no Session and no SessionFactory. SessionFactory is a hibernate-specific interface that you shouldn't use if you use JPA (use either Hibernate's own API or use Hibernate as JPA Provider, but not both.)

Solution 3

A session is what you use to interact with the database.

A transaction is used to specify boundaries for the session to operate within.

Essentially, transactions prevent the database from being corrupted by only allowing a session to interact with it at one time. (It's a bit more complicated then that, as you can have many transactions reading from the database, but only one transaction that's writing.)

Share:
34,050
Admin
Author by

Admin

Updated on December 26, 2020

Comments

  • Admin
    Admin over 3 years

    I just begin my JPA 2.0 studies, and I have this piece of code:

    em = SessionFactory.startSession();
    tx = em.getTransaction();
    

    My problem is: I'm not sure if I completly understand the difference between the use of a session and the use of a transaction. In a few lines, can anyone please tell me the biggest differences between them ? Thanks !

  • asawyer
    asawyer about 13 years
    @David Thanks, it's also how I describe a web session request/response relationship to people.
  • Admin
    Admin about 13 years
    I'm using just what you said, hibernate as JPA provider ! So, what you are saying is that I should have written the title as "...in hibernate" instead of "...in JPA 2.0", right ?
  • rghome
    rghome over 9 years
    For completeness, can you have multiple sessions within a single transaction? I need to pay a bill at a restaurant - I don't have enough cash, so I pay half, go out to the cash machine and get some more money, pay the rest and then the transaction is complete. Two sessions, one transaction. Is this possible?
  • Farhan Shirgill Ansari
    Farhan Shirgill Ansari about 9 years
    @asawyer: what will constitute an operation then? How is an operation different from a transaction?
  • Don Cheadle
    Don Cheadle about 9 years
    one way this analogy breaks: in hibernate, if you "commit" an individual transaction, calling session.close() will throw an exception...
  • HopeKing
    HopeKing about 6 years
    Creating a post, up-voting, commenting etc. by a logged in user on stackoverflow all happen in one Session. Each of individual actions is a transaction. Another analogy.