• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

DAO to Instance variables

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a bunch of DAO classes that extend the class called BaseDAO, that has instance variables like ....

private Connection _conn = null;

private boolean b_connIsReference = false;

private PreparedStatement _pstmt = null;

private Statement _stmt = null;

private ResultSet _rs = null;

private OracleCachedRowSet _crs = null;

private OracleConnectionFactory _connfactory = null;

/** stored procedure use */
private CallableStatement _cs = null;

/** @associates String */
private ArrayList a_columnNames = new ArrayList();

/** @associates String */
private ArrayList a_whereValues = new ArrayList();


Besides that the DAOs' that extend the BaseDAO. The corresponding DAOs also have the instance variables.


An instance of BaseDAO is created every time to get a copy of the instance variable.

An instance of corresponding DAO is also created whenver the DAO is has a request.

The apllication is working with out any issues.

Question...

Is it OK to have instance variables in DAOs?

Is my current design efficient?

Appreciate expert opinion and thanks in advance.
[ November 05, 2006: Message edited by: Bear Bibeault ]
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Puthriah Sarma:

Is it OK to have instance variables in DAOs?

Is my current design efficient?

Appreciate expert opinion and thanks in advance.



It totally depends !!

It depends on how you are organising your code.I will not prefer to use any variable as instance variable, which deals with database.

Using connection,statement or resultset as instance variable means you are going to use them across method.

And it is very likely that you get unexpected result.

Let say you are sharing instance variable of connection ! and in one method you close the connection and in another method you get error because of close connection. and If you don't close connection it may be possible you forget to close connection in other method.

That is why say It totally depends how you organise your code.


Shailesh
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If you are using a lot of instance variables there there will be unsafe.when multiple threads will access then there thread unsafe.
So if there is most requirement of those var then declare it as instance var else as local.
Regards,
Prashant kumar Singh
 
Srinivasa Kadiyala
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for the valuable feed back
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic