Tuesday, 17 April 2012

DataBase Normalization 3NF



DB Normalization is the process of organizing the columns and fields of a relational table to minimize the redundancy,complexity and dependency.


First Normalization Form( 1NF ) :


A table is said to be in  1nf when there are no repeating groups or  no repeating elements present.


Second Normalization Form ( 2NF):


A table is in 2NF if 


 a) the table satisfies the 1NF


 b) no partial dependencies on the primary key(may be compound primary key) [ remove subsets of data that apply to multiple rows of a table and place them in separate tables supplied with foreign key relation ]




Third Normalization Form(3NF)


A table is in 3NF if


a)already satisfied the 2NF 


b) all the attributes should be directly dependent on the primary key



Example:

Let's try  normalizing the following table



1nf :remove the attributes which are redundant class_1,class_2,class_3 in the above table and bring it on as below





 

2nf: No partial dependency of non-key attributes on the primary key and remove subsets of data consolidated as tables with foreign key relation

Considering student_id and Mentor as compound primary key, class is not a full dependent on the composite py key and on segregating will result in the tables as follows 


Add caption





3Nf : all the attributes should be directly dependent on the primary key

In the STUDENT_ID,MENTOR AND MENTOR_ROOM table mentor_room is no way dependent on the student_id  which is the py key so on consolidating 




Thanks for Reading.... cheers  :-) :-)

Saturday, 24 March 2012

Accessing Restful Services from Java Command Line Application`




In this small note, let us see the simple java command line programs to access RESTful Service.REST stands for Representational State Transfer(that is representation of resources is exchanged between client and server).I would be using( accessing :-) ) the Twitter WebService(Messaging API) and the Webservice URI for the same is


http://twitter.com/statuses/public_timeline.xml (for payload in xml format)
http://twitter.com/statuses/public_timeline.json(for payload in json format).



I've tried accessing the service in two simple ways


1.using java.net package
2.using jakarta Commons HttpClient.


1.Using java.net package






2.Using HttpClient : This method of accessing is also simple as first one,but is little sophisticated with the return status code check and granularity.
needed binaries
1.jakarta commons Httpclient 
2.jakarta commons codec
3.jakarta commons logging.






Cheers and Thanks for reading :-) :-)

Wednesday, 25 January 2012

Separate Threads for Process Input and Error Stream ~CMD mode execution using java Runtime ~



I had to run a bat file from cmd(command) mode using java and flatten the output of the cmd mode to a text file for instance and my block of code looks as below and its quite simple as well.I've used the InputStream and ErrorStream to serve the purpose.


and a while loop to iterate and write to text file.




while ((s = stdError.readLine()) != null) {
//put to a file

}




while ((s = stdInput.readLine()) != null) {
//put to a file

}


But,I have ended up in a problem ,the thread seems to have ended up blocking and the bat file(which consists of triggering my web driver test cases)was also blocked.The quick work around was to have two anonymous inner class of Thread and it solved the problem.


This is only for quick work around before I look into non blocking io's.






Thanks for reading..... cheers :) :)



Friday, 20 January 2012

Set With Cardinality



Java's Set interface  has 3  general purpose implementations,with differences in the purpose they serve (sorted,ordered,link mechanisms,bucketing formulae etc)


  1.HashSet
  2.TreeSet
  3.LinkedHashSet 


I was looking for some collection with  virtues like

  •  Indicating the collection has x number of  Y-type objects,n number of z-type objects.
  •  Remove n number of y-type objects from collection.
  •  Add 10 z-type objects to the collection etc
i.e collection which maintains some cardinality ,instead of just holding single instance of unique objects.
com.google.common.collect.HashMultiset Collection serves the purpose and it maintains the cardinality of the objects it is holding and the following is the simple block of code I've used to see how it works.






Thanks for reading.... cheers :-)


Saturday, 7 January 2012

Unobtrusive JavaScript



Unobtrusive JavaScript is the practice of separating out any JavaScript behavior
code from your content structure and presentation.

<input type="button" onclick="validateDate()" id="_btn" />

The above line of  html content of a  button is some ways coupled to the behaviour of onclick.This can be decoupled by using Unobtrusive JavaScript paradigm.

Little modification to the above will set the things right :)

<input type="button"  id="_btn" />.

Instead of embedding the behavior in the markup,this can be segregated by moving the script to script block with in <head> tag else to the bottom of your document considering some performance stuff.

<script>
window.onload=function(){
document.getElementById"(_btn").onclick=function(){
//do the stuff here 
}
}
</script> 



The above lines of code is similar to $(document).ready (function()......) in jquery.This helps in clear seperation of behavior from structure.

Thanks for reading....cheers... :-) :-) :-)


Tuesday, 3 January 2012

Web Driver Grid -forcing to run on a particular client node.



WebDriver is open source tool for automatic web application testing.

WebDriver Grid  concept is like a central hub will be started and the client machines will be
registered to the central hub.The client nodes will have different capabilities (capabilities here 
refer to browser version,os,platform etc).

Grid allows you to :
  • scale by distributing tests on several machines ( parallel execution )
  • manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
  • minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.

The central Webdriver hub will be instantiated using the remote webdriver  as
 webDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
and the  webDriver will be supplied with capability say for eg

capability.setBrowserName(“firefox ); 
capability.setPlatform(“LINUX”);  
capability.setVersion(“3.6”);
so ,the remote web driver will look for the nodes satisfying the supplied capability and run the tests on the particular node.So,if there are multiple nodes satisfying the capability and the pick is random

Let me brief how to set up a Gridstep 1: Start the hub using the following command on the central machine(say X).
java -jar selenium-server-standalone-2.14.0.jar -role hub
step 2: Register the node machine to the hub started in step 1
java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register
The above are two steps to start a hub and register a client node.So there can be many client nodes listening to a single hub.
Step 3: Stepping to the test cases,the webDriver should be instantiated using the RemoteWebDriver on your hub.
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability);
driver.get("www.google.com");
driver.findElement(By.id(.....)) and the test cases go on.
So,if there are n node machines listening to the hub and when you execute the test case,the hub will pick the node which satisfies the capability set in the step 3 and run the tests on the particular machine.The client nodes capability can be passed along with the registering command as follows
java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register  -browser "browserName=firefox,version=7.0,platform=windows" 

I was needed to develop a web app using this grid concept,so the client can access a url and start the test on the particular machine.The trick here to solve was if there are n nodes registered to the hub with same capabilities ,the hub may delegate the test case execution to any of the clients.So,the real user who started the test may not see it running on his machine,but may be as a surprise to some other client machine where the tests get executed launching the browser.

The way I used, to solve this is by customizing the browser version to the ip of the client machine,so the capability from the client machine will be unique,so hub will have to delegate the execution of testcase to the same machine from where the request is from.
I have done little dos scripting to identify the ip config of the machine while registering to the hub.
SETLOCAL
set host=%~1
set ip=
if "%host%"=="" ( for /f "tokens=2,* delims=:. " %%a in ('"ipconfig|find "IPv4 Address""') do set ip=%%b
) ELSE ( for /f "tokens=2 delims=[]" %%a in ('"ping /a /n 1 %host%|find "Pinging" 2>NUL"') do set ip=%%a)

java -jar selenium-server-standalone-2.14.0.jar -role node  -hub http://X:4444/grid/register -Dwebdriver.chrome.driver="chromedriver.exe" -browser "browserName=firefox,version=%ip%,platform=windows" 
and while instantiating the remote webdriver in the hub,I will be setting the capability as 
below
capability.setBrowserName(“firefox ); 
capability.setPlatform(“windows”);  
capability.setVersion(request.getRemoteHost());
You can download the required  selenium-server-standalone-*.jar from http://code.google.com/p/selenium/downloads/list.
Cheers..thnx for Reading :)

Wednesday, 9 November 2011

Communications Link Failure Due to Underlying Exception



I was using  Spring 3.x along with mySql for some of my Web app,after considerable idle time (server is up and running but no request hits to the app say for around 6 hrs)it ended up in the following exception



type Exception report


message 


description The server encountered an internal error () that prevented it from fulfilling this request.


exception 


org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: StatementCallback; SQL [SELECT * from TABLE_PARENT]; Communications link failure due to underlying exception: 


** BEGIN NESTED EXCEPTION ** 


java.net.SocketException
MESSAGE: Software caused connection abort: socket write error


STACKTRACE:


java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2744)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at org.apache.commons.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:440)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:395)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:455)
at com.pm.dboard.dao.PMDBoardDAO.fetchDomains(PMDBoardDAO.java:77)
at com.pm.dboard.service.PMDboardService.fetchDomains(PMDboardService.java:23)
at com.pm.dboard.controller.DBoardController.getDashBoard(DBoardController.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:140)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)




This error was solved after adding the following properties to the datasource bean in the spring's dispatcher-servlet.xml



<property name="testOnBorrow" value="true"/>
<property name="validationQuery" value="SELECT 1"/>


so my datasource bean will look like





<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/xxxxx" />
<property name="username" value="durga" />
<property name="password" value="s3cret" />
<property name="testOnBorrow" value="true"/>
               <property name="validationQuery" value="SELECT 1"/>
</bean>


I took considerable amount of time to figure this out and guess this post may come in handy .


Some more configurable properties at  http://commons.apache.org/dbcp/configuration.html


Thanks for reading.
cheers ... :-)