Spring/Hibernate @Transactional - Session closing before it should?

I don't want to allow for transactions within the View or Controller, I want the intended behaviour inside the @Transactional of Service layer. – Corey Mar 30 at 5:44 Setting up OpenSessionInViewFilter does not mean that transactions are controlled by View or Controller. This just means that hibernate session is kept alive until the view is processed.

This allows you to fetch entities which are lazy loaded. OpenSessionInViewFilter does not have any side effect on transactions. – Nilesh Mar 30 at 6:11 Unfortunately, this will cause me further pain down the road.

I will start using JacksonMapping to convert objects to JSON. JacksonMapping eagerly grabs all fields within an object, which means if I'm not VERY explicit, I could accidentally load my whole database into memory. I do like these lazy exceptions to keep me in check, but this particular error I feel isn't supposed to be happening.

– Corey Mar 30 at 18:52 The other (probably a bit clumsy) option is to manually bind the session to the thread! This could be done through an AOP advice so that it makes the solution non-intrusive. Works?

– Nilesh Mar 300 at 16:04.

Associations are lazy-fetched by default. So once you exit the scope of the @Transactional annotation, you can't get the collection. While OpenSessionInViewFilter is an option, I agree with you that that's not a great approach.In general you want to exercise tight control over transactional boundaries.

If you know you always want to do a fetch join, just include that in your HQL or set fetch = FetchType. EAGER in your @ManyToMany. If you want to include tags in some cases but not in others, you can do initialize(project.getTags()) for the case where you want the tags, which will do a separate query for the tags, but it's not an n+1.

Alternatively you can have two separate HQL queries (one that includes the fetch join and one that doesn't). Only thing is that if you have multiple collections you can't do fetch joins on more than one.So initialize() is pretty useful in such contexts.

Yes, but my manager has the @Transactional, and the lazy fetch error is occurring inside the manager. – Corey Mar 30 at 18:32 Also, initialize() is throwing the same lazy error. – Corey Apr 1 at 23:29.

What is the transaction manager in use - it should be bernateTransactionManager or JTATransactionManager.

Org.springframework.orm. BernateTransactionManager – Corey Mar 30 at 18:31.

Annotate your ProjectManager class as @Service and not @Component.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions