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

Using same object for multiple uses

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
reply
    Bookmark Topic Watch Topic
  • New Topic