Posts

Showing posts from 2012

What is Hibernate Caching?

Image
In a typical application, you perform lot of operations like instantiate objects, load object from the database and so on. Sometime in multiuser application you may face a situation in handling multiple call of databases. Hibernate offers caching functionality which is designed to reduces the amount of necessary database access. This is a very powerful feature if used correctly. It increases your application performance and works between your application and the database as it avoids the number of database hit as many as possible. Hibernate Cache Types : Hibernate uses different types of caches. Each type of cache is used for different purposes. Let us first have a look at this cache types. First level cache Second level cache Query level cache 1. First level cache : First-level cache is the session cache and is always Associates with the Session object. Hibernate uses this cache by default. The Session object keeps an object under its own cache before committing to th...

Useful java Keytool Command

Generate a Java keystore and key pair : keytool -genkey -alias mycert -keyalg RSA -keystore keystore.jks -keysize 1024 Generate a keystore and self-signed certificate :  keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks -storepass password -validity 360 -keysize 2048 keytool command to view certificate details from keyStore : keytool -list -v -keystore keystore.jks Check a particular keystore entry using an alias: keytool -list -v -keystore keystore.jks -alias mydomain keytool command option is -printcert which prints details of a certificate stored in .cer file : keytool -printcert -file test.cer Export a certificate from a keystore: keytool -export -alias mydomain -file mydomain.crt -keystore keystore.jks   keytool -export -alias mydomain -keypass keypass -keystore keystore.jks -storepass jkspass -rfc -file keytool_crt.pem Note : "keytool -export" command uses DER format by default. The "-rfc" option is to...

How to install mod_jk.so on Cent OS

The Basics - What is mod_jk? The mod_jk connector is an Apache HTTPD module that allows HTTPD to communicate with Apache Tomcat instances over the AJP protocol. Steps: 1. Download the latest apache connector from http://tomcat.apache.org/download-connectors.cgi . 2. Untar the download by           tar zxvf <filename> 3. Goto native directory of connector           cd <connector dir>/native/ 4. Run the buildconf.sh scripts        ./buildconf.sh Note : If you get any issue like "autocong" not installed then install following things:        yum install autoconf        yum install libtool 5. You need "httpd-devel" tools to build it. So make sure you have already installed it by          yum list installed | grep httpd-devel     else install it " yum install...

Load Balancing on Web Application server clusters

Image
Overview A cluster is a group of servers running a Web application simultaneously, appearing to the world as if it were a single server. To balance server load, the system distributes requests to different nodes within the server cluster, with the goal of optimizing system performance. This results in higher availability and scalability -- necessities in an enterprise, Web-based application. High availability can be defined as redundancy. If a single Web server fails, then another server takes over, as transparently as possible, to process the request. Scalability is an application's ability to support a growing number of users. If it takes an application 10 milliseconds(ms) to respond to one request, then it should take 10 ms to respond to 10,000 concurrent requests. Of the many methods available to balance a server load, the main two are: DNS round robin and Hardware load balancers. DNS Round Robin To balance server loads using DNS, the DNS server ...

How to create self signed certificates programmatically ?

The most common approach of generating a self-signed certificate is using the  java keytool. There may be a situation when you want to create a self signed certificates programmatically One approach of programmatically generating these self-signed certificates is through the Bouncy Castle API. To start with this, you need to have the Bouncy Castle jar in your classpath.(You can download it from here ) Steps to generate self signed certificate key: 1. Create a public/private key pair for the new certificate           KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");         keyPairGenerator.initialize(1024, new SecureRandom());         KeyPair keyPair = keyPairGenerator.generateKeyPair();   2. Create new certificate Structure         // GENERATE THE X509 CERTIFICATE         X509V3Certi...

How to generate Self-Signed Certificate Using keytool

Image
The example uses the keytool utility to create a new self signed certificate. Open the command console (Run as Administartor) on whatever operating system you are using and navigate to the directory where keytool.exe is located. Run the following command (where validity is the number of days before the certificate will expire): keytool -genkey -keyalg RSA -alias selfsigned -keystore keystore.jks  -keysize 1024 Fill in the prompts for your organization information.  This will create a keystore.jks file containing a private key and  self signed certificate. 

MySql: Give Root User Logon Permission From Any Host

Image
I have already updated about this in my previous  blog but that was from UI, In this tutorial You can do the same task from MySQL client itself. To configure this feature, you’ll need to update the mysql user table to allow access from any remote host, using the % wildcard. Open the command-line mysql client on the server using the root account. Then you will want to run the following two commands, to see what the root user host is set to already: use mysql; select host, user from user; Here’s an example of the output on my database. mysql> use mysql; Database changed mysql> select host,user from user; +-----------+------+ | host      | user | +-----------+------+ | localhost        | root | | 127.0.0.1 | root | | ::1       | root | | localhost |      | +-----------+------+ 4 rows in set (0.07 sec) Now I’ll update the localhost host to use the w...

Liquibase Tutorial

