Where to open sessions in the Spring / Hibernate stack?

I am trying to find a good design for a Spring / Hibernate application. When creating such an application, it looks as if there are several important solutions.

The first important decision seems to be to place the session / transaction boundary. It seems that I have 3 main options: as a filter before the controllers are even called, right under the controllers at the service call level and in a packed way below the business level in the repository calls.

It seems to me that the right challenge is the middle way, but I'm not sure. I don’t want my transactions to open too long, but at the same time I don’t want to constantly worry about individual objects and lazy loading in business logic. However, there are some disadvantages. For example, business logic makes it difficult to remotely call without raising transactions for a few seconds. I wonder if there is a better way?

+5
source share
2 answers

Solution 1: open a session in view mode

  • Benefits:
    • you will not encounter lazy initialization exceptions, so you can use your model objects in your view without thinking twice
    • , , ( , ).
  • :
    • (, ), , ( " ", , ).

2: -

  • :
    • ( , , , , tx, )
    • ( db- )
  • :
    • , . lazyInitialization - , , DTO, ( ): , , DTO, , . -, , .

3: dao

dao -, dao. "autocommit" , .


, , , "sql footprint" ( org.hibernate.SQL DEBUG) sql , .

, , , ( , , )

+1

, . , , , . DAO Service, , , .

, , , , , , . , .

, / .

+1

All Articles