"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
J. Kevin Robbins wrote:Please feel free to nitpick as my goal is to continue improving as a developer.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jesper de Jong wrote:First of all, returning a List<List<String>> isn't very informative. Just by looking at the method signature you have no idea what this method returns (it's a list of lists of strings, but what does the content of the return value mean?). I would make a class to hold a record and then return a list of instances of that class instead.
Jesper de Jong wrote:Since the two values are dates, I would use Date objects instead of strings. If you're using Java 8, then definitely use the new date and time API, and otherwise consider using Joda Time.
Jesper de Jong wrote:Are you absolutely tied to using stored procedures? If not, then I would consider using JPA / Hibernate or Spring Data JPA.
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Richard Tookey wrote:The code does exhibit one of my pet hates -
You catch an exception that you class as severe, you log it and then continue as if nothing had happened. To my mind, if you have a severe exception then you cannot continue. I would expect to see the exception logged and then re-thrown or converted to another exception and then thrown.
Winston Gutkowski wrote:The first thing is that, if you're on version 7 or later, you may be able to save yourself a bit of code with a "try-with-resources" block.
Winston Gutkowski wrote:Second: What you seem to have implemented is, for want of a better term, a "stringizer"; and I'm not sure precisely what that gives you (have a look at the StringsAreBad page) - ie, you've simply converted a native form (a ResultSet) into a matrix of Strings, which you'll presumably have to convert back to whatever type each 'field' actually represents later on.
Winston Gutkowski wrote:Third: It seems to rely on being able to read ALL the rows in one go, which might lead to a VERY big program if your query returns millions of rows.
Winston Gutkowski wrote:Fourth: The inner List relies on the order (and number) of fields being the same every time. Given the nature of rows in a database, this may not be a problem, but a List<HashMap<String, String>> - where the HashMap contains "column name' + value" - might be a bit more generic.
Winston Gutkowski wrote:However, finally: It all seems a bit "procedural" to me. My understanding of a DAO is that it's an "interim" between a data-access layer and an actual Java object (eg, a Customer), and is actually provided to you by the DA layer. And I'd say that ResultSet already fits that bill pretty well.
All your code seems to have done is add another layer (and actually a more abstract one) between that and whatever 'Pojo' you end up with.
I suppose it might be easier to use if you're just slapping data out to a screen, but I can't really see what else it buys you. But maybe I'm just being thick.
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Karthik Shiraly wrote:
Normally, I'd put the DataSource.getConnection() call too inside try block.
Not sure what happens in this case, where it's getting connection from something called com.mypackage.jkr.obj.DataSourceHolder.
If that class too is swallowing the exception, then this code is going ahead as if it's no big deal, which I think is wrong.
On the other hand, if that class is throwing some exception - presumably an unchecked one - then this class is
1) inconsistent in exception handing, because it propagates some exceptions while swallowing others 2) possibly throwing exceptions but has not documented them
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
J. Kevin Robbins wrote:This is something that I definitely need to learn more about. I usually don't do anything with exceptions except log them. How do you recover from a SQLException? What's the advantage of re-throwing it just to be handled somewhere else? What's the advantage to converting it to a different type of exception?
J. Kevin Robbins wrote:
This is something that I definitely need to learn more about. I usually don't do anything with exceptions except log them. How do you recover from a SQLException? What's the advantage of re-throwing it just to be handled somewhere else? What's the advantage to converting it to a different type of exception?
Joe Harry wrote:
J. Kevin Robbins wrote:
This is something that I definitely need to learn more about. I usually don't do anything with exceptions except log them. How do you recover from a SQLException? What's the advantage of re-throwing it just to be handled somewhere else? What's the advantage to converting it to a different type of exception?
It definitely is not a good idea to handle SQLException in a layer that sits above the DAO layer in the call chain. I would rather throw a domain specific exception (for example., DataAccessException) to the layer above! The advantage is that your business layer or the client of your DAO is independent of any java sql packages.
Dave Tolls wrote:Just an observation, but your DataSourceHolder opens a new Connection each time, which is fine.
However it is never closed, at least as far as I can see.
Dave Tolls wrote:Also, you are doing the same thing in getConnection as in the DAO, absorbing an exception that probably should be thrown (or wrapped and thrown as mentioned above).
In this case getConnection should probably throw it rather than attempt to handle it. It's what I would expect from something that produces a Connection.
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
J. Kevin Robbins wrote:
Dave Tolls wrote:Just an observation, but your DataSourceHolder opens a new Connection each time, which is fine.
However it is never closed, at least as far as I can see.
Look back at the original code post where I get the connection from the DataSourceHolder and then close it in the finally block. Does that not work the way I think it does? If I'm not really closing the connections in the finally block of all my DAOs then I'm surprised that I've never exhausted the connection pool. I'm going to debug my way through one now and see if that portion of the finally block executes.
J. Kevin Robbins wrote:
Can you please give me an example of this? Are you talking about throwing it back to the servlet so the servlet can send an error message to the jsp?
Dave Tolls wrote:
Ah, phooey.
I'll go get my eyes tested!
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Joe Harry wrote:Yes, I assume that there is a POJO layer in-between your Servlet and your DAO. This POJO layer is where your business logic should be encapsulated. You catch the SQLException in your DAO layer, wrap that into your application specific exception and hand it over to the business layer. Now depending upon the business functionality, you can decide what to do with the exception in your POJO business layer.
J. Kevin Robbins wrote:
Not always. Many times the page is just a report that displays data in a grid-style format. Some of the apps have an object that I call a ChartBuilder, other times it's just servlet to DAO and back to servlet for display. It almost sounds like I would benefit from a POJO just for exception handling for those cases, or is that overkill?
Do you want ants? Because that's how you get ants. And a tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|