In my grails service I have a variable that stores some JSON obtained by fetching data from web service. I would like to make that variable unique for every user of the app. I am guessing i would need to move that variable to controller and use session[] to make it happen or is there an alternative?
If you want a separate instance of the variable per user of the application, you could either store it in the session, or make the service session-scoped (they are singletons by default) and store it in the service.
class MyService {
static scope = 'session'
}
Related
I am learning Spring, I learned about bean scopes - what are the real world use cases for each of them, I am not able to get any help. please help when to use Singleton, Prototype , Request and Session scopes in Spring.
Singleton: It returns a single bean instance per Spring IoC container.This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object. If no bean scope is specified in the configuration file, singleton is default. Real world example: connection to a database
Prototype: It returns a new bean instance each time it is requested. It does not store any cache version like singleton. Real world example: declare configured form elements (a textbox configured to validate names, e-mail addresses for example) and get "living" instances of them for every form being created
Request: It returns a single bean instance per HTTP request. Real world example: information that should only be valid on one page like the result of a search or the confirmation of an order. The bean will be valid until the page is reloaded.
Session: It returns a single bean instance per HTTP session (User level session). Real world example: to hold authentication information getting invalidated when the session is closed (by timeout or logout). You can store other user information that you don't want to reload with every request here as well.
GlobalSession: It returns a single bean instance per global HTTP session. It is only valid in the context of a web-aware Spring ApplicationContext (Application level session). It is similar to the Session scope and really only makes sense in the context of portlet-based web applications. The portlet specification defines the notion of a global Session that is shared among all of the various portlets that make up a single portlet web application. Beans defined at the global session scope are bound to the lifetime of the global portlet Session.
Usually Struts 2 action instances will get create on the request. I mean per every request new action instance will get create. But if I integrate with Spring then there will be only one action instance will get create (I am not sure correct me if I am wrong).
So in this case what is if I have instance variables in the action class?
First user here will set that instance with some instance variables and second user may set there something. How it will behave at this time?
More clarification: Instance variable means, in Struts 2, action forms won't be there so, your action itself work as a form to get the request parameters. First user enters something and second user enters something and both are setting to one instance action.
If your actions are managed by Struts container, then Struts is creating them in the default scope.
If your actions are managed by Spring container, then you need to define the scope of the action beans, because Spring by default uses singleton scope.
If you don't want to share your action beans between user's requests you should define the corresponding scope.
You can use prototype scope, which means a new instance is returned by the Spring each time Struts is being built an action instance.
I am asking this question because of the following reasons:
Usually struts 2 action instances will get create on the request. I mean per every request new action instance will get create. But if I integrate with spring then there will be only one action instance will get create (I am not sure correct me if i am wrong). So in this case what is if I have instance variables in the action class. First user he will set that instance with some instance variables and second user may set the something.
How it will behave at this time.
More clarification: Instance variable means, in struts 2, action forms wont be there so, your action itself work as a form to get the request parameters. First user enters something and second user enters something and both are setting to one instance action.
By default Spring will create a singleton instance of your action class. In that case, depending on how your action classes are written there might be such a danger.
But you can also specify that a bean be created prototypically (scope="prototype") so that a new instance of the class is created with each request.
First, If you integrated struts2 with spring, normally, the action instances are managed by spring container! this is supported by struts2 spring plugin: https://struts.apache.org/release/2.3.x/docs/spring-plugin.html
Second, as the plugin doc mentioned, by default, the action bean's scope is request, this is up to the struts2, but you can change yuor action scope to other type,i.e. session,application,etc.
I deployed a web application on the localhost GlassFish server. This application takes order information from user and stores it in a List type variable in a Stateless Session Bean.The list object is created in the constructor.
I open the order page and add multiple orders in it. When I open the show orders page in different tabs and different browsers, it displays all the order information bean correctly, as though the state is maintained in a Stateless Bean!
I think this behavior is wrong as each browser/tab should create different session with the server and new order information should be shown for each browser/tab. How can this behavior be explained?
Your use case is precisely what a stateful session bean is for, if you want your List object to be maintained across method invocations, and if you want each session to be assigned its own bean.
Stateless session beans are pooled and made available to any session. But your instance fields are not guaranteed to be cleared, so you can't depend on them being cleared. The behavior that you are seeing is not unexpected. Even if you were successful in creating separate sessions in multiple tabs, those sessions could very well have been (and apparently were) assigned the same session bean. That's because the associated method invocations occurred at different points in time. Now if the associated method invocations occurred simultaneously instead, then the platform would have assigned a different stateless bean to each invocation (session). In that case, you'd see different behavior.
See also;
conversational state of session beans
and
Stateless and Stateful Enterprise Java Beans
Never let what you can't do get in the way of what you can do.
Problem: Stateful Session Bean was not maintaining separate state per client. In the example I tried, I input orders from the JSP page, which were stored in a List in a Stateful Session Bean. When I called the same URL from a different browser (i.e. a different session), the list of orders input in the previous session were visible. The same EJB was getting referenced in both sessions. (Verified by sysouts)
It's like saying, the shopping cart of some other user was directly visible to me as if they were my orders!!
Solution: Used an HttpSessionListener and got the dependency of the Stateful EJB through JNDI, in sessionCreated(HttpSessionEvent se) method. Next, added the stateful EJB in an HttpSession and accessed the EJB through session in servlet.
Suggestions for using JNDI, instead of DI, for Stateful Session Bean and Adding EJB to HttpSession are given in the answer above. Don't know if it is the proper way to go, but it works!!
In symfony2 every user created controller extends Controller Class as shown below,
class MyController extends Controller {
thus functions related to session handling are available with $this object, But controllers in Vendor and Core don't extend Controller class thus don't provide access to session related functions. So is there any way to use these functions without extending Controller class.
Presently I am using $_SESSION[], for setting and getting session variables.
Is there any way other than above.
Symfony2 provides a service for sessions, this is what you're trying to retrieve. All services in symfony2 are retrieved using the service container, which is what you're referring to with
$this->get('session');
To properly make use of the service container in your own controllers you can either...
Configure your controllers as services (see: here)
Extend the base Controller class provided by the Symfony2 stack (making the get() method available to your child Controller)
The first option is the correct way to go, you have full control over what services are then injected into your respective controllers (see service container documentation)