Pages

Monday, April 11, 2016

C3P0

Establishing a Connection

                              Creating and establishing a database connections are relatively very expensive. Because it involves processes like establishing a network connection, initializing database session, authorization in the back end database etc.


      Database or DBMS is a Data Source. A legacy file system or some other data source with a corresponding JDBC driver are also data sources.

       When establishing a connection to a database, a JDBC (Java DataBase Client) Application can use any of the two classes

  • Driver Manager
                  connects an application to a data source which is specified by its URL (eg:database URL)
                   driver manager class calls the method DriverManager.getConnection to create the connection.The method requires a database URL depending on your DBMS. eg: jdbc:mysql://localhost:3306/


  • Data Source   
                 provides a new way for JDBC clients to obtain a database connection. Its the newer and the preferred way.

Advantages of using Data Source over Driver Manager

                        01) portability is increased

                        02) enables the connection pooling and distributed transaction
                              mechanisms
                      
                        03) properties of data source are kept in a configuration file, 
                              so that any change to the data source or data drivers can
                              be made in the configuration file. But in driver manager 
                              properties are hard coded in the application. So after the
                              change the code has to recompiled






             C3P0 provides Data Source implementations


  What is C3P0  ? 

           C3P0 is an easy-to-use library. Increase the functionality of traditional JDBC drivers and make them enterprise ready. The most useful service it provides is when  acquiring database connections it provides newer javax.sql.DataSource  scheme which has many advantages over the traditional DriverManager based JDBC drivers.

 

  Prerequisities

requires level 1.6.x or above Java Runtime environment 

  C3P0 Usage

 For the users c3p0 provides standard jdbc DataSource objects. 3 ways are available to acquire c3p0 pool-backed datasources.
     

  • instantiate  PoolBackedDataSource and set its ConectionPoolDataSource and    
        thereby build our own pool backed data source


First method among the above mentioned 3 methods is regarded as the most convenient method. Lets discuss each in detail.


Method 1

Instantiating and Configuring a ComboPooledDataSource


       - instantiate an instance of  com.mchange.v2.c3p0.ComboPooledDataSource.

        -  ComboPooledDataSource() is a public ,no-arg constructor.

        - But before using the DataSource, make sure to set at least the property   
          jdbcUrl.

       - Following shows the code for fully configuring the DataSourceand make a
          usable pooled DataSource.


   
     
       - Above configured DataSource can then be used to make the connection 
         with the database as follows

                 
ComboPooledDataSource dataSource = DatabaseUtility.getDataSource();
Connection connection = dataSource.getConnection();
     
       - Furthermore, we can configure multiple DataSources using named 
         configurations supported by c3p0.

       - For that, construct the ComboPooledDataSource with the configuration
         name as the constructor argument as below.



Method 2

Using the DataSources factory class

  hope detailed explanation on this method soon

 

 

 

 



Using any of the above mentioned methods we can create our  Data Source.
For any Data Source built in, c3p0 use default values for the configuration parameters, if not programmed specifically. But these default values can be overridden using configuration files. Configuration also can be done in 3 ways.

  • using a simple java.util.Properties files called c3p0.properties
  • using a XML formatted file called c3p0-config.xml
  • using more advanced HOCON configuration files  called application.json


Tuesday, April 5, 2016

Log4J

Logging in Java 

                            

Why we need logging in Java ?

Java Logging or Logging in Java is almost an art of science. At the same time what to log in java, which login level to use, the format of messages etc. also comes along with the practice.


      It is a well known fact in industry that java logging severely affects the performance of systems. Thus it becomes very necessary to understand and learn java logging in detail and best practices on logging in java.

    System.out.println in Java for printing messages is not powerful as the advanced Java logging API s. In case of writing a Java application server, to know what the server is doing or when a server error occurs, java logs would be very much useful. This necessity cannot be full filled by System.out.println. (Though we can redirect the standard output to a file, it is very inflexible. At the same time it prints out everything and we cannot filter what goes to the output)


