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

Monday, February 29, 2016

MySQL

What is MySql ?

 

Its a fast and easy to use Relational Database Management System(RDBMS) being used for many small and large scale businesses.

Database is an application that stores a collection of data. Nowadays we mostly use RDBMS to store and manage large volumes of data. This is called Relational Database, since all data are stored in tables and and different tables are related using relations like primary keys or foreign keys.

 

 


Installing MySQL Using binaries

 

  •  First download the required package as a tar file
  • Then uncompress it at the place where you desire to make the mysql installation directory

  • Execute the following commands creating a user and a group called mysql


shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
shell> bin/mysqld_safe --user=mysql &

  • here I chose /usr/local as my installation directory 
  • Be careful when giving permissions to the created mysql user as the root


 Problem...?

Above mentioned steps didn't make my mysql demaen up. terminal struck in  the middle
the below error was given when the service was tried to started

test@hsenid-OptiPlex-3020:/usr/local/mysql$ service mysql.server start
Failed to start mysql.server.service: Unit mysql.server.service failed to load: No such file or directory.

 

      Solution :)

what is mysqld...?

http://dev.mysql.com/doc/refman/5.7/en/mysqld.html


it is the main program that does all the work in the mysql installation process.

it manages access to MySql data data directory which stores mysql databases and tables and also log files and status files.

Then I found that there are several methods of starting the mysql server

  1. invoking mysqld directly
  2.  invoking mysqld_safe which tries to determine proper options for mysqld http://dev.mysql.com/doc/refman/5.7/en/mysqld-safe.htmlOn linux systems that support systemd, can use it to control the server
  3. http://dev.mysql.com/doc/refman/5.7/en/server-management-using-systemd.html
  4.  On systems that uses System V-style run directories (such systems provides a standard process for controlling which programs init launches and configuration files for SysV init are located in the /etc/rc.d/ directory.) invoke mysql.server  http://dev.mysql.com/doc/refman/5.7/en/mysql-server.html



  • I used the last method. 
  • There I implemented the final steps

shell> cp mysql.server /etc/init.d/mysql
shell> chmod +x /etc/init.d/mysql

  • then move to etc folder

shell> cd /etc
shell> ln -s rc.d/init.d   //this didn't work for me sice I had init.d file directly in etc
                             so,create a symbolic link pointing to rc.d/init.d to init.d
                              
 
  • Now the system is installed. It is needed to activate it. Use following in linux
 
 
 
shell> chkconfig --add mysql
 
if required...
 
shell> chkconfig --level 345 mysql on 


 update-rc.d mysql default  => to start


  • Then run mysql

 bin/mysql -u root -p
 


  • following might be a useful link

http://coolestguidesontheplanet.com/start-stop-mysql-from-the-command-line-terminal-osx-linux/ 


MySQL from the Command Line


  • to see whether mysql runs properly 
 test@hsenid-OptiPlex-3020:~$ sudo netstat -tap | grep mysql

tcp6       0      0 [::]:mysql              [::]:*                  LISTEN      813/mysql

  • securing mysql accounts

https://dev.mysql.com/doc/refman/5.6/en/default-privileges.html


  • check the status of mysql service 

/etc/init.d/mysql.server status

service mysql.server status

  • Configure MySQL to launch at any place without going to the bin directory 

add the following to  the bashrc file

MYSQL_HOME=/usr/local/mysql
export PATH=$JAVA_HOME/bin:$ANT_HOME/bin:$MAVEN_HOME/bin:$CATALINA_HOME/bin:$MONGODB_HOME/bin:$MYSQL_HOME/bin:$PATH


and source the bash file 

  •  Find the port, hostname & username of mysql 

start mysql

mysql> select user();
+--------+
| user() |
+--------+
| test@  |
+--------+



mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)


mysql> SHOW VARIABLES WHERE Variable_name = 'hostname';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| hostname      | hsenid |
+---------------+--------+
1 row in set (0.00 sec)

 

  • To change the port in mysql

    check the chosen port is pure =>  sudo netstat -tanp| grep 3337


     Edit etc/mysql/my.cnf file  

 MySQL GUI Tools

  •  Workbench => suitable(current version 6.3)Navicat for Mysql

  • Sequel Pro => suitable (comes with 5 extra languages)

  • HeidiSQL => proj discontinuied since 2010

  • SQLyog => only Windows

  • SQL Wave => 30 day trial version

  • dbForge Studio

  • MyDB studio

 





 

 

 

 








 


 

Thursday, February 25, 2016

Markdown


What is MArkdown....??


   MArkdown is a text to HTML conversion tool.  It allows to write the text on a easy to read and easy to write plain text with some defined syntax and the text would be converted to a structurally valid HTML page. 



In this blog post we are discussing markdown syntax for plain text formatting 


The below screen shot would provide an idea of it.




In the above figure, the first column shows the markdown syntax, while the second column is the corresponding HTML format. The display on the browser is shown in the third column.

 NOTE If you can't do something in markdown, then you can easily switch back and forth between Markdown and HTML within one document


Markdown Syntax

 

 

 

 

 

 

 

 




.Md Files

 

The files written with Markdown syntax are saved on .md files

Markdown formatted documents or .md files are publishable as it is (plain text)

.Md files are not only useful as an easy replacement for HTML documentation, but also for source code version control systems like GitHub. The reason for this is that files can be compared with historical versions.  

A file called README.md is present in GitHub, popular version control system, which contains details if the project (how to install the program, how to use its functions and basically what it does) , written in markdown syntax




 




References

 https://www.shopify.com/partners/blog/86763270-what-is-markdown-syntax