2

Good day, my situation is the follow: I am designing a small web system using java, my knowledge in this field is very limited (and principally comes from small tutorials and Head and First Servlets and JSP Book) with produces as result and endless flow of doubts about how to make a good (and efficient) design, the only thing that is 100% clear is that MVC is a must (at least in my case).

After reading this this I got a general idea of how the system should be done. How to implement the view and the controller (and how they shall communicate) became clear.

Now, my problem resides in the model part of the MVC pattern. Java Beans and DAO classes are to be used here right? My application will have mainly CRUD operations (only 2 - 3 additional data operations will be needed in the whole system) so, am I supposed to do N (being N the number of tables with I need access to) Java Bean objects and N DAO classes with only those CRUD operations with the follow structure?

Java Bean:

 public class TableJB{ 
     // Table attributes with their respective getters and setters
 }

DAO:

 public class TableJB{ 
     public static void create(TableJB jb){}
     public static TableJB read(int id){}
     public static void update(TableJB jb){}
     public static void delete(int id){}
     public static List<TableJB> listAll(){}
 }

The problem I see with this structure is that I will be having the connection management (opening and closing) in each of these methods, it seems wrong in my perspective (as if someone else should be handling these) and I am far from sure if this is the better practice. Also I wonder if I am supposed to re-validate data before writing it the database in case that client validations were disabled for some reason (e.x: by disabling JavaScript)?

As an important note: I am not allowed to use any framework for my solution (so no Spring, Hibernate, etc.).

Community
  • 1
  • 1
Potatow
  • 76
  • 1
  • 1
  • 9
  • 1
    Related: http://stackoverflow.com/q/15981244/1065197 – Luiggi Mendoza Sep 11 '13 at 21:09
  • Will not there be future unclosed connections problems if I just return them in a class as the DatabaseConnectivity example that you just linked? – Potatow Sep 11 '13 at 21:13
  • *I wonder if I am supposed to re-validate data before writing it the database in case that client validations were disabled for some reason (e.x: by disabling JavaScript)* all your validations must be done in server side mostly than in client side. – Luiggi Mendoza Sep 11 '13 at 21:13
  • 1
    No, the database connection pool handles this. I explained this before: [How to properly keep a DB connection from a Connection Pool opened in JBoss](http://stackoverflow.com/a/18749265/1065197). The principle applies for any decent connection pool, and I assume the application server you will use already has one :) – Luiggi Mendoza Sep 11 '13 at 21:15
  • Can you elaborate on why you're not allowed to use frameworks? Is this a homework thing, or a matter of not adding new dependencies to a project, or what? Just what are you allowed to use? – David Moles Sep 26 '13 at 22:10
  • Personally, it seems to me that you are talking about "services" here, not the model. In my world (and worlds vary), the "model" is the object(s) that is/are referred to by a view in order to render itself (or to be rendered). The controller would have used the DAOs (or services) to obtain the model instance(s) and put them in place for the view to render. Don't know if that helps or confuses. I'll add that this is flavored by my many years of Spring MVC usage, so take it with a grain of salt. – MikeThomas Sep 26 '13 at 23:01

2 Answers2

1

First things first, you must do your user input validation on the server-side. As you said, client-side javascript can be disabled so you can't solely rely on it. Client-side validation should be treated a recommended, but optional, extra.

For your business model and database model, you might be well served to think about these two things separately. For example, I would consider all of my business data and then follow the object orientated design principles and come up with a solid class diagram. As a separate activity, I would follow the principles of data normalization and come up with a relational database model (ERD). These two data models could (and probably should) look different to each other. The last step would be to design the DAO layer as a bridge between the two data models - it's probably going to look like a hybrid of the two models. Once I have a rough draft of these three pieces built, I'd continue to iterate and improve.

Generally, you're going to be opening and closing a database connection for each and every CRUD operation - use Database Connection Pooling to address performance concerns - http://wiki.metawerx.net/wiki/DatabaseConnectionAndPoolingQuestions

icyitscold
  • 1,151
  • 1
  • 9
  • 19
0

Definitely, before start coding you should Architect the solution using Software engineering principal. Hibernate is the solution of the a lot rewriting of the code including the open and close connection. In the connection pooling you need to get and release the connection manually.

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15