Monday, 3 October 2016

Understanding static for Database interaction and when we should use static

This is very common practice when a developer thinks whether a method should be static or not during development and generally an average developer always use static for Utility data but when it comes to Database call, most of developers confuse when should we use static ????




Understanding of Static

Why we declare static ??

Ans : Those method which consist the data which is common for multiple objects, never changes or infrequent changes.

Q. When static method and when static block gets initialize the data ?
Ans : static method gets initialize the data when we invoke it.Static block initialize the data during class loading time.

static blocks invoked and gets initialize the data after a first call of the mehod and their state never changes after that during program execution.

This is the reason that the data which actually changes shouldn't be accessed through static block. Therefore static blocks used in caching.

For ex- If a static block invoked already and fetch some data from Database and now accessing the data from static block will return same data which actually we got on first call. Therefore if a UI which contains data from DB will result stale data.


So now the next question is :

Q. So we shouldn't use static block for Database interaction ?
Ans : Generally we shouldn't use for database interaction but in some special cases for ex- In caching, we can use static methods to gets stale data from cache and after certain amount of time (caching refresh interval) we can have static method call.

Or the data which never changes in the database can be accessed via static block.

Similarly the Connection to a Database itself never changes annd therefore we can access it via static block or method..


Q. What is the difference between static block and static method ?

Ans : Static block initialize the data during class loading in which the block declared but static method gets initialized the data only when we called the method.

Q. Can't we use static block for read only data ?
Ans : First we need to understand the difference between Read only data and data never changes. Read only data is the data which a particular user or entity has not permisssion. Data never changes is the data which never changes throughout its life. For ex- Age of a person - we can declare static because it never changes throughout the object life cycle and similarly historical data.

Thefore static has no relation with read only data but it can be used to access infrequent or never changing data.


Q. When should use static block ?
Ans : When we need data during application initialization, caching purpose etc

Q. When should we use static method ?
Ans : Define static methods in the following scenarios only:
  1. If you are writing utility classes and they are not supposed to be changed.
  2. If the method is not using any instance variable.
  3. If any operation is not dependent on instance creation.
  4. If there is some code that can easily be shared by all the instance methods, extract that code into a static method.
  5. If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overridden.
In general : One rule-of-thumb: ask yourself "does it make sense to call this method, even if no Obj has been constructed yet?" If so, it should definitely be static.
So in a class Car you might have a method double convertMpgToKpl(double mpg) which would be static, because one might want to know what 35mpg converts to, even if nobody has ever built a Car. But void setMileage(double mpg) (which sets the efficiency of one particular Car) can't be static since it's inconceivable to call the method before any Car has been constructed.
(Btw, the converse isn't always true: you might sometimes have a method which involves two Carobjects, and still want it to be static. E.g. Car theMoreEfficientOf( Car c1, Car c2 ). Although this could be converted to a non-static version, some would argue that since there isn't a "privileged" choice of which Car is more important, you shouldn't force a caller to choose one Car as the object you'll invoke the method on. This situation accounts for a fairly small fraction of all static methods, though.)

Q. Then why we use static method for utility classes ?
Ans : A utility class is one whose behaviour never changes and remain same irrespective of the application state.

Q. Can we use static method for Database call ?

Ans : Yes but if we really need it .For ex- if two or more objects sharing a data accessed from Database then in that case we can access the data through static method.

Q. Why in MVC - service classes shouldn't contain static method if multiple controller classes may need the same method and generally request routing through the controller return stateless data ?
Ans : Straight reason : It violate the service spec because we cant put the service method in in the service interface.

but why we generally dont need because of the following reasons :

(I) you can't replace Service with a mock easily during tests (tools like PowerMock aside)
(ii) static methods do not participate in standard, proxy-based AOP (no transactions, security, custom aspects)
(iii) you can no longer use fancy injection techniques, like injecting HTTP request (request scoped) into singleton scoped services (poor design anyway, but...)
But to be complete, there are also advantages:

static method is actually closer to your intent, Spring beans are very rarely stateful, thus they don't really need an instance to work
static call might be faster


Q. In Transaction which one should use ?
Ans : no one transaction data never access in static...i.e. why I said static always for caching and never for transaction in application infrastructure.


Thursday, 28 July 2016

How to enable a port in Linux

Go to /etc/sysconfig/ and open iptables-config file

add following entries :
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

iptables -A OUTPUT -p tcp --dport 3306 -j ACCEPT

save and close the file

Finally type the following command :
iptables-save

Verify whether port is enabled or not by using following command :

 netstat -lnt | awk '$6 == "LISTEN" && $4 ~ ".3306"'

Tuesday, 28 May 2013

How to send ajax request in Liferay

http://www.liferay.com/community/forums/-/message_boards/message/9582892

Thursday, 9 May 2013

How to develop portal using Liferay

http://mrbool.com/how-to-develop-portal-using-liferay/27987


  • Before completing step(3) in above suggested url steps  MAKE SURE to follow the steps:

1 copy the build.properties file in same location inside sdk plugin folder of liferay
2. Rename to build.<USERNAME(name of the user operating the machine)>.properties
Ex- build.VHV.properties

