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

前端 未结 4 1784
终归单人心
终归单人心 2020-12-23 11:30

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

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

My problem is

4条回答
  •  春和景丽
    2020-12-23 11:46

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

提交回复
热议问题