Thursday, 12 November 2009

Develop High Transaction Web Applications with Java MySQL & NetBeans

This entry is a brief tutorial on how to develop high transactional web application. I will be looking at how to develop a high transactional application while maintaining data integrityFor the purpose of keeping this entry simple, I will be using some RAD tools, the NetBeans IDE, to generate most of the code.

In order to follow the tutorial, you will need the following:
  1. NetBeans IDE 6.7+
  2. Java JDK 1.6+ (my version is 1.6.0_17)
  3. MySQL 5.1+
  4. MySQL Sakila database
  5. Apache Tomcat 6.x
  6. An understanding of JPA transactions and ReSTful web services
This is my definition of High Transaction Applications:

A high transaction application is one that can serve multiple simultaneous requests from clients and keep them secure from each other. The application has only two purposes: read or write data to/from a repository either JMS or DB. The transactions have to meet the ACID criteria in order to be deployed in the real world.

Ok, the above is my definition and you are free to redefine it. I am going to build a web application that will be an n-tier application:
  • Database back-end (MySQL)
  • Entity classes
  • ReSTful Web Services to allow other developers to integrate the application with theirs
  • A web based front-end
For the simplicity of the article, I will not implement any security such as user or application level security (authentication and database table privileges). This tutorial is mostly geared toward the newbies but I am sure that more advanced developers would benefit too.

Let's get coding.

  1. Make sure that you have loaded the MySQL Sakila database into your MySQL database. You can download the Sakila database from the NetBeans plugin centre (see here).
  2. Create a new web application and name whatever you like. I have named mine "WebApplication". I will now refer the application as WebApplication. Make sure to choose Tomcat as your deployment server
  3. Add the MySQL driver "mysql-connector-java-5.0.7-bin" to the WebApplication libraries. NetBeans will work and connect to the DB even without the driver but once you deploy your application to a server, the application will not be able to connect to the DB and throw a ClassNotFoundException com.mysql.jdbc.driver.

  4. We are going to develop the back-end first. NetBeans makes it very easy for us to create Entity classes from database. I would recommend newbies to learn how to manually create Entity classes and configure the persistence.xml file. This tutorial makes use of JPA but one should ignore the drawbacks of JPA/ ORM frameworks.



  5. Right click on the project name and choose Entity Classes from Database... On the next screen choose the "filmActor" table and click on the Add button. Make sure that "Include Related Tables" box is checked underneath the Selected Tables panel. The screenshot does not show the "filmActor" table as I have previously generated the Entity class but I am sure you get the idea.





  6. If you are required to create a Persistence Unit, click on the persistence button -> you can accept the default name -> choose your Persistence Library -> choose your database connection -> choose "None" for Table Generation Strategy -> click Finish








  7. After choosing the table to generate the Entity from click next -> fill Package name -> tick Generate Name Query... -> click Finish


  8.  

    NetBeans generates all the Entity classes based on the database table that you have chosen. The next thing that we want to do is generate a set of ReSTful web services from the generated Entity classes. Again, NetBeans facilitates the work for us (it is important that you also know how to create the classes manually or you will not know how to debug them if there is any problem in the future).

  9. Right click on the project name "WebApplication" -> RESTful Web Services from Entity Classes... -> Choose the Entity Classes that you would like to generate the WS for and click add or add all -> click next -> on the following screen, accept the default values and click Finish




  10. You can go ahead and test your ReSTFul Web Services by right clicking on the name of the application and click on Test RESTful Web Services




  11. The previous step will launch your web browser within which you can test your web services (click on the node on the left and see the queries on the right)




  12. Back in the NetBeans IDE; right click on the project name "WebApplication" -> click on JSF Pages from Entity Classes... (The JSF pages will not use the web services as there are packaged together with the Entity Classes. This will improve performances and still allow external applications to integrate). Choose the Entity Classes that you would like to generate the pages for -> click next. On the final screen, fill in the package name for the JPA Controller and JSF Classes -> click finish




    NetBeans will generate the necessary files to create a CRUD application with a user interface. I suggest that you familiarize yourself with the generated code.



  13. Expand the Configuration folder under your project name "WebApplication" and the web.xml file. At around line 38, change the content of the welcome-file-list to look as follow

    <welcome-file-list>
            <welcome-file>faces/welcomeJSF.jsp</welcome-file>
    </welcome-file-list>
    This will make the generate JSF page to be the landing page for the application when requested. Make sure that you do have a "welcomeJSF.jsp" file before making the change.



  14. Right click on the application name -> Run. The application should load in your web browser. Now go on, play around with the application. And why not create a client to send request to the web services? (not today)


You can load test your application by using Apache JMeter. It is easy to run and configure. If you want to take a look at how JPA implements the ACID features, browse to your controller classes. Here is a short introduction to JPA transaction. Hope you enjoyed and if you need any clarification, just leave me a comment and try to get back to you ASAP (if time permits).

6 comments:

  1. So... basically, this is a tutorial on how to use the NetBeans RAD features. I am not seeing anything about "high transaction" web applications. Guess you get what you paid for.

    ReplyDelete
  2. @Ara, NetBeans RAD features are useful in creating high transaction web app. High transaction web app if updating a DBMS system, need to make sure that they implement all the ACID features correctly. If you run the tutorial and take a look at the "controller" classes, you will see how the code is generate. I am not saying the code generate is bug, actually I said the complete opposite. You have to check your code to make sure that you:

    - open a connection
    - execute the statements
    - commit the changes (transaction)
    - roll back if there's any problem.

    High transaction goes beyond just code and brings in system architecture, caching and data replication.

    This is entry was more about the code side.

    ReplyDelete
  3. Agile software development is a group of software development methodologies based on iterative and incremental development, where requirements and solutions evolve through collaboration between self-organizing, cross-functional teams.
    java software company | software development company | java web development | blackberry app development | iphone app development | android app development | java outsourcing | it outsourcing services | http://www.tenaxtechnologies.com

    ReplyDelete
  4. This is good written blog and helped me lot to share so many things from here..Thanks for this valuable post.
    web designing company

    ReplyDelete
  5. Are there some advantages of one language C and Java over the other? Which one is optimal? Which one is more "future proof"? Would it be optimal to know both? If so, which order? I am a little confused on the subject. Just got across this course http://www.wiziq.com/course/1779-core-and-advance-java-concepts, will it be helpful also. A little enlightenment could help. Thanks

    ReplyDelete
  6. Hello mates, its wonderful paragraph concerning cultureand completely explained, keep it up all the time. my weblog;
    CMS developer London

    ReplyDelete