• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

java.sql.Connection object, can it be a class var of a DAO?

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All. I am wondering if there is any reason NOT to do this. I have many DAO object that start like this :


Could I declare the conn, stmt, and rs variables in my BaseDAO class as protected class variables, and then in ProcedureDAO just do this ?


Then in BaseDAO I could have a method that closes all these objects, I could call it in the finally block of the getProcedure method and any other method of classes that extend BaseDAO.

I never see this in any example code, so I am wondering if there is a reason NOT to do it.
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are proposing to move the declarations of those variables to a wider scope. There's a design rule of thumb which says to declare variables in the narrowest possible scope.

But rules can be broken if there's a good enough reason. So it's up to you to provide a reason why you want to break that rule, not up to somebody else to provide a reason why you shouldn't. What's your reason?
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic