wood burning stoves 2.0*
The moose likes JDBC and the fly likes PersonDao design help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PersonDao design help" Watch "PersonDao design help" New topic
Author

PersonDao design help

Greg Bag
Ranch Hand

Joined: Dec 25, 2010
Posts: 52
Hi guys, I just wanted to get your input on the way I have abstracted my methods. This is like a PersonDao that I will be using in my servlets. This is only a personal project I'm working on to refresh my servlet skills.

Some things that bother me...

1) If I use an instance of the PersonDao in many servlets(all in the doGet() or doPost() methods), will there be any concurrency issues? The only time that should happen is if I put it as an instance variable on the servlet right?
2) Code seems to violate the open-closed principle. If I add a new enum, I have to go back and change the getPerson() method.

Overall, does it seem like it's well designed. If I didn't do it this way, then I would've had 2 large methods of getPersonById and getPersonByCredentials and the bodies of both those methods would almost be identical to the getPerson() method. I saved a lot of duplication.

Any input would be greatly appreciated!







E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Greg Bag wrote:Hi guys, I just wanted to get your input on the way I have abstracted my methods. This is like a PersonDao that I will be using in my servlets. This is only a personal project I'm working on to refresh my servlet skills.

Some things that bother me...

1) If I use an instance of the PersonDao in many servlets(all in the doGet() or doPost() methods), will there be any concurrency issues? The only time that should happen is if I put it as an instance variable on the servlet right?
2) Code seems to violate the open-closed principle. If I add a new enum, I have to go back and change the getPerson() method.




1.) Yes, creating the instance in the doGet/doPost is safe.
2.) You can build the query in a generic way by using the person object as the search criteria. Then in your query you include the property in the where if it is populated on the passed in Person object. That way you can search for any property on the Person class without adding any code. You might have to do a case on the property type in order to build the query properly. Also, you are vulnerable to sql injection, use PreparedStatements to build your queries.
Greg Bag
Ranch Hand

Joined: Dec 25, 2010
Posts: 52
Also another question regarding concurrency. Imagine 10 different doGet() requests from 10 different users around the world called within milliseconds of each other to a servlet called LoginServlet.java. Inside the servlet's doGet() method is a call to DbUtil.getConnection(). The DBUtil class is static and the method getConnection() is also static. What happens here? What are possible race conditions, concurrent issues, etc etc? Can there be corruption between connections or something? Is there a better alternative to this design for connection pooling?

Just picture LoginServlet.java doing something like this.


Greg Bag
Ranch Hand

Joined: Dec 25, 2010
Posts: 52
Anyone?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

You only need one datasource. As long as the different Dao's call getConnection, you are fine. You want the DataSource to be handling the connection pooling for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PersonDao design help
 
Similar Threads
Severe Error: Syntax
Adding code to return an ArrayList of objects
Hibernate QuerySyntaxException with MySql
Returning a 0 value error
invalid column index