What is Log4J ?


  • It's a java logging utility. In brief it is a library that specializes in logging.
  • Log4J is written in Java and distributed under the Apache Software   License.
  • It gives levels of priorities to the logging process and logging information can  be directed to variety of destinations like databases, files, console, UNIX syslog etc.
  • Log4j is highly configurable through external configuration files at the runtime




 Log4j Architecture

Log4j API follows a layered architecture. Each layer performs a different task. And different objects are designated to perform these tasks




Getting a logger object........
  •  is a class in org.apache.log4j.*
  • one logger object per one java class
  • call a static factory method to create a logger object
 

static Logger log = Logger.getLogger(YourClassName.class.getName())



  • logger methods are used to generate log statements 

  • logger methods are assigned with a level of priority







Getting an layout object........

  •  is a class in org.apache.log4j.Layout which is the top level  class(abstract) in the hierarchy
  •  we work with its subclasses which are as follows

 











line 13 => layout object created












Getting a appender object........

  • takes the log statements created by the logger object and stores in some database
  • is an interface









line 18 => create Appender object

#first parameter -> layout object
#second parameter -> file name












Configuration  in Log4j 2

The latest version of Log4j being Log4j 2.5, has made the logging process more efficient, less coding and easy. Yet the basics explained above applies.

Log4j 2 mainly focuses on configuration. Once the configuration is done successfully, its just a matter of initializing the logger and calling the required logger method as shown below.

    In the above class Bar the logging is done by following steps, after proper configuration
  • importing log4j related classes
  • define the static logger variable
  • call the logger methods like entry, error

4 Ways of accomplishing Log4j Configuration

 
       1) Through a configuration file written in XML, JSON, YAML, or properties format. 
       2) Programmatically, by creating a ConfigurationFactory and Configuration implementation.
       3) Programmatically, by calling methods on the internal Logger class.
       4) Programmatically, by calling the APIs exposed in the Configuration interface to add
          components to the default configuration.
 
 
 

 Here we mainly focus on configuring log4j using a configuration file 

Furthermore it should be noted that log4j provides default configuration in cases where a configuration file is not found. In the default configuration, following are included

  • A ConsoleAppender attached to the root logger.
  • A PatternLayout set to the pattern "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
  • Level.ERROR is assigned to the root Logger

example of a configuration file




  •  two main tags. <Appenders></Appenders>and   <Loggers> </Loggers>
  •  define the  appender type as console, file etc. inside the Appenders tag




               - pattern for the log message layout can be specified under the        PatternLayout tag as shown above
  •  define the logger level inside Loggers tag for each appender specified in Appenders tag 

 

 

 

               -if we want to specify different logger levels to different classes , it can be done by adding a new logger definition
               -it can be done as below by mentioning the class name which requires a different logger level than the others and its required logger level








Sunday, April 3, 2016

Mongo DB

What is MongoDB..... ?

  • open source
  • cross platform
  • document oriented
  • leading NoSql database 

  • written in C++

UBUNTU INSTALLATION

  •  Download the MongoDB binary file of the latest technology from the site
        https://www.mongodb.org/downloads

  • Extrace the file from the downloaded archive using the command 
    tar -zxvf mongodb-linux-x86_64-3.2.4.tgz
      
  •   Copy your extracted archive to the installation directory(target directory)
  • Give the location of the MOngoDB installation folder to the PATH variable in bashrc. Add below to the .bashrc file
     export PATH=<mongodb-install-directory>/bin:$PATH
 

  GETTING STRATED

  • navigate to the installation folder
  • go to bin folder in it and type  
           ./mongod --dbpath /data/db
  • take another terminal and type 
          ./mongo
 
 MongoDB configuration file => etc/mongos.conf

 

