Hibernate UnknownServiceException :Unknown service requested as transaction completed

后端 未结 4 1630
滥情空心
滥情空心 2021-02-13 12:40

I have a simple class which starts 3 threads and saves a new object in each thread. But I am getting exception which i cannot understand. Can anyone help me understand why the e

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 12:49

    A Session is actually a Unit of Work which should be bound to the current executing Thread. A unit of work group multiple DML operations inside a single transaction which can succeed only if all operations succeed. So a session is atomic, and atomicity implies a single operating thread.

    The Session is also the 1st Level Cache, so in the current Session you will always get the same Entity object reference, no matter how many times you call session.get() or session.load(). If a Session were thread-safe than during flush time you might execute other transaction intermediary changes. So a Session must be isolated from other executing Session, and isolation implies a single operating thread.

    So, a Session is not meant to be thread-safe to preserve the atomicity and isolation requirements.

提交回复
热议问题