Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using same object for multiple uses

 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be ok in doing more than one operation per connection. Just make sure your objects are threadsafe.
 
Mahesh Pinnamaneni
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic