Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating Session Variables

 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created some global variables for my web app that I can reference in my servlets and classes.

Example:



How can do the same thing but at a session level. I need to set a few variables based on user log on and make these available to my classes without having to send these to the class constructors.
 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bad idea to make mutable data globally accessible.

Do you have a good reason not to pass data to constructors?
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if you have a web application, what you want to use are Session attributes.
You can get a Session object from a Request and get/set attributes on that.

 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:It's a bad idea to make mutable data globally accessible.

Do you have a good reason not to pass data to constructors?


Yes, I think so. Up until now the application could run under one common userid and password which I set as global variables.
Now I have a need to run the application based on a mode setting. So I have set the userid and password as fields of a defaults class where it can be set dynamically.
In this scenario I would have to send the defaults class to my connection class.

I have the following code in my app over 2300 times.

 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Well if you have a web application, what you want to use are Session attributes.
You can get a Session object from a Request and get/set attributes on that.



But how do I reference a session attribute in a class?

I have the following in the post method of my first servlet that the client request:

 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I would be more worried about having that particular piece of code in your application more than 2300 times. Why are you establishing connections in so many different parts of your application?

You would be much better off creating some kind of connection pool, or connection manager class, pass the username and password to its constructor, and then pass the manager to any class that needs to set up a connection.

Being able to mutate global variables is a recipe for disaster.
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, I would be more worried about having that particular piece of code in your application more than 2300 times. Why are you establishing connections in so many different parts of your application?

You would be much better off creating some kind of connection pool, or connection manager class, pass the username and password to its constructor, and then pass the manager to any class that needs to set up a connection.

Being able to mutate global variables is a recipe for disaster.


I am using connection pool. This code is used in every place I use sql to get remote data.

Can you explain what you mean by connection manager class?

This is the DataConnection class:

 
Stephan van Hulst
Bartender
Posts: 6327
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The DataSource counts as the connection pool/connection manager. In fact, I don't know why you have a wrapper for it. The whole DataConnection class seems useless.

Just pass a DataSource to the constructor of classes that need to perform database queries. Those classes should not have to care about usernames, passwords or other configuration, that's what the DataSource class is for.

When your application receives a request, your controller/servlet can look up a DataSource and set it up (optionally by taking configuration from the request's session). It passes the DataSource to the constructor of anything that interacts with the database.

All of this can be done very neatly if you have some sort of dependency injection framework. Are you using an MVC framework for your web application?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic