Thursday, April 28, 2011

How to configure DBCP(DB connection pooling ) using mysql and apache tomcat 6.XX server

Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them.
This technique of "pooling" connections is based on the fact that most applications only need a thread to have access to a JDBC connection when they are actively processing a transaction, which usually take only milliseconds to complete. When not processing a transaction, the connection would otherwise sit idle. Instead, connection pooling allows the idle connection to be used by some other thread to do useful work.
In practice, when a thread needs to do work against a MySQL or other database with JDBC, it requests a connection from the pool. When the thread is finished using the connection, it returns it to the pool, so that it may be used by any other threads that want to use it.
When the connection is "loaned out" from the pool, it is used exclusively by the thread that requested it. From a programming point of view, it is the same as if your thread called DriverManager.getConnection() every time it needed a JDBC connection, however with connection pooling, your thread may end up using either a new, or already-existing connection.

Benefits of Connection Pooling

1. Reduced connection creation time
2. Simplified programming model
3. Controlled resource usage



Implementation of Connection Pooling

Generally, you configure a connection pool in your application server configuration files, and access it via the Java Naming and Directory Interface (JNDI). The following code shows how you might use a connection pool:


 Implementation of Connection Pooling
Generally, you configure a connection pool in your application server configuration files, and access it via the Java Naming and Directory Interface (JNDI). The following code shows how you might use a connection pool:
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class MyServletJspOrEjb {
public void doSomething() throws Exception {
/*
* Create a JNDI Initial context to be able to
* lookup the DataSource
*
* In production-level code, this should be cached as
* an instance or static variable, as it can
* be quite expensive to create a JNDI context.
*
* Note: This code only works when you are using servlets
* or EJBs in a J2EE application server. If you are
* using connection pooling in standalone Java code, you
* will have to create/configure datasources using whatever
* mechanisms your particular connection pooling library
* provides.
*/
InitialContext ctx = new InitialContext();
/*
* Lookup the DataSource, which will be backed by a pool
* that the application server provides. DataSource instances
* are also a good candidate for caching as an instance
* variable, as JNDI lookups can be expensive as well.
*/
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
/*
* The following code is what would actually be in your
* Servlet, JSP or EJB 'service' method...where you need
* to work with a JDBC connection.
*/
Connection conn = null;
Statement stmt = null;
try {
conn = ds.getConnection();
/*
* Now, use normal JDBC programming to work with
* MySQL, making sure to close each resource when you're
* finished with it, which allows the connection pool
* resources to be recovered as quickly as possible
*/
stmt = conn.createStatement();
stmt.execute("SOME SQL QUERY");
stmt.close();
stmt = null;
conn.close();
conn = null;
} finally {
/*
* close any jdbc instances here that weren't
* explicitly closed during normal code path, so
* that we don't 'leak' resources...
*/
if (stmt != null) {
try {
stmt.close();
} catch (sqlexception sqlex) {
// ignore -- as we can't do anything about it here
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (sqlexception sqlex) {
// ignore -- as we can't do anything about it here
}
conn = null;
}
}
}
}
Best Size for Connection Pool

To correctly size a connection pool for your application, you should create load test scripts with tools such as Apache JMeter or The Grinder, and load test your application.
For an example 15to 20 connections which can cater to about 400 to 500 concurrent users over a minute time span .

Connection Pooling Configuration

Apache Tomcat 4.1
Place a copy of mysql-connector-java-[version]-bin.jar in $CATALINA_HOME/common/lib/. Then, follow the instructions in the section MySQL DBCP Example of the Tomcat documentation.
Notice that Connector/J 3.0 and newer work with the same settings: http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
Tomcat uses the Apache Software Foundation's DBCP libraries, which you can also use in your own non-J2EE code: http://jakarta.apache.org/commons/dbcp/

Sunday, April 24, 2011

How to reduce cyclomatic complexity (part of refactoring ) of java code ?

Database view usage and impact on performance of website

The word View itself suggests that it will be restricted access to some information in database based on privileges and shared summary of information from different tables.

A view is a virtual table.They are just complex query to create abstraction for user .These are not real table because it does not take physical memory more it is like table on fly .View may consists data from many table .
It may be required to show to user on a page from many table with just querying view than dealing with complex queries .

Most of time view are created to simplify interaction with DB and to make faster retrieval of group of data.

Advantages -

1. Grouping data from multiple table in one virtual table
2.Hiding complexity of query
3.They may provide security depending on DB privileges.For example you can create view for admins to see all other information except password .
4. It saves memory as they are not real tables .They are just special group of queries
5. If data in real table is updated then it will reflect in the view .
Suppose very simple a situation there is "employee" with emp_id(pk) and emp_name columns .There is other table with "department" id_emp(fk) , dept_name .
You want to show table in UI with employee name and Department name .Create view for this like view_emp_dept and query that for showing or updating data.



Examples - create view

CREATE OR REPLACE  VIEW view_emp_dept AS
SELECT emp_id ,emp_name , dept_name
FROM employee, department
GROUP BY name
Now query this view to get employee

Example - Get data from view

 SELECT emp_name , dept_name from  view_emp_dept  where emp_id =10;



Example - drop view

 DROP VIEW view_name view_emp_dept

If view are not defined properly then we have to create new view for each situation so define views considering some generic situation .
If view definition is not efficient for example distinct key word, unrefereed tables etc it can kill performance so before defining view we have consider time taken by that query .


So judicious use of view could help to simplify queries in code and could make our life simpler .


Monday, April 11, 2011

MAT for JVM heap dump analysis

Load Testing of web application using Jmeter

Jmeter is open source tool from apache to test performance of application when large number of users access in small amount of time .

This not a browser but it is software which simulated request and response condition which happens at actual time.
Jmeter does not execute your Java scripts , render ajax request etc .It just creates http request object in case of web application and set proper parameter in it.So if you want to analys time taken by each page loading then it would be time difference between request and reposes .Actual browser will take some more time depending on type of browser and its caching mechanisms .
In practice it is very impossible to create scenario in which 1000 users logs in to system and try to play with it .Jmeter virtually creates given numbers of threads which can innudate the server with hhtp request .

Stress testing / Load testing is concept from mechanical engineering in which we test how much requests server can handle without going out of memory :)
Jmeter has pretty long learning curve .


From scratch if you have to do something minimum one week will require to just create a demo .
Lot of problems come in this like cookie manager policy , URL rewriting , creating login and password mapping for say 100 users .


To use you much know main terminologies in it .

Main terminologies


Element

Description

Thread Group

As with any load test, execution is multi-threaded. The thread group element is what controls concurrent connection to your application. In end user terms, it's your group of users.

Samplers

A sampler is the basic type of controller. Very simply, samplers tell JMeter to send requests (HTTP, SOAP, etc…) to a server. There are also Logic Controllers, but we won't be using one in this tip.

Listeners

Listeners are what you use to access to the information JMeter gathers while running. They give you the pretty charts

Timers

Timers are how you set delays in JMeter. Timers fire before each request that a thread makes.

Assertions

Assertions, like in any test tool, allow you to check for specific behavior as your test executes. Assertions provide the standard pass/fail results.




Installation –

Ø Please refer to installation video - Installation video

· Download from

http://jakarta.apache.org/jmeter/

· Download zip file - Unzip it at c: / drive

Ø Please refer video to learn how to use it - You tube video is planned .



Steps to follow -

Scenario -

I am taking general scenario in which you have to log in to system and hit some pages .
Lets consider that 100 users hit the same page in period of one second .

First you have to record all requests and responses using Jmeter then run for 100 users .

Please refer to you tube video lecture for this

How to use Jmeter.


References

1.Jmeter reference book

http://www.doc88.com/p-7040136164.html

2. Apache user manual

http://jakarta.apache.org/jmeter/usermanual/index.html





Friday, April 1, 2011

Design Patterns in Java

