Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

To be stateful of to not be stateful, that is the question  RSS feed

 
D James Scanlan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone.

First, a confession. I'm coming in from the dark side. I've been a microsoft programmer for years but my organisation is now shifting over to java.

In .net there's a strong focus on making classes stateless - i.e. a data layer would be a set of static operations. This avoids having to instantiate the class, which among other things cuts out spending resources constructing and deconstructing the class.

However, I've discussed this issue with a consultant we brought in and he suggested that in the java world it would be more appropriate to make things statefull, to actually instantiate objects and to rely on java to garbage collect and manage resources. He also claimed instantiated objects were more thread safe.

What do you think about this? Thanks in advance etc..
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific on what you meant by data layer:
a. Are these helper classes to get and set data
b. customized classes for different data scenarios.

The instantiation as needed question will be answered based on more details from you.
 
D James Scanlan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure,

When I was talking of "generic data layers" I was meaning the class that performs the interaction to a back-end server. Performing all the standard crud operations.

I appreciate that things could be different in the java world. I'm currently working in a struts based websphere project that has data acccess performed by a number of objects. I prefer to centralise data access and make the main operations with a database generic. So a data access object wants to call a stored procedure, but it doesn't do it directly, instead it calls my generic class that does the work and then returns a value back.

My question is, would the method/operation calls of this generic class be static or should I instantiate it first. This isn't a question about whether data access classes need to be stateful to allow for caching or referencing database connections. This is a question about stateful v stateless purely.

Thanks in advance.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In J2EE realm, there are different ways to do it. I would suggest you to look into something like Hibernate to get to your centralized data access situation. It has different functionalities that can enable/disable data access as stateful or stateless based on dataset(or query for that matter) being accessed.

Hibernate is a open source project.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is, would the method/operation calls of this generic class be static or should I instantiate it first.
I don't know how things work in .NET servers, but in JEE servers it is possible for two clients to make a request to the same servlet at just about the same time. When this happens they are both using the same servlet object. So if your class only has static methods, both clients can only see one set of data. Whereas if you created a separate instance of the class for each client, then each client would be seeing their own data.

I would have thought that static methods would be a bad idea in .NET for the same reason, but then as I said I don't know much about .NET. If it has some mechanism for providing separate versions of class X for different requests then the all-static idea would be okay.
 
ulrika Johansson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a newbie and I made a mistake.
[ January 07, 2006: Message edited by: ulrika Johansson ]
 
ulrika Johansson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by stateful?

Anyway, there's no difference between Java and .net in any design respect. So, basically it doesn't matter what technology you use. It could be Sun, it could me MS, it could be IBM.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulrika, you will need to contact one of us via e-mail at this point since you resisted all past attempts to communicate with you. These are available via the links on our postings.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Ulrika, you will need to contact one of us via e-mail at this point since you resisted all past attempts to communicate with you. These are available via the links on our postings.


Just to clarify, "one of us" means a sheriff or Paul Wheaton.
[ January 07, 2006: Message edited by: Ilja Preuss ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!