Noticed that if I want to read some data and if I do not have a transaction context I will not be able to do so because
org.hibernate.HibernateException: No Session found for current thread
For reading data , is not required a transaction normally.
So in order for Spring to manage the session it needs to have a transaction even for read only operations like selects… ?
Is that not an overhead ?
PS.I do not want to open and close session manually…
Thanks a lot.
@Transactionaltells spring to open and close a session, in addition to instructing it to start and commit a transaction. This is not very straightforward, but that’s how it works. So if you don’t have@Transactional, no session gets opened. Here are your options:@Transactional(readOnly=true)– the purpose is to have a read-only transaction. I recommend that oneEntityManagerinjected with@PersistenceContext. It will open a new underlying session for each invocation. Not that good option. But you should consider usingEntityManagerwith a readOnly=true transaction