3. Now change the argument:

app.server.dir=C:\liferay-portal-6.1.1-ce-ga2\tomcat-7.0.27 (path of Liferay tomcat folder)

4. Save
5.  Now install sdk in eclipse


  • Also change database as mysql for liferay after running on localhost

Wednesday, 10 April 2013

Create Desktop application in Java Using XML

http://stackoverflow.com/questions/5213838/java-create-a-gui-with-xml

1.There are some Java-based engines to render XUL (using Swing), but I'm not sure what state they are in.



I have had good experience with ANTFormhttp://antforms.sourceforge.net/.
It generates Java Swing panels from XML. I have used it to build simple GUI apps that execute ANT targets.
Example of the XML declaration:
<antform title="Send Mail" 
    save="properties.txt"
    image="doc/images/testlogo.jpg">
    <label>To send a mail, use the following form. Pick a recipient,
 type a subject and a body...the script will do the rest.</label>
    <selectionProperty label="Recipient: " 
    property="recipient" 
    values="address1@somewhere.com; address2@somewhere.com; address4@somewhere.com" 
    separator=";"/>
    <textProperty label="Subject : " property="subject" />
    <multilineTextProperty label="Message body: "
        property="body"/>
    <booleanProperty label="Send immediately: " property="send"/>
</antform>
Example of what it produces:
enter image description here
share|edit
I used Apache Pivot http://pivot.apache.org/ and really liked it.

Create Desktop application in java using HTML

Using JavaFX
http://docs.oracle.com/javafx/2/swing/overview.htm
http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

Q. Difference between Declarative and Programmatic/Procedural approach?



Ans :
A Procedural language provides "assignments" to keep and/or alter the states while executing through steps. It provides the means to express WHAT a program can do in STEPS (the HOWs) 
A pure declarative programming language has no concept of "assignments" to allow you alter states. It allow the programmers to express what the software can do only. (not the how-part)

I suppose there is always a programmatic approach available since the UI is not made of magic. I guess declarative UIs are just build on the top of programmatic UIs

Declarative
Describe a result and get it via a black box. Examples:
·         yacc
·         Treetop
·         SQL
·         Regular Expressions
·         lex
·         XSLT
·         Markup (XML,HTML etc), troff, CSS, VHDL
Procedural
Describe the algorithm and process steps, at various degrees of abstraction.
·         C, most legacy languages
·         PHP, mostly
·         in some sense all major languages
Object Oriented
·         Tends to be in the procedural category, typically refers to languages that exhibit a hierarchy of types that inherit both methods and state from base types to derived types, but also kind of includes prototype-based languages like JavaScript. Somewhat a separate dimension from the other categories here.
Functional
You left this one out. The opposite of imperative programming, it emphasizes the application of functions without side effects and without mutable state. The declarative systems above exhibit certain aspects of functional programming.
·         Scheme
·         Erlang
·         OCaml
·         Haskell
·         Lisp, depending. (Lisp perhaps deserves its own unique category)
·         Clojure, somewhat
·         Ruby, somewhat less
·         F#
Ex-
Building User Interfaces with JSF: Declarative vs. Programmatic Approach
When we are using JSF tags to build the user interface, we are using the declarative approach. With declarative approach we can visualize the hierarchy of the user interface.
Let’s see how it’s possible to produce the same user interface using both approaches. We will build the following simple panel:
richpanel.png
Here is an example of declarative approach:
  <h:outputText value="I love New York"/>  <h:outputText value="I love the Big Apple and using RichFaces"/> 
Using tags mimics better how the actual user interface will look. In other words, it’s easier to build the user interface using tags.
Here is an example of programmatic approach that produces exactly the same page:
Application application = FacesContext.getCurrentInstance().getApplication(); // create rich:panel HtmlPanel panel = (HtmlPanel)application.createComponent(HtmlPanel.COMPONENT_FAMILY); // set width panel.setStyle("width:400px"); // create h:outpuText HtmlOutputText outputTextHeader = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE); // set h:outputText value outputTextHeader.setValue("I love New York"); // add header facet to rich:panel panel.getFacets().put("header", outputTextHeader); // create h:outputText HtmlOutputText outputText = (HtmlOutputText)application.createComponent(HtmlOutputText.COMPONENT_TYPE); // set h:outputText value outputText.setValue("I love the Big Apple and using RichFaces"); // add h:outputText as a child to rich:panel panel.getChildren().add(outputText);
As you can probably see, when using programmatic approach, it’s more difficult to visualize and understand the hierarchy of the user interface. It basically looks flat.
While most of JSF pages will be built using the declarative style, programmatic approach will almost always be required in a large application. In situations where pages are highly dynamic, the programmatic approach provides power and flexibility. The programmatic approach is also needed when components need to be manipulated inside a Java class (for example managed beans). A common example of this would be using the ‘binding’ attribute and setting some property of the component, for example:
component.setRendered(false);
The programmatic approach is not “bad� as long as you know how and when to use it.
This is just to give you an idea what is the difference between declarative and programmatic approaches to developing user interfaces. Of course JSF provides a declarative approach to build the user interface using JSF and RichFaces tags