This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Using same object for multiple uses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using same object for multiple uses" Watch "Using same object for multiple uses" New topic
Author

Using same object for multiple uses

Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
Hi all,

pls look at the following code:
I have got one class which will get the connection & process the DRL & DML stmts with oracle DB. (DAO.java).

I am using this class in my java file to get the connection and execute the queries.
My class is : MyDBTest.java
i am using like this::
String qry1 = "select * from emp";
DAO dao = new DAO();
dao.getRecords(qry1); //this getRecords method internally get the connection and execute the select queries.

and i have to execute one more insert query. can i use the same object to do this ??? like
String insQry = "insert into .....";
dao.executeQuery(insQry);

Is there any harm in using the same object for different operations.

or i have to create one more object of DAO class and use it for insert queries.

pls help me..
thanks & regards,
Mahesh
Scheepers de Bruin
Ranch Hand

Joined: Jul 19, 2005
Posts: 99
Generally it is a good idea to group related functionality together. For example: Say we have a Store and a Customer table.

I'd create three DAO classes: BaseDAO, StoreDAO and CustomerDAO

BaseDAO would handle the generic stuff (like opening/closing connections, etc) and have StoreDAO and CustomerDAO extend BaseDAO.

StoreDAO would handle all operations related to the store table (SELECTs, INSERTs, DELETEs, UPDATEs et al) in public methods. Same with Customer.


We're doomed!!<br />Yay!!!<br />No that's bad Girr!!<br />Yay!!!
Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
thanks Scheepers, for ur reply.

thats a good idea..and we are doing the same way..but like we execute multiple DML queries in a batch,, is it possible to execute the select and insert queries in a batch?

becoz here i am using the one object of dao in "select" query and the same object to "insert" query.
is this OK with the 'Objects' to use for different operations?
regards,
Mahesh
Scheepers de Bruin
Ranch Hand

Joined: Jul 19, 2005
Posts: 99
It should be ok in doing more than one operation per connection. Just make sure your objects are threadsafe.
Mahesh Pinnamaneni
Ranch Hand

Joined: Jul 02, 2004
Posts: 100
Thanks Scheepers...

By the way how can i make my program threadsafe.
is it so by using synchronized?
regards,
mahesh
Scheepers de Bruin
Ranch Hand

Joined: Jul 19, 2005
Posts: 99
Not quite.

Using synchronised simply means one thread at a time can access whatever you're synchronising - creating a breeding ground for bottlenecks.

The easiest way to make an object threadsafe:
1) Make sure you have no attributes that are specific to a thread (Having the request object in an object as an attribute would be a perfect example of how to make that object NOT threadsafe)
2) Attributes that you have should be read ONLY. (IF you have them - it's generally not a good idea. Rather pass the stuff you need in a method as arguments)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using same object for multiple uses
 
Similar Threads
how to rollback in java
Converting an application to Java (new to Java)
Steps to execute a query
Connection pool + performance
Need Help on DAO