c12
c12

Reputation: 9827

JPA EntityManager Session Lifecycle Close Event

When using the Hibernate implementation of JPA and managing those resources using a Spring datasource, when does the EntityManager session get closed? For example I have the basic pattern below that has the Spring service layer calling a Spring DAO layer (@repository). The DAO repository autowires the PersistentContext's EntityManager and performs the DB operation. My question is when is the EntityManager session closed, after the getData Service method finishes? I'm asking this question as I'm trying to see how long the hibernate L1 cache will be around and since its directly tied to the EntityManager session...

@Service
public class TestService{
@Autowired
private dataDAO;
@Transaction
public List getData(Long id){
    return dataDAO.getDate(id);
}
}

Upvotes: 1

Views: 1230

Answers (1)

Jean-Philippe Bond
Jean-Philippe Bond

Reputation: 10679

By default, spring is releasing the session at the end of the transaction. In this case, the session will be release at the end of the getData method. The first level cache (L1) is bound to the actual transaction and is cleared as soon as the transaction is terminated.

Upvotes: 3

Related Questions