Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PersonDao design help

 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic