• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it the right way?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ralf Rommel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ralf Rommel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sunglasses. AKA Coolness prosthetic. This tiny ad doesn't need shades:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic