55

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 !

4 Answers4

139

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?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
asawyer
  • 17,642
  • 8
  • 59
  • 87
  • 1
    @David Thanks, it's also how I describe a web session request/response relationship to people. – asawyer Mar 23 '11 at 17:42
  • 1
    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? – rghome Jan 23 '15 at 09:11
  • @asawyer: what will constitute an operation then? How is an operation different from a transaction? – Farhan stands with Palestine Feb 12 '15 at 17:53
  • one way this analogy breaks: in hibernate, if you "commit" an individual transaction, calling `session.close()` will throw an exception... – Don Cheadle Mar 30 '15 at 19:34
  • 2
    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. – HopeKing May 04 '18 at 15:12
10
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.)

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 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 ? –  Mar 23 '11 at 17:55
7

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.)

Jeremy
  • 22,188
  • 4
  • 68
  • 81
3

In Hibernate, the transaction management is quite standard, just remember any exceptions thrown by Hibernate are FATAL, you have to roll back the transaction and close the current session immediately.

Here’s a Hibernate transaction template :

    Session session = null;
    Transaction tx = null;

    try{
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();
        tx.setTimeout(5);

        //doSomething(session);

        tx.commit();


    }catch(RuntimeException e){
        try{
            tx.rollback();
        }catch(RuntimeException rbe){
            log.error("Couldn’t roll back transaction", rbe);
        }
        throw e;
    }finally{
        if(session!=null){
            session.close();
        }
    }