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.
@Transactional
tells 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 oneEntityManager
injected with @PersistenceContext
. It will open a new underlying session for each invocation. Not that good option. But you should consider using EntityManager
with a readOnly=true transactionIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With