Re: Session object null
On 5/18/10 6:55 PM, Ken wrote: The problem has been resolved with what you said... Glad to hear it. However I'll fill you as I am new and looking for best practise. I can offer my advice, but I don't have the hubris to claim that's necessarily what others would agree to as best practice. :-) Most requests are DB queries, via Hibernate. There is a Hibernate configuration file that needs to be loaded that specifies connection parameters and the tables to include. In our system there are many databases, one for each company (some are holding companies)... So there are a number of configuration files, depending on the log-on credentials a different database is loaded (some users may be able to select which one they are working with)... The processing is more less consistent, the databases hold different accounting information for the particular company. Anyways this configuration object and/or the resulting SessionFactory (Hibernate connection sessions not Struts2) aught to be in session scope. Currently these objects are only stored in memory and obtained with new in the request cycle. So to think about it I'm currently creating a hibernate SessionFactory EVERY request from credentials supplied by the log in form and then throwing it away... well I'm going to change that. So it isn't an issue... but knowing what I'm doing any advice on how you'd do it? Just to confirm--you have a single database that contains the login info, and based on info there other data will come from different databases? I almost went a similar route on a project but decided the headaches weren't worth it, so I've got entirely separate deployments for each end application (and that's OK for me -- sounds like your requirements require another solution). This sounds like it would best fit in an interceptor, probably before "params" (or if you use "params prepare params" between the first params and the prepare) so that by the time you get to prepare (and at least the last params) you've already got a connection to the correct DB so you can store incoming fields directly in the model object. Depending on the tradeoff between the cost to construct this SessionFactory and the size/resources required to store it in the session between requests you can either have this interceptor create it anew each request or cache it in the session, only creating it if necessary. There is nothing fancy, just the struts mapping file and actions with the default stack. And for view jsp's templated with tiles. I've only just started but I have to say everything has been far easier than expected... So I'm really enjoying Struts2. Glad to hear it! Do you use Spring? I've heard great things about it. For dependency injection, for example, it is quite nice. I used "appfuse" to jump-start most struts2 apps I've built, so the configuration headaches are minimized...but you've already figured out the hard parts of your particular DB situation, so that shouldn't be too bad... -Dale - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Session object null
Thank you, The problem has been resolved with what you said... However I'll fill you as I am new and looking for best practise. On Tue, 2010-05-18 at 15:25 -0400, Dale Newfield wrote: > On 5/18/10 2:17 PM, Ken wrote: > > Let me see if I understand this correctly. Since the action object has > > not been created yet, and this is in a different thread... I can not get > > the correct context... Although the session objects _is_ someplace... > > It will also not work in the constructor even if the class is > > SessionAware because the object must exist before setSession(Map m) > > could be called... > > You've not provided much context regarding the life cycle of the object > you're trying to create, where it is created/used/stored, etc. So it's > tough to answer specifically. There are numerous scopes in which > objects of varying lifespans can be stored. It sounds like your object > is a per-session object, even though you're going through a request to > create it? Are there reasons it needs external resources (DB Most requests are DB queries, via Hibernate. There is a Hibernate configuration file that needs to be loaded that specifies connection parameters and the tables to include. In our system there are many databases, one for each company (some are holding companies)... So there are a number of configuration files, depending on the log-on credentials a different database is loaded (some users may be able to select which one they are working with)... The processing is more less consistent, the databases hold different accounting information for the particular company. Anyways this configuration object and/or the resulting SessionFactory (Hibernate connection sessions not Struts2) aught to be in session scope. Currently these objects are only stored in memory and obtained with new in the request cycle. So to think about it I'm currently creating a hibernate SessionFactory EVERY request from credentials supplied by the log in form and then throwing it away... well I'm going to change that. So it isn't an issue... but knowing what I'm doing any advice on how you'd do it? There is nothing fancy, just the struts mapping file and actions with the default stack. And for view jsp's templated with tiles. I've only just started but I have to say everything has been far easier than expected... So I'm really enjoying Struts2. Do you use Spring? I've heard great things about it. > connection, etc.) that would mean it should be created and destroyed on > a per-request basis (most reliable, don't have to worry about > storage/serialization/clusters/etc.)? Are there reasons it needs to > live longer than the request? > > > So... > > I should really be treating execute() as my constructor for now... until > > I better understand when the session variable becomes available. > > Struts2 needs to see who I am before it knows my session right? So it > > isn't going to provide it to me until it has done a certain amount of > > set up? > > There are hooks for lots of different events, from application startup > to session creation, to request processing, etc. The important part is > to figure out your requirements, then we can suggest where to do the > work/store the object. > > -Dale
Re: Session object null
On 5/18/10 2:17 PM, Ken wrote: Let me see if I understand this correctly. Since the action object has not been created yet, and this is in a different thread... I can not get the correct context... Although the session objects _is_ someplace... It will also not work in the constructor even if the class is SessionAware because the object must exist before setSession(Map m) could be called... You've not provided much context regarding the life cycle of the object you're trying to create, where it is created/used/stored, etc. So it's tough to answer specifically. There are numerous scopes in which objects of varying lifespans can be stored. It sounds like your object is a per-session object, even though you're going through a request to create it? Are there reasons it needs external resources (DB connection, etc.) that would mean it should be created and destroyed on a per-request basis (most reliable, don't have to worry about storage/serialization/clusters/etc.)? Are there reasons it needs to live longer than the request? So... I should really be treating execute() as my constructor for now... until I better understand when the session variable becomes available. Struts2 needs to see who I am before it knows my session right? So it isn't going to provide it to me until it has done a certain amount of set up? There are hooks for lots of different events, from application startup to session creation, to request processing, etc. The important part is to figure out your requirements, then we can suggest where to do the work/store the object. -Dale - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Session object null
Thank you Dale, Let me see if I understand this correctly. Since the action object has not been created yet, and this is in a different thread... I can not get the correct context... Although the session objects _is_ someplace... It will also not work in the constructor even if the class is SessionAware because the object must exist before setSession(Map m) could be called... So... I should really be treating execute() as my constructor for now... until I better understand when the session variable becomes available. Struts2 needs to see who I am before it knows my session right? So it isn't going to provide it to me until it has done a certain amount of set up? Ken. On Tue, 2010-05-18 at 12:49 -0400, Dale Newfield wrote: ActionContext my current method is local to the thread so I can't use that > On 5/18/10 12:35 PM, Ken wrote: > > What I don't understand is I have an object who's constructor needs > > session data... > > So it calls: > > Map session = ActionContext.getContext().getSession(); > > > > But session is always null... > > http://struts.apache.org/2.1.8.1/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext() > > Explains that this static method works by looking up the value in a > ThreadLocal: "Returns the ActionContext specific to the current thread." > > So if that constructor is happening within the same thread in which the > request was originally being processed, and after xwork finishes setting > up that context, it should work: > > "The ActionContext is thread local which means that values stored in the > ActionContext are unique per thread. See the ThreadLocal class for more > information." > > But random code in a random thread occurring at a random time cannot > rely on ActionContext.getContext(). > > I'd check out the *Aware interfaces if I were you. > > -Dale > > - > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org >
Re: Session object null
Thank you Dale, Let me see if I understand this correctly. Since the action object has not been created yet, and this is in a different thread... I can not get the correct context... Although the session objects _is_ someplace... It will also not work in the constructor even if the class is SessionAware because the object must exist before setSession(Map m) could be called... So... I should really be treating execute() as my constructor for now... until I better understand when the session variable becomes available. Struts2 needs to see who I am before it knows my session right? So it isn't going to provide it to me until it has done a certain amount of set up? Ken. On Tue, 2010-05-18 at 12:49 -0400, Dale Newfield wrote: ActionContext my current method is local to the thread so I can't use that > On 5/18/10 12:35 PM, Ken wrote: > > What I don't understand is I have an object who's constructor needs > > session data... > > So it calls: > > Map session = ActionContext.getContext().getSession(); > > > > But session is always null... > > http://struts.apache.org/2.1.8.1/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext() > > Explains that this static method works by looking up the value in a > ThreadLocal: "Returns the ActionContext specific to the current thread." > > So if that constructor is happening within the same thread in which the > request was originally being processed, and after xwork finishes setting > up that context, it should work: > > "The ActionContext is thread local which means that values stored in the > ActionContext are unique per thread. See the ThreadLocal class for more > information." > > But random code in a random thread occurring at a random time cannot > rely on ActionContext.getContext(). > > I'd check out the *Aware interfaces if I were you. > > -Dale > > - > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org >
Re: Session object null
On 5/18/10 12:35 PM, Ken wrote: What I don't understand is I have an object who's constructor needs session data... So it calls: Map session = ActionContext.getContext().getSession(); But session is always null... http://struts.apache.org/2.1.8.1/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html#getContext() Explains that this static method works by looking up the value in a ThreadLocal: "Returns the ActionContext specific to the current thread." So if that constructor is happening within the same thread in which the request was originally being processed, and after xwork finishes setting up that context, it should work: "The ActionContext is thread local which means that values stored in the ActionContext are unique per thread. See the ThreadLocal class for more information." But random code in a random thread occurring at a random time cannot rely on ActionContext.getContext(). I'd check out the *Aware interfaces if I were you. -Dale - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Session object null
Hello Struts2 Forum, I've used the session object to get and set parameters. Where the set is done in an action() and the get is done in a jsp via OGNL. What I don't understand is I have an object who's constructor needs session data... So it calls: Map session = ActionContext.getContext().getSession(); But session is always null... but when it gets to the JSP it has data! I'm using Tomcat, any ideas? Ken.