Important Terminology 

  •  Database  
                       Its a container for collections. Multiple databases resides in   a single MongoDB server.
  • Collection 
                       Group of documents is a collection.Typically documents in a collection are  similar. But documents within a collection can have different fields.

  • Document
                    Set of key-value pairs. Documents have dynamic schema
  • Dynamic Schema 
                     documents in the same collection can have different fields or structure. These documents can have the same field, but values need not to be same. Below shows a sample document structure.

      

 Relationship of RDBMS terminology with MongoDB 

Working with MongoDB

 Below mind-map shows the basic commands when dealing with MongoDB like creating a database, creating a collection, querying  a document etc.





querying a document

    We learnt find() method is used to query a document and find().pretty() to get the display in a formatted way. Below is a screen shot of a simple query


Below shows commands in querying a document on the basis of some condition.
 (in below commands 'mycol' is the collection name)





(hope the update on the following sub topics)

Indexing

Aggregation

Replication

Sharding

 

 

 














Saturday, April 2, 2016

MAVEN

What is MAVEN ??



It is a Open source Build automation and project management tool across all segments of the Java community. It speeds up the development life cycle of a project by facilitating a uniform build system through Convention over Configuration philosophy

 Basically it helps with managing

  • Builds
  • Documentation
  • Reporting
  • Dependencies                           
  • Releases

My First Maven Project

      to create the simplest maven project execute the following command

 Here we encounterd some new terms. Lets clear them. :)

  • archetype => its a template of a project.It is combined with some user inputs to create a working maven project.
  •  generate  => it is the goal of the project associated with the archetype mechanism.

  • DgroupId => key identifier of a project.Typically the domain name of the organisaion is given

  • DarchtypeGroupId  => designated group id for the plugin that created the project

  •  artifact => the final output of a development project

  • DartifactId => the name given to the artifact produced fro the project


Once the above command is executed the directory called my-app is created with the below directory structure.



The pom.xml file created in the project directory is significant. It is the basic unit of work in maven.It contains every important piece of information about the project. Following shows the POM.xml file

  • project             => top level element in all maven pom.xml files. Specifies   the Urls from where the maven dependencies required for the project need to be downloaded.
  •  modelVersion  => the version of the object model this POM uses.
  • groupId, artifactId => as specified  in the command used to create a maven project
  • packaging  => type of the package used by the artifact. eg: JAR, WAR etc.
  • version  => version of the artifact generated by the project. SNAPSHOT indicates that the project is in the state of development.
  • name  => indicates the display name used for the project
  •  url  =>  indicates where the project site can be found
Other areas that can be included in a POM.xml file are
  •  dependencies
  •  developers and contributers
  • plug-in lists
  • plug-in executions
  • plug-in configurations

 

What is Build Life Cycle ?

  It is the sequence of phases which defines the order in which the gaol has to be executed. Here phase is one stage of the Life Cycle

 Maven has 3 standard life cycles. They are :

  • Clean
  • Default
  • Site

Clean Lifecycle

This deletes the output of a build by deleting the build directory

 consists of the following phases

  1. pre-clean
  2. clean
  3. post clean


               
pre-clean
execute processes needed prior to the actual project cleaning
clean
remove all files generated by the previous build
post-clean
execute processes needed to finalize the project cleaning

When we execute mvn postclean life cycle, all the above mentioned 3 phases are executed.


Default Lifecycle

 This is used to build the application and is the primary life cycle of maven. Basically has 23 phases. 

Below are the main phases required to build a standard basic application.

  • Validate
  • Compile
  • Test
  • Package
  • Integration-test
  • Verify
  • Install
  • Deploy

Site Lifecycle

 This life cycle is used for the creation of the project site documentation.


pre-site
execute processes needed prior to the actual project site generation
site
generate the project's site documentation
post-site
execute processes needed to finalize the site generation, and to prepare for site deployment
site-deploy
deploy the generated site documentation to the specified web server
 

 

 

 

 



REFERENCES USEFUL

https://www.mulesoft.com/tcat/tomcat-maven

tomcat manager
http://blog.techstacks.com/2009/05/tomcat-management-setting-up-tomcat.html