• 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

JDBC Code Review

 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am currently working on a discussion forum application.

The method below is used to start a new thread. Multiple tables are being updated (inserts and updates). Some tables are being queried for data required for updates.

Transaction management is in place.

Apart from this I also have a custom exception which is thrown in case of an empty result set.

If any kind of exception is thrown, the transaction is rolled back. I am also rethrowing the exception to the calling program for some further processing.

Can any one go through the code and suggest how to improve it? There is always room for optimization and refactoring so all suggestions are welcomed.

Regards,
Danish


 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code looks decent, but it's a huge load of boilerplate. Why don't you check out JDBCSupport ? It can minimize the code for you ;-)
 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:Your code looks decent, but it's a huge load of boilerplate. Why don't you check out JDBCSupport ? It can minimize the code for you ;-)



Yes, I agree that the method is really long. Checked out JDBC Support. It is still in beta and was released recently.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Danish Shaukat wrote:
Yes, I agree that the method is really long. Checked out JDBC Support. It is still in beta and was released recently.



It's my project and it's quite young. Check it out and let me know how it's going ;-)
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Danish,

Looks neat the code, however even for a basic code you should consider the JDBCSupport.

However, in MHO I think you should consider use hibernate, it will turn your life so easy and it isn't that hard.

Cheers and good luck!


 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My comments:
1) I would use a ConnectionPool rather than obtaining the connection via username/password each time.
2) I agree on the boilerplate comment. Many people write their own superclass with the boilerplate or use an ORM framework like Hibernate.
3) startNewThread() is way too long. While the exact length of a method is debatable, most people agree is should fit on a screen or two. Look for snippets you can extract into new methods - like reading from the resultset.
4) I would list out the columns rather an "select *". In fact, I would also specifically list the columns I needed - in this case threadId. Returning columns you don't need as a performance impact.
5) threadRS.close() should be in a finally block.
6) Having to null out variables is an anti-pattern that signifies the code that uses those variables should have been in a separate method.
7) I'd name "selectThread" as "selectTheadStmt". The former sounds like it is of Thread type.
8) "// get Unix time stamp value in seconds " - These comments are also an anti-pattern that the code the comment applies to should be in method named getUnixTimeStampValueInSeconds() and the like.
9) Why do you clear the parameters when the prepared statement has just been created?
 
Jeanne Boyarsky
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the length thing, it isn't just boilerplate. It is that there is so much going on that it is hard for a reader to figure out what the code does. What if someone other than you has to maintain the code? For that matter, what if you have to in a year?

Compare the code in the first post to the following. Your method names would be clearer of course as they could describe what the queries do. They'd also be more real and pass the proper variables around.



In addition to fitting on a screen, my version has business meaning - you can tell why each code block is needed. It also provides chunks that are possible to reuse.

[edited to fix typo]
 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wilson and Jeanne,

Thanks for going through the lengthy code. Your comments are valuable. I will be changing the code as per guidelines.

Regards,
Danish
 
Wilson Cursino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Danish you are welcome. Let me know if you need any help with the conversion.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic