wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Is it the right way? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Is it the right way?" Watch "Is it the right way?" New topic
Author

Is it the right way?

Ralf Rommel
Greenhorn

Joined: Dec 25, 2002
Posts: 15
Currently, I have a class something like this


Is this class properly desinged and/or coded? Or can it be made better?
What difference will it make if I make the attributes like dataSource, context as static?
Basically I want to know, whether it is a right way of doing things.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Basically, it's hard to tell from that short code snippet. One thing is badly designed for sure, though: the class name...
Another detail: You don't need to (and therefore shouldn't) declare all the local variables at the top of a method. Always declare it at the latest possible time.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ralf Rommel
Greenhorn

Joined: Dec 25, 2002
Posts: 15
IP: Basically, it's hard to tell from that short code snippet
I know. Actually I wasn't even expecting to get a reply for such a vague post!
But, believe me, that's all I have
Let me try to explain. Assume that this method will check whether a given user-id is present in the database or not, and return a TRUE or FALSE. That's it. So some other class will call this method to check the validity of the user-id. Nothing else is there in this method or in this class (at least for present) apart from the try-catch-finally blocks and log statements.
IP: One thing is badly designed for sure, though: the class name...

Actually, the name of the acutal is even worse. I'm not posting it, for my manager also visits JavaRanch, and I don't want him to sniff me posting here asking for answers !
IP: You don't need to declare all the local variables at the top of a method
Coding standards of this Organization
Okay. Let me be bit more concrete in my question.
When I access a datasource from a Servlet, I used to initialize the datasource in the init() method and reuse it in service() method to get the connections later. How do I acheive a similar effect here?
I mean, everytime the isValid() method is called, I need to lookup for the datasource again and again. Are there any better ways to do it?
If I make it static, I know one instance of it will shared by all the objects. But even that doesn't help. So is there a way to initialize the datasource once, and keep it safe for a longer span?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ralf Rommel:
IP: One thing is badly designed for sure, though: the class name...

Actually, the name of the acutal is even worse. I'm not posting it, for my manager also visits JavaRanch, and I don't want him to sniff me posting here asking for answers !

So you don't want him to know that you are competent enough to ask for help at the right places? You must be working in a strange business culture...

IP: You don't need to declare all the local variables at the top of a method
Coding standards of this Organization

And a very stupid one...

When I access a datasource from a Servlet, I used to initialize the datasource in the init() method and reuse it in service() method to get the connections later. How do I acheive a similar effect here?
I mean, everytime the isValid() method is called, I need to lookup for the datasource again and again. Are there any better ways to do it?
If I make it static, I know one instance of it will shared by all the objects. But even that doesn't help. So is there a way to initialize the datasource once, and keep it safe for a longer span?

Sounds like you are in need of some form of datasource pooling. This part of your question could probably better be answered in the JDBC forum.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
When I access a datasource from a Servlet, I used to initialize the datasource in the init() method and reuse it in service() method to get the connections later. How do I acheive a similar effect here?
I may be missing the point, but I thought that's what a constructor is for. Servlets only have an "init" because the constructor is called by the container.
How about something like:

Does that make sense?
Darn. forgot to pad those blank lines again. Should be more readable now
[ November 20, 2003: Message edited by: Frank Carver ]

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ralf Rommel
Greenhorn

Joined: Dec 25, 2002
Posts: 15
IP: You must be working in a strange business culture...
No. I'm working under a strange manager! In his opinion, anybody (except him) who gets help from others to solve problems are incompetent
IP: And a very stupid one
I can only pray that someone in this organization reads this and realizes how true is your comment.
IP: Sounds like you are in need of some form of datasource pooling.
There is a connection pool already, provided by WebLogic. I'm getting connections from that using this datasource. Now, are you suggesting to have a pool of datasources itself (in addition to pool of connections)? I don't think WebLogic provides any such options. But let me try it in JDBC forum also for a better answer, as you suggested.
The reason why I posted the message in this forum is to know the best practices in such cases. I'm sure lot of people would have faced a similar problem before, and how they solved it? Is there any pattern which can help?
To FC:
Thanks for the code.
It is true that the constructor is a better place. But we won't gain anything by that. Do we?
Assume that, this method will be called from the doGet() method of a Servlet like this

So, everytime the doGet() method is called, we will be creating a new object and we will be looking-up for a datasource. Right?
Now, I can't modify the servlet to include the datasource object, for the servlet doesn't belong to me.
Any suggestions? Hope I'm not confusing things here.
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Hi Frank,
I had a look at your code. I have some questions there
1) In your code, you have made the query string as static. But why not make the the dataSource also as static? Is there some specific reason for that?
2) You are initializing the datasource in the constructor. What if that part of code throws an Exception (like NamingException)? How to deal with it? (I read somewhere, it is a bad practice for constructors to throw exceptions)
3) Your finally block contains the code to close the connection. But the code connection.close() itself can throw SQLException. How to deal with it?
Will it be okay to do something like

Please comment.


Mani
Quaerendo Invenietis
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it the right way?
 
Similar Threads
PreparedStatement's executeQuery() returning resultset with no rows
Statement Closed Error - please help
Data Access Code
looking for suggestions on better DAO design
JDBC pooling, MVC, and OO newbie...