What is Liquibase ? LiquiBase — available since 2006 — is an open source, freely available tool for migrating from one database version to another, It is an open source database-independent library for tracking, managing and applying database changes. A handful of other open source database-migration tools are on the scene as well, including openDBcopy and dbdeploy. LiquiBase supports 10 database types, including DB2, Apache Derby, MySQL, PostgreSQL, Oracle, Microsoft® SQL Server, Sybase, and HSQL. All changes to the database are stored in XML files and identified by a combination of an "id" and "author" tag as well as the name of the file itself. A list of all applied changes is stored in each database which is consulted on all database updates to determine what new changes need to be applied. LiquiBase executes changes based on this XML file to handle different revisions of database structures and data. When you first ...

How to get Current Thread Example

/*         This Java example shows how to get reference of current thread using         currentThread method of Java Thread class. */   public class GetCurrentThread {           public static void main ( String [ ] args ) {                                 /*                  * To get the reference of currently running thread, use                  * Thread currentThread() method of Thread class.                  *                  * This is a static method.                  */                         ...

Should Logger members of a class be declared as static?

Advantages for declaring loggers as static Disadvantages for declaring loggers as static common and well-established idiom less CPU overhead: loggers are retrieved and assigned only once, at hosting class initialization less memory overhead: logger declaration will consume one reference per class For libraries shared between applications, not possible to take advantage of repository selectors. It should be noted that if the SLF4J binding and the underlying API ships with each application (not shared between applications), then each application will still have its own logging environment. not IOC-friendly Advantages for declaring loggers as instance variables Disadvantages for d...

How to get directory size in linux ?

 du -sm <dir_name>

What is Liquibase ?

1.  Liquibase is an open source database-independent library for tracking, managing and applying database changes. 2. All changes to the database are stored in XML files and identified by a combination of an "id" and "author" tag as well as the name of the file itself. 3. A list of all applied changes is stored in each database which is consulted on all database updates to determine what new changes need to be applied. 4. Liquibase executes changes based on this XML file to handle different revisions of database structures and data. 5. When you first run a changelog, LiquiBase manages those changelogs by adding two tables into your database. databasechangelog : maintains the database changes that were run. databasechangeloglock : ensures that two machines don't attempt to modify the database at one time. 6. Limitations do exist such that it will not export triggers, stored procedures, functions and packages. Sample changeLog file:  The above is...

How to integrate Liquibase with Spring and Hibernate ?

Image
A sample tutorial on how to integrate Liquibase with Spring and Hibernate. While writing this tutorial, I have added javadoc in the code for better understanding and I believe You already have good knowledge on Spring and Hibernate. The main motto of this tutorial is to give an idea on how you can integrate Liquibase with Spring and Hibernate. If you are new to Liquibase : Click Here To integrate liquibase into your project, you need liquibase jars, So  download it before starting the project. I have created an application named "SHLIntegration". The Structure of the project is as follows : The dependencies are also listed here: Lets start with Employee class : 1. Create Employee class having getter/setter and add proper JPA annotation to each variable as below.   public class Employee {     @Id     @GeneratedValue     @Column(name="EMPLOYEE_ID")     private long id;     @Column...

Some ANT task

1. How to build  project from another build. <target name="project2"             description="Builds project2 project, required depedency">         <!-- Build project2 first  -->         <subant target="dist" verbose="yes" inheritall="false">             <filelist dir="../com.waheed.project2"                       files="build.xml" />         </subant>     </target>   2. How to read SVN revision and write into some file <loadfile property="revision" srcFile="./.svn/entries">         <filterchain>           ...

Spring MVC tutorial

Before Starting, I believe you must have basic idea about JAVA, SPRING and Spring MVC. For Spring MVC : http://waheedtechblog.blogspot.in/2012/08/spring-mvc.html In this tutorial , I will just tell you what are the basic thing that you need to start MVC. Step 1 : Create a class  @Controller public class HelloWorld {        @RequestMapping ( "/hello" )      public String helloWorld() {           return = "Hello World, Spring 3.0!" ;      } } 1 . The class HelloWorld  has the annotation @Controller and @RequestMapping("/hello") . When Spring scans this class, it will recognize this bean as being a Controller bean for processing requests. 2 .The @RequestMapping annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. Step 2. Mapping Spring MVC in WEB.xml The entry point of Spring 3...

Spring MVC

Image
Spring MVC helps in building flexible and loosely coupled web applications. The Model-view-controller design pattern helps in seperating the business logic, presentation logic and navigation logic. Models are responsible for encapsulating the application data. The Views render response to the user with the help of the model object . Controllers are responsible for receiving the request from the user and calling the back-end services. When a request is sent to the Spring MVC Framework the following sequence of events happen. The DispatcherServlet first receives the request. The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request. The Controller process the request by calling the appropriate service methods and returns a ModeAndView object to the DispatcherServlet . The ModeAndView object contains the model data and the view name. ...

Sending Email Via JavaMail API Example

From last one month, My internet device is missing from my terrace. So, Mostly I use mobile to access my mail but accessing mail via mobile is very irritating thing because of the slow bandwidth. To send one single mail I have to wait till the mailbox get opened. This is very simple way to send mail without opening your account. :) This is the example to show you how to use JavaMail API method to send an email via Gmail SMTP server. You need mail.jar library to run this code. import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /**  * @author abdul  *  */ public class SendEmail {     public static void main(String[] args) {                 final String username="YOUR_USER_NAME"...