This blog is going to scratch surface of vast and subjective topic called design patterns .To use them is not science but it is an art .To judge the situation and put effort to create generic code is important otherwise one might waste lot of time in creating world class design of piece code which is going to be used once in a while .So it is combination of proper judgment , situation , time , resources available , importance of functionality etc .
Many people do not understand very concept of usage of them , most of them understand only singleton pastern .As usage is situation based it is very difficult to teach the proper situation in which one has to implement one .Many time we have to use combination of many design pasterns to tackle the situation .To use the cocktail of such pasterns need much experience and keen mind .But even though design patterns are used and other developers do not understand them then they can not maintain that code .So point is this more intellectual solution could confuse other developed if they are not aware of design patterns and ultimate delay in bug fixing.


What is design pattern and why to use them ?

What is a design pattern?

If a problem occurs over and over again, a solution to that problem has been used effectively. That solution is described as a pattern.
The design patterns are language-independent strategies for solving common object-oriented design problems.

What are the advantages?

  • Reusable foolproof Design , and code
  • Time saving as they are tested strategies for particular situation .
  • Change extensive and flexible .
  • Easy maintenance of code
  • Better clean code improve communication between developers who work on it.

What are the disadvantages?
  • Complexity of code as solution is generalized.
Design Pattern Categories

  • Creational Design Pattern
Deals with creation of instance of object . Whole idea revolves around theme to control
instantiation of object .

Exp. Window window = new Window();
  • Structural Design Pattern
  • Behavioral Design Pattern
Creational Design Pattern

  • Factory
  • Abstract factory
  • Builder
  • Singleton/Multiton
  • Prototype
  • Lazy initialization/Eager initialization
1. Factory Method

Concept

  • It deals with the problem of creating objects without specifying the exact class of object that will be created.
  • This return the children classes in form of parent class .
  • It uses inheritance .(subclasses + interface )
  • Factory class have only one instance .

Example -




Use –
  • Flexible way to create object without bothering its actual class.


2. Abstract factory

Concept –

Provide interface for creating families of related or dependent objects without specifying their concrete class.
Concrete factory have only one instance .

Example –




Use –
Abstract factory isolates concrete class to be generated .
Creator don’t have idea which kind of class of object it must create
Return several group of related objects .

3. Builder

Concept –

To build Object part by part than creating whole at a time .
The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects.

Example –


Use –
  • Let you vary product internal representation
  • Isolates code for construction and representation
  • Finer control over construction process.


4. Prototype

Concept -

Create new Object by copying or cloning the prototype object.
Reduce the cost of creation
Cloning may use shallow copy / deep copy of cloning
May use serialization for deep copy .

Example –




Use –
  • To reduce for creation of complex object .
  • There is an object instance in memory almost similar to object wanted .Deep Clone that object and set new few fields and use for further purpose.
5. Singleton

Concept

Ensure that class has only one instance and provide global point to access it .
Most of times services and factory methods uses this form .

Example-




Use –

While creating factories for object creation
Creating services like file manager , process manager etc .

7. Lazy initialization/Eager initialization

Concept -

  1. Create new object on first request for the object .
  2. Create object before the first request for the object .

Comparison

Lazy

Eager

public class View{

private int courseCount;

private boolean isCounted;

public int countNbCourses()

{

if(!isCounted){

courseCount = countCourses();

}

return courseCount;

}

}

public class View{

private int courseCount;

private boolean isCounted;

public View

{

courseCount = countCourses();

}

public int countNbCourses()

{

return courseCount;

}

}



Summary of creational patterns

Pattern Name

Generalized Situation

Factory Pattern

Used to choose and return instance of class in form of super class / interface .

This uses inheritance .

Abstract Factory Pattern

Return one of several group of classes .

Return object based on chosen factory .

Uses Factory pattern internally .

Builder Pattern

Assemble number of objects to create new composite object this based on data it represent .

Many time objects are assembled using factory .

Prototype Pattern

Copies or clones existing instance than creating new expensive one .

Singleton pattern

Ensure there is only one instance of object and provide global access to that point .




Structural Patterns

  • Adapter
  • Bridge
  • Composite
  • Proxy
  • Façade
  • Decorator

Behavioral Pattern

  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator

Reference -

Design Patterns - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides