Thursday, 30 May 2013

Java 7 feature :Multi catch block exception handling example with tutorial

Java 7 features – Exception Handling

Java 7 language has introduced features like Usage of Stirings in Switch case, and improving the exception
handling . It introduced multi catch block

Prior to java 7, if we want to handle multiple exceptions in single catch block is not possible, but we can achieve this using multiple catch block where each catch block used to catch single exception.
Let us see the example of multi catch exception handling in java 7

Handling multiple exceptions in catch block example prior 7

For example, prior to java 7, if we want to catch multiple exceptions, thrown by try block, we need to write a separate catch block for each exception type as shown below.
 // possible code throwing exception 
     }catch(IllegalArgumentException iae){
 //catch  IllegalArgumentException and print error message
     }catch(Exception iae){
 //catch  Exception and prints error message
Java 7 introduced multiple exceptions are handled in single catch block
Handling multiple exceptions example in java 7
Java 7 has introduced single cach block for handling multiple exceptions thus reduce code of catch blocks
Below example
   // possible code throwing exception
catch(IllegalArgumentException iae|Exception e) {
In the above code, IllegalArgumentException and Exceptions are defined in same catch block with separator pipe | symbol

Advantages of Multi catch exceptions in Single catch block:-

1. Simplify the coding
2. Decrease code duplicate
3. Reduces catch blocks

That’s my understanding on multi catch block in java 7 language

Wednesday, 29 May 2013

2 ways to create Connection Pool in java with examples

Java Connection Pooling

Usually applications are hosted on application server, and talk to database using database connection. data
base connection is plain HTTP socket connection to database machine.
Whenever request comes to application, application create a plain socket connection to database, once the request is completed, connection is timeout/closed, so like this for every request, connection is established and performances of application is decreased.So connection is expensive operation, so for every request, creating connection and closing connection is expensive operation. creating and closing connections need to be handled by applications.
To improve the performance of connections mechanism, connection pool is introduced.

Connection pool is a mechanism/concept used by software applications to connect to backend databases with a pre created group connections and reuses the connections whenever required,
Once a request is completed, connections are placed in connection pool

Connection pool is pool of group of pre created connections managed by application server or webserver,

Advantages of Connection Pool

Reduce network calls, thus improve network latency
Improve the application performances by reusing the connections
No need of opening or closing connections as group of connections already in pool and reuse and placed in or out from pool.

Disadvantages of Connection Pool

Need to do configure connection pooling carefully, otherwise database reject/close connections.
Connections are like a resource and there might be resource memory leakage issues

How to create Connection Pooling in java

Below are the examples to create various ways to create a connection pool.
Connection Pool can be implemented in various ways by using Custom java code connection pooling using LIFO stacks or FIFO Queues or different frameworks provided by apache and others.
Connection pool can be created in either in Standalone or web container/application Servers like jboss, tomcat , weblogic and websphere.

JDBC Connection Pooling example

Connection pool can also be created usind jdbc using JNDI name or Datasource.
JNDI name is a name configured mapped to connection pools using websphere, weblogic
Datasource is interface on which implementation can be done by different vendors like apache DBCP and c3po

Connection Pool settings:-

For any connection pool, Below number of parameters are required. For having better performance in Production systems, settings need to be tuned.

Total number of connections in Connection Pool:-This parameter specifies the total number of connection to be created at start up. Basically in web application, these connections are created at application start up and destroyed when application stops.

Maximum Connections :- tells maximum connections to be created in connection pool

Minimum connections: - specifies minimum connection to be create a pool

Connection Increment: - if total request are more than maximum connections, container increment the connection count

Connection pool Idle timeout :- if connection is established and idle for some time, then container return that connection to pool, later other request can be reuse it. This is basically to free the database connection resource.

Apache DBCP Connection pool tutorial:-

Apache DBCP is database connection pooling concept to create/manage group of connections. This can be used to create a java database connection pool to improve the performance of an java applications. Apache tomcat is internally using DBCP connection pool framework

connection pool using Apache DBCP with example

Datasource object is required for getting Connection object.
To get Datasource object, we need to create a following code
import java.sql.*;
import org.apache.commons.dbcp.*;

public class C3POPConnectionExample {
 public static void main(String args[]) throws Exception {
  BasicDataSource dataSource = new BasicDataSource();
  Connection con = dataSource.getConnection();
  System.out.println("Connection Object information : " + con);

To execute/compile the above dbcp connection pool example, dbcp jar should be in classpath

Java C3PO connection pool

C3PO connection pool is one of powerful and stable open source connection pool, performance is also good compared to apache dbcp.

Below is the example for C4PO code. Before that c3po jar should be in classpath.

import java.sql.*;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBCPConnectionExample {
 public static void main(String args[]) throws Exception {
  ComboPooledDataSource connectionPoolDatasource = new ComboPooledDataSource();
  Connection con = connectionPoolDatasource.getConnection();
  System.out.println("Connection Object information : " + con);

that's it my learnings/understanding on connection pooling in java.

Please click +1 if you like this post.

Saturday, 25 May 2013

Linux/Unix - Top 6 cat command examples with tutoral

Basic Cat command examples in Linux/Unix

Cat command is simple command used by every Unix/Linux user. It is one of frequently used command in Linux/Unix Systems.Cat command works on bash, and powershell and shell options.
Cat command behaves different based on the different types of usage.

By using cat command, we can do the following functionalities

1. Read files using cat command
2. Create files Using cat command
3. Copy files content using cat command.

Cant command works unix,linux as well as ubuntu terminals

Cat command Syntax

Cat (options) (one or more file name)

Let us see the various cat command examples as below.

Read or view file using cat command with examples in Linux/Unix

This is simple command to read the contents of file and output the file content to Standard output like console
$cat cloud.txt

This is test file contains test content.
If it is small file, we can view it using cat command, but file size is large we can use more or less with pipe options in cat command.

How to create a file using Cat command in Unix

Cat command is a basic command to create files. To create a file demo.txt
$ cat >demo.txt
This is test file contains
test content.
Once the above command is entered, new demo.txt file is created and console waits from user to type input and whatever data typed by user is placed in file.> symbol specifies that data to placed in a file typed by a user. You need to type CTRL with typing D to save data to file and exit from console.

How to merge files using cat command in Linux/Unix

Cat command also used to merge different files and create a new file
$ cat file1 file2 > output.txt

First file1 data is copied to output.txt and next file2 data is copied to end of file1’s data.

How to view the line numbers of each line using cat command

In java programs, if any error happens, Error/Exception thrown with print statck trace which will give filename as well as line number where exception is occurred. In that case, to view the java code with line numbers, we can use cat command with option –n
$ cat –n cloud.txt
1 This is test file contains
2 test content.

Append/concatenate data to existing file using cat command

$cat >>cloud.txt
New data is appending to existing file
We have to press CTRL+D option to save/append the new data to existing data in a file

How to view file with showing spaces, tabs using cat command with examples.

In any text Editors, we have option to be enabled to show spaces and tabs codes

Cat has also has the option to view spaces and tabs and end of lines using option –T
$ cat –T filename

Cat command options

Basic cat command has different options, can be used with all flavors of linux like AIX

-T output spaces, tabs and End of lines characters in the file
-s suppress the multiple blank lines
-n output number on each line of file

That’s it from usage of cat command in daily life of software development.

Please click +1 if you like this post.