Should I care about Open Session in View when using Grails? - model-view-controller

From my experience of using spring MVC in conjunction with hibernate I know about lazy exception problem occured when addressing to lazy-fetched collection when rendering view.
It pure spring+hibernate world it fixes by introducing OpenSessionInViewInterceptor or OpenSessionInViewFilter thus enabling one hibernate session per request.
So the question is: should I care about this problem in grails or such one-session-per-request behaviour is enabled by default.
If this isn't grails defaults please provide some code to implement this behaviour.
Thanks.

Grails registers a customized subclass of OpenSessionInViewInterceptor (it adds WebFlow awareness). So anything done in the context of a web request will have an open session and lazy-loaded references and collections will resolve.

Related

Integrate JSF2 and Spring3 (with AOP)

I am using Spring and JSF2 a lot and was wondering what the best way to integrate them is? Now i understand there are basically two ways to do this, but i have some problems with both:
a) Use normal #ManagedBean, and inject Spring-Services into that beans using #ManagedProperty: The problem is that i can't use Spring-AOP inside a #ManagedBean obviously, because it is not Spring-managed. I usually use an arround-aspect on every method annotated with my custom annotation #DatabaseOperation. Another example would be #Secured from Spring-Security and so on. I use a lot of AOP in my project and not beeing able to use them on "the top level" is really limiting for me.
b) Use SpringBeanFacesELResolver to make everything managed by Spring. On the pro side is that AOP works like a charm, but the cons are big too:
No ViewScope. I am not sure if i can trust custom view scope implementations like this https://github.com/michail-nikolaev/primefaces-spring-scopes on productive systems?
Serialization is not possible. It's already pretty complicated, but once i use AOP i can't get it to work because org.springframework.aop.aspectj.AspectJPointcutAdvisor is not Serializable
So my question is: How do you overcome this issues? How do YOU integrate JSF2 and Spring3.x? I have been using possibility b) mostly, but on my next project i need session replication..
Any further suggestions?
The main integration pain point of the two frameworks is that JSF is usually used in a stateful way. to make the integration the most seamless, you would have to let Spring handle the statefulness and page navigations aspects himself instead of JSF, as well as bean creation and scoping.
The second important integration point is at the level of the view expression language. We want to be able to access spring beans while building the view, which means the managed bean layer is no longer needed.
The best integration available for the two frameworks is provided by introducing Spring webflow, see here for further details. With SWF, JSF no longer manages beans itself, this is done by Spring. JSF does not manage page navigation anymore, this handled in the SWF flow definition.
Also the statefulness is handled by SWF. The flow definition XML file replaces large parts of the faces-config.xml for view navigation, transition actions, bean definition, etc.
Using the SWF JSF integration means that your faces-config.xml is mostly empty. Expression language accessing directly spring beans can be used while building the view, and a view scope is available.
If you want to keep a page isolated from the rest of the application, youcan create a flow with a single view state and self-transitions. An advantage of SWF is that it prevents duplicate form submissions via a POST-REDIRECT-GET mechanism that works transparently out of the box.

Spring context refreshing makes Hibernate session throw this exception: No Session found for current thread

I am developing a web application that has multiple spring contexts. It has a main context that holds business logic, hibernate session and application's core needs, and other contexts are for spring mvc binding. Normally application works fine and everything, but when i refresh main context and try to reach hibernate session from other contexts, hibernate session throws this exception:
org.hibernate.HibernateException: No Session found for current thread at
org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at
org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980)...
But funny thing is application can do startup initiation which includes selecting lots of data from db.
Is there a way that i can refresh spring context safe and sound?
P.S: I can get other spring context objects with no problem. And application works fine with multiple contexts until i refresh main context. And i tried refreshing the mvc context, from which i call hibernate session, after main context. Still the same exception in that mvc context.
The reason you are getting that error is that you are whacking the Hibernate sessions out from underneath the threads. The sessions, from what I remember, associated with the Thread that created them. This is why you can't pass a proxied Hibernate entity to frameworks like BlazeDS. As soon as the proxied entity leaves the thread, the session is no long associated with it and you loose the ability to go after the items that are being proxied.
One solution might be to not use Proxies, and eagerly-load all relationships. This would be undermining one of the main benefits of using an ORM, however. Or, if you could somehow re-start all the threads in questions, making new threads that are associated with the new Hibernate session(s), that might work too.

Best practise - Struts2 / Spring Integrated application startup process - for adding default lookup values in application context

I am a Struts2 and Spring newbie and looking for some insight. When we load a web application we would typically want to cache some default look up data. e.g. if we wanted to store states or other data that does not change frequently and add it to the application context where we can access it across the application. What is the best way to realize this in a Struts2 application integrated with Spring? I read a bit about annotating with #PostConstruct which means I define my own class/method that would get a handle to the context by calling ServletActionContext.getServletContext() and then use setAttribute to add something. Is that a good way of going about things or is there a better option? Or would simply implementing a ServletContextListener be ideal?
Thanks for any input.
If you want to use the ServletContext, use Spring's ServletContextAware interface and then use an #PostConstruct or afterPropertiesSet method to add items to the servlet context.
This is simpler to use than the listener and integrates seamlessly with Spring, giving you access to properties files declared in Spring and any other beans.

Anybody configured Coldfusion 8 with Spring-Hibernate?

I want to run an application[MVC] with models in Spring framework and Data Access layer in Hibernate, View and Controllers in Coldfusion.
Has anybody configured such and application ?
There is Palladium, which allows you to use ColdFusion as the view layer, but everything else is controlled by Spring MVC.
Mike Nimer has a blog post about setting Spring up in ColdFusion.
I wrote a proof of concept with a Spring bean factory wired into FW/1, but it never went beyond the prototyping phase. I.e. it works, but has not been tested as production worthy.

Stateless Spring MVC

Im currently reading Spring in Action 3rd edition, and have been experimenting with Spring MVC.
Everything works well, until i tried to 'port' my example webapp to a stateless webapp.
To determine whether a session object is created, i placed a debugging servlet filter on /* URL mapping, which just print out the req.getSession(false), and continue the chain.
I tried changing all of my controllers' scope to request out naiveness, but of course, session is still created on a page/controller that binds the model to the form.
I wonder how to achieve stateless Spring MVC ? Im out of luck for tutorials on this matter so far.
Make sure to have all the JSPs use
<%#page session="false" %>
else a session will be created as soon as a JSP is executed.
Accidental session creation is one of the most common sources of invalid bug reports in Spring Security (hence the FAQ you linked to in your comment above).
Spring Security's debugging filter can be useful if you're having issues with session creation. It will automatically log a message when a session is created, with a stacktrace inidicating where it happened. It also provides other useful information with more human-readable output that standard log entries, so is useful in a development environment. If you're using Spring Security's namespace support, you just need to add the element
<debug />
to your configuration.

Resources