aspose file tools*
The moose likes JDBC and the fly likes DAO to Instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "DAO to Instance variables" Watch "DAO to Instance variables" New topic
Author

DAO to Instance variables

Srinivasa Kadiyala
Ranch Hand

Joined: Jun 10, 2004
Posts: 237
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 ]

Srini
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

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


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Prashant kumar Singh
Greenhorn

Joined: Nov 04, 2006
Posts: 22
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

Joined: Jun 10, 2004
Posts: 237
Thanks to all for the valuable feed back
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DAO to Instance variables