Showing posts with label Interview. Show all posts
Showing posts with label Interview. Show all posts

Monday, October 06, 2014

JSF 2 introduction

faces-config.xml contains
  • definition of Managed beans, 
  • defines navigation rules (map return conditions to results page), 
  • register validators, 
  • declare locates, 
  • inject bean properties, 

Sample

 <faces-config … version="2.2">
 <managed-bean>
 <managed-bean-name>messageHandler</managed-bean-name>
 <managed-bean-class>
 coreservlets.SimpleController2
 </managed-bean-class>
 <managed-bean-scope>request</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
 <from-view-id>/starting-page.xhtml</from-view-id>
 <navigation-case>
 <from-outcome>return-value-1</from-outcome>
 <to-view-id>/result-page-1.xhtml</to-view-id>
 </navigation-case>
 <navigation-case>
 <from-outcome>return-value-2</from-outcome>
 <to-view-id>/result-page-2.xhtml</to-view-id>
 </navigation-case>

View

 <!DOCTYPE … >
 <html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://xmlns.jcp.org/jsf/html">
 <h:head><title>JSF 2: Basic Navigation Rules</title>
 …
 </h:head>
 <h:body>
 …
 <h:form>
 Your message:
 <h:inputText value="#{simpleController.message}"/>
 <br/>
 <h:commandButton value="Show Results"
 action="#{simpleController.doNavigation}"/>
 </h:form>
 …
 </h:body></html>
 @ManagedBean
 public class SimpleController {
  private String message="";
 // getMessage and setMessage
 public String doNavigation() {
 if (message.trim().length() < 2) {
 return("too-short");
 } else {
 String[] results =
 { "page1", "page2", "page3" };
 return(RandomUtils.randomElement(results));
 }
 }
 }

Managed beans have following scope
  • request
  • application
  • session
  • flow
  • none
Other JSF info
  • web.xml declars extension of url *.jsf
  • You will enter URL as xyz.jsf but actual file name would be xyz.xhtml
  • view technology is facelets
  • JSF has integrated AJAX support (f:ajax tag) and can be thought of as an alternative to jQuery and Dojo etc
  • Event handling
  • built in capabilities for validation
  • Page templating
JSF life cycle
  1. Restore view 
  2. Apply request values; process events 
  3. Process validations; process events 
  4. Update model values; process events
  5. Invoke application; process events
  6. Render response

Friday, October 03, 2014

Typical JEE development process, stack

Agile development using SCRUM methodogy
In the Scrum method of agile software development, work is confined to a regular, repeatable work cycle, known as a sprint or iteration. In by-the-book Scrum, a sprint is 30 days long, but many teams prefer shorter sprints, such as one-week, two-week, or three-week sprints. But how long each sprint lasts is something for the team to decide, who must weigh the advantages or disadvantages of a longer or shorter sprint for their specific development environment. The important thing is that a sprint is a consistent duration.

During each sprint, a team creates a shippable product, no matter how basic that product is. Working within the boundaries of such an accelerated timeframe, the team would only be able to build the most essential functionality. However, placing an emphasis on working code motivates the Product Owner to prioritize a release’s most essential features, encourages developers to focus on short-term goals, and gives customers a tangible, empirically based view of progress. Because a release requires many sprints for satisfactory completion, each iteration of work builds on the previous. This is why Scrum is described as “iterative” and “incremental.”

Every sprint begins with the sprint planning meeting, in which the Product Owner and the team discuss which stories will be moved from the product backlog into the sprint backlog. It is the responsibility of the Product Owner to determine what work the team will do, while the team retains the autonomy to decide how the work gets done. Once the team commits to the work, the Product Owner cannot add more work, alter course mid-sprint, or micromanage.

During the sprint, teams check in at the daily Scrum meeting, also called the daily standup. This time-boxed meeting gives teams a chance to update project status, discuss solutions to challenges, and broadcast progress to the Product Owner (who may only observe or answer the team’s questions).

Just as every sprint begins with the sprint planning meeting, the sprint concludes with the sprint review meeting, in which the team presents its work to the Product Owner. During this meeting, the Product Owner determines if the team’s work has met its acceptance criteria. If a single criterion is not met, the work is rejected as incomplete. If it satisfies the established criteria, then the team is awarded the full number of points.

Because certain sprints are hugely successful and others less than ideal, a team also gathers at the end of each sprint to share what worked, what didn’t, and how processes could be improved. This meeting is called the sprint retrospective meeting.


  1. Architecture and design scenario
    1. architecture concepts
    2. architecture definition
    3. proof of concepts
  2. Setting up development environment
  3. data modelling
  4. analysis and design
    1. class diagrams - inheritance, association, aggregation, composition
    2. usecase diagrams
    3. deployment diagrams
  5. development - reusable components, continuous integration, build automation
  6. WebServices
    1. SOAP web services - stacks, security, 
    2. Restful - stacks, security
    3. Code first Vs contract(WSDL) first approaches
  7. xml binding
  8. Stateless session beans
  9. design patterns
  10. ORM - JPA - Hibernate
  11. Continuous integration (CI)
    1. Developers check out code into their private workspaces.
    2. When done, the commit changes to the repository.
    3. The CI server monitors the repository and checks out changes when they occur.
    4. The CI server builds the system and runs unit and integration tests.
    5. The CI server releases deployable artefacts for testing.
    6. The CI server assigns a build label to the version of the code it just built.
    7. The CI server informs the team of the successful build.
    8. If the build or tests fail, the CI server alerts the team.
    9. The team fix the issue at the earliest opportunity.
    10. Continue to continually integrate and test throughout the project.
    11. Tools used for CI
      1. Deployed jenkins.war in Tomcat
      2. Configure ANT and Maven in Jenkins
      3. Setup Jenkins jobs to build projects
      4. http://www.vogella.com/tutorials/Jenkins/article.html
  12. build automation

Thursday, October 02, 2014

Transaction management - Spring Framework

Local vs. Global Transactions
Local transactions are specific to a single transactional resource like a JDBC connection, whereas global transactions can span multiple transactional resources like transaction in a distributed system.

Local transaction management can be useful in a centralized computing environment where application components and resources are located at a single site, and transaction management only involves a local data manager running on a single machine. Local transactions are easier to be implemented.

Global transaction management is required in a distributed computing environment where all the resources are distributed across multiple systems. In such a case transaction management needs to be done both at local and global levels. A distributed or a global transaction is executed across multiple systems, and its execution requires coordination between the global transaction management system and all the local data managers of all the involved systems.

Programmatic transaction management: This means that you have manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.

Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

A transaction attribute may have one of the following values:
Required
RequiresNew
Mandatory
NotSupported
Supports
Never

Transaction Isolation level
DEFAULT This is the default isolation level.
READ_COMMITTED Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
READ_UNCOMMITTED Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
REPEATABLE_READ Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
SERIALIZABLE Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.

Spring.xml for hibernate
<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- Enable Annotation based Declarative Transaction Management -->
    <tx:annotation-driven proxy-target-class="true"
        transaction-manager="transactionManager" />

    <!-- Creating TransactionManager Bean, since JDBC we are creating of type
        DataSourceTransactionManager -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
   
    <!-- MySQL DB DataSource -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/TestDB" />
        <property name="username" value="pankaj" />
        <property name="password" value="pankaj123" />
    </bean>

    <bean id="customerDAO" class="com.journaldev.spring.jdbc.dao.CustomerDAOImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="customerManager" class="com.journaldev.spring.jdbc.service.CustomerManagerImpl">
        <property name="customerDAO" ref="customerDAO"></property>
    </bean>

</beans>



import org.springframework.transaction.annotation.Transactional;

import com.journaldev.spring.jdbc.dao.CustomerDAO;
import com.journaldev.spring.jdbc.model.Customer;

public class CustomerManagerImpl implements CustomerManager {

    private CustomerDAO customerDAO;

    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }

    @Override
    @Transactional
    public void createCustomer(Customer cust) {
        customerDAO.create(cust);
    }

}



Spring transactions client driver

package com.journaldev.spring.jdbc.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.journaldev.spring.jdbc.model.Address;
import com.journaldev.spring.jdbc.model.Customer;
import com.journaldev.spring.jdbc.service.CustomerManager;
import com.journaldev.spring.jdbc.service.CustomerManagerImpl;

public class TransactionManagerMain {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");

        CustomerManager customerManager = ctx.getBean("customerManager",
                CustomerManagerImpl.class);

        Customer cust = createDummyCustomer();
        customerManager.createCustomer(cust);

        ctx.close();
    }

    private static Customer createDummyCustomer() {
        Customer customer = new Customer();
        customer.setId(2);
        customer.setName("Pankaj");
        Address address = new Address();
        address.setId(2);
        address.setCountry("India");
        // setting value more than 20 chars, so that SQLException occurs
        address.setAddress("Albany Dr, San Jose, CA 95129");
        customer.setAddress(address);
        return customer;
    }

}

Annotation style transaction
To use the annotation style transaction management all you have to do is to add a 3 simple bean configuration in your xml file i.e:

<context:annotation-config/>: Tells Spring framework to read @Transactional annotation

<tx:annotation-driven/>: Automatically adds transaction support which eventually wraps your code in transaction scope
Initializing DataSourceTransactionManager bean


Example:
@Transactional
public class AnnotatedUserDao implements IUserDao {

Example for read only:
@Transactional(readOnly = true)
public User selectUser(int uid) {

Programmatic transactions in Spring
public void deleteUser(final int uid) {
 DefaultTransactionDefinition paramTransactionDefinition = new    DefaultTransactionDefinition();

  TransactionStatus status=platformTransactionManager.getTransaction(paramTransactionDefinition );
try{
  String delQuery = "delete from users where id = ?";
  jdbcTemplate.update(delQuery, new Object[]{uid});
  platformTransactionManager.commit(status);
}catch (Exception e) {
  platformTransactionManager.rollback(status);
}



Wednesday, October 01, 2014

JRockit JVM Garbage Collection

Following are 3 GC algorithms available in JRockit
  1. throughput(Transactions per second): Optimizes for maximum throughput -- default
    1. moves most work to GC pauses
    2. Least GC overhead
    3. Application threads do as little as possible
  2. pausetime: Optimizes for short and even pause times
    1. move work out of GC pauses
    2. More overall GC overhead
    3. Application threads do more work
  3. deterministic: Optimizes for very short and deterministic pause times (requires Oracle JRockit Real Time
JRockit mission control
  1. monitor health and performance of JVM in production
  2. Performance tuning, diagnostics
  3. Profiling - detect hot methods

JRockit flight recorder
  1. Start JVM with recorder options to dump to a .jfr file
  2. Use mission control GUI to analyze the file


Java Application Tuning

Tuning objectives

Tools used
Jprofiler
JRockit mission control
Wily introscope
Fluke
Splunk
Wireshark

testing - load runner, winruuner, junit, jmeter

Measurement
CPU (ideally should not cross more than 50-75%)
Memory
IO usage
Response time
Latency
Throughput

Application level tuning

Java language tuning
Using hashmaps vs hashtable, array vs vector,
Use StringBuffer to Concatenate Strings
Assign null to Variables That Are No Longer Needed
Declare Constants as static final
Avoid Finalizers - Adding finalizers to code makes the garbage collector more expensive and unpredictable
Synchronize Only When Necessary

EJB

JNDI

Application Server
Precompile jsps

OS
File Descriptors
set rlim_fd_max = 8192
Verify this hard limit by using the following command:
ulimit -a -H
Once the above hard limit is set, increase the value of this property explicitly (up to this limit)using the following command:
ulimit -n 8192
Verify this limit by using the following command:
ulimit –a

DISK IO settings

TCP/IP settings

Virtual memory settings

Run TOP command in Linux to check CPU usage.
Run VMSTAT, SAR, PRSTAT command to get more information on CPU, memory usage and possible blocking.
Enable the trace file before running your queries,then check the trace file using tkprof create output file.
According to explain plan check the elapsed time for each query,then tune them respectively.

What is the use of iostat/vmstat/netstat command in Linux?
Iostat – reports on terminal, disk and tape I/O activity.
Vmstat – reports on virtual memory statistics for processes, disk, tape and CPU activity.
Netstat – reports on the contents of network data structures.

Caching

JVM tuning/Garbage collection
best GC algorithm, especially for web server applications, is the Concurrent Mark Sweep GC
operation mode to server

-Xms, -Xmx
-XX:PermSize=256 m -XX:MaxPermSize=256m --> this option is not there in new HotSpot JVM
Log GC -Xloggc:$CATALINA_BASE/logs/gc.log -XX:+PrintGCDetails
-XX:+PrintGCDateStamps
HeapDump on Out of memory - -Xloggc:$CATALINA_BASE/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps

jvm - space ratios,
gc type – CMS (concurrent mark and sweep, parallel), G1 (this is new in HotSpot)

In the event CPU usage rate is high - If TPS is low but CPU usage rate is high, this is likely to result from inefficient implementation. In this case, you should find out the location of bottlenecks by using a profiler. You can analyze this by using jvisuavm, TPTP of Eclipse or JProbe.
     
JMS tuning
quota - space for jms messages at queue or jms server level. if quota limit is reached, exception is raised
send timeout - blocking producers when quota limits are reached
paging messages
message buffer size - amount of memory stored in memory before messages are paged out
message compression
expired messages

JDBC code tuning
Use prepared statements. Use parametrized SQL.
Tune the SQL to minimize the data returned (e.g. not 'SELECT *').
Minimize transaction conflicts (e.g. locked rows).
Use connection pooling.
Try to combine queries and batch updates.
Use stored procedures.
Cache data to avoid repeated queries.
Close resources (Connections, Statements, ResultSets) when finished with.
Select the fastest JDBC driver.

JDBC parameter tuning
Increasing Performance with the Statement Cache
Connection Testing Options for a Data Source - test frequency/
Minimized Connection Test Delay After Database Connectivity Loss
Minimized Connection Request Delay After Connection Test Failures
Minimized Connection Request Delays After Loss of DBMS Connectivity
Minimizing Connection Request Delay with Seconds to Trust an Idle Pool Connection
A leaked connection is a connection that was not properly returned to the connection pool in the data source. To automatically recover leaked connections, you can specify a value for Inactive Connection Timeout on the JDBC Data Source

Threadpool/work manager
Contention/locks
Minimize I/O
Parellelization/concurrency

Hibernate tuning
property name="show_sql"

Tip 1 - Reduce primary key generation overhead
Tip 2 - Use JDBC batch inserts/updates
Tip 3 - Periodically flush and clear the Hibernate session
entityManager.flush();
entityManager.clear();

Tip 4 - Reduce Hibernate dirty-checking overhead
@Transactional(readOnly=true)
public void someBusinessMethod() {
}

Tip 5 - Search for 'bad' query plans - full table scans and full cartesian joins
Tip 6 - Use the second-level and query caches
Define lazy loading as the preferred association loading strategy,
Set ReadOnly to "true" on Queries and Criteria, when objects returned will never be modified.

Cache – 1st level = Session, 2nd = Hibernate, 3rd = Query results cache

SQL Tuning
How to see execution plan for an SQL
SQL> set autotrace onSQL> select count(*) from table_name;
SQL> explain plan set statement_id = '111' for select * from tableName;SQL> select * from plan_table;

How to find out cpu time for an SQL?
SQL> TIMING START select_empSQL> SELECT * FROM employee ; SQL> TIMING SHOW select_emp timing for: select_emp real: 1760
use indexes, improve h/w, caching, tune it, hints,
use explain plan or sql analyzer to see the execution path
add hints
rewrite sql
replace sql with pl/sql

How would you improve a poor performing query?

How do you tune a sql statement?

understand row level lock and how it migrates to table level lock

soa performance tuning - dehydrate, db persistence
http://docs.oracle.com/cd/E25178_01/core.1111/e10108/bpel.htm

Oracle HTTP server tuning
http://docs.oracle.com/cd/E25178_01/core.1111/e10108/http.htm#ASPER99006

Java Memory leaks

Causes of memory leaks in Java
The four typical causes of memory leaks in a Java program are:
  1. Unknown or unwanted object references: These objects are no longer needed, but the garbage collector can not reclaim the memory because another object still refers to it.
  1. Long-living (static) objects: These objects stay in the memory for the application's full lifetime. Objects tagged to the session may also have the same lifetime as the session, which is created per user and remains until the user logs out of the application. Example: Listeners, Static Collection classes, Connections, JNI
  1. Failure to clean up or free native system resources: Native system resources are resources allocated by a function external to Java, typically native code written in C or C++. Java Native Interface (JNI) APIs are used to embed native libraries/code into Java code.
  1. Bugs in the JDK or third-party libraries: Bugs in various versions of the JDK or in the Abstract Window Toolkit and Swing packages can cause memory leaks.

Symptoms
  • Causes Out of memory after some time
  • GC keeps reclaiming lesser heap each time (memory used for long lived objects increase over time)
  • Causes frequent full GC
  • Available free heap decreases each time
  • Response time decreases

Detection of  Memory leaks
  • Add JVM args to collect GC metrics --> verbose:gc,-XX:+PrintGCTimeStamps,-XX:+PrintGCDetails
  • Monitor heap. If heap size increases after each full GC, this means a memory leak.
  • Use HeapDumpOnOutOfMemoryError to dump heal to a file
  • Use JMAP to dump heap and analyze using visualVM
    • jmap -dump:file=xyz.log PID
  • Use profilers

Other technique to detect memory leak
So Java Memory Leaks occur when objects still have a GC root reference, but are not actually used anymore. Those “Loitering Objects” stay around for the whole life of the JVM. If the application is creating those “dead objects” on and on, the memory will be filled up and eventually result in a java.lang.OutOfMemoryError. Typical causes are static collections, which are used as a kind of cache. Usually objects are added, but never removed (Let’s face it: How often have you used add() and put() and how often used remove() methods?). Because the objects are referenced by the static collection, they cannot be freed up anymore, as the collection has a GC root reference via the classloader.

Examples of memory leaks
Not closing resultset and statement objects explicitly…

JVM Performance Tuning

On a multi-processor machine with 8 CPUs, here are typical settings:

On a 8 processor
java 1.6 update 45

 Heap Size: -Xms2g -Xmx2g  
 Perm Size: -XX: PermSize=512m -XX: MaxPermSize=512m  
 Newgen Size(Fixed Size) : -Xmn1g  
 Garbage Collector(Parallel): -XX:+UseParallelOldGC -XX:ParallelGCThreads=8 -XX:LargePageSizeInBytes=4m -XX:+UseCompressedOops  
 GC Statistics: -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps - Xloggc:/appserver/Weblogic/admin/360control_perf_domain/mserver/360control_perf_domain/logs/gc_360control.$(date +"%Y-%m-%d").log  


http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

http://www.petefreitag.com/articles/gctuning/
http://java.sun.com/docs/hotspot/gc1.4.2/faq.html
http://java.sun.com/developer/technicalArticles/Programming/turbo/


The garbage collector first performs a task called marking. The garbage collector traverses the application graph, starting with the root objects; those are objects that are represented by all active stack frames and all the static variables loaded into the system. Each object the garbage collector meets is marked as being used, and will not be deleted in the sweeping stage.

The sweeping stage is where the deletion of objects take place. There are many ways to delete an object: The traditional C way was to mark the space as free, and let the allocator methods use complex data structures to search the memory for the required free space. This was later improved by providing a defragmenting system which compacted memory by moving objects closer to each other, removing any fragments of free space and therefore allowing allocation to be much faster:

For the last trick to be possible a new idea was introduced in garbage collected languages: even though objects are represented by references, much like in C, they don't really reference their real memory location. Instead, they refer to a location in a dictionary which keeps track of where the object is at any moment.

Fortunately for us – but unfortunately for these garbage collection algorithms – our servers and personal computers got faster (and multiple) processors and bigger memory capacities. Compacting memory areas this large often was very taxing on the application, especially considering that when doing that, the whole application had to freeze due to the changes in the virtual memory map. Fortunately for us though, some smart people improved those algorithms in three ways: concurrency, parallelization and generational collection.


Generational garbage collection

In any application, objects could be categorized according to their life-line. Some objects are short-lived, such as most local variables, and some are long-lived such as the backbone of the application. The thought about generational garbage collection was made possible with the understanding that in an application's lifetime, most instantiated objects are short-lived, and that there are few connections between long-lived objects to short-lived objects.

In order to take advantage of this information, the memory space is divided to two sections: young generation and old generation. In Java, the long-lived objects are further divided again to permanent objects and old generation objects. Permanent objects are usually objects the Java VM itself created for caching like code, reflection information etc. Old generation objects are objects that survived a few collections in the young generation area.

Since we know that objects in the young generation memory space become garbage early, we collect that area frequently while leaving the old generation's memory space to be collected in larger intervals. The young generation memory space is much smaller, thus having shorter collection times.

An additional advantage to the knowledge that objects die quickly in this area, we can also skip the compacting step and do something else called copying. This means that instead of seeking free areas (by seeking the areas marked as unused after the marking step), we copy the live objects from one young generation area to another young generation area. The originating area is called the From area, and the target area is called the To area, and after the copying is completed the roles switch: the From becomes the To, and the To becomes the From.

In addition, the Java VM splits the young generation to three areas, by adding an area called Eden which is where all objects are allocated into. To my understanding this is done to make allocation faster by always having the allocator reference to the beginning of Eden after a collection.

By using the copying method, garbage collection achieves defragmentation without seeking for dead memory blocks. However, this method proves itself to be more efficient in areas where most objects are garbage, so it is not a good approach to take on the old generation memory area. Indeed, that area is still collected using the compacting algorithm – but now, thanks to the separation of young and old generations, it is done in much larger intervals.


OutOfMemoryError (on HeapSpace, PermGen) and JVM Performance Tuning
HeapSpace OutOfMemoryError:


"Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"

Most of you would have encountered this error before. This means JVM's usage of heap space exceeded what you specified in -Xmx vm argument or the default max heap size (if you did not specify one).

When you get this error, before you increase your -Xmx value, make sure to profile your application and see whether the usage is justified or if there is any memory leak. See below section on profiling tools.

PermGen OutOfMemoryError:

"java.lang.OutOfMemoryError: PermGen space"

Sometimes, you would also get a different kind of out of memory error. In JVM heap, there are different generations - Young, Tenured and PermGen. Young and tenured generations are where the regular objects from your application are stored. Size of young + tenured generation is controlled by -Xmx variable you saw above. PermGen is where JVM stores string pool, metadata about your application's classes etc.,

This error would normally happen only if your application loads a large number of classes, but is not unloading for some reason, which could be either legitimate or a memory leak. Also, make sure to check if your application heavily uses String.intern(), which could also contribute to this error.

Monday, August 25, 2014

Oracle PL/SQL functions and procedures

PL/SQL Function

DECLARE
   a number;
   b number;
   c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
    z number;
BEGIN
   IF x > y THEN
      z:= x;
   ELSE
      Z:= y;
   END IF;

   RETURN z;
EXCEPTION
   WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.PUT_LINE('No such employee: ' || Emp_number);

END;

Calling a function from another function/procedure
BEGIN
   a:= 23;
   b:= 45;
   c := findMax(a, b);
   dbms_output.put_line(' Maximum of (23,45): ' || c);

END;

PL/SQL Procedure

DECLARE
   a number;
   b number;
   c number;

PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
   IF x < y THEN
      z:= x;
   ELSE
      z:= y;
   END IF;
EXCEPTION
   WHEN NO_DATA_FOUND THEN
      DBMS_OUTPUT.PUT_LINE('No such employee: ' || Emp_number);
END;

Calling a procedure
  1. From the SQL prompt.
 EXECUTE [or EXEC] procedure_name;
  1. Within another procedure – simply use the procedure name.
 procedure_name;

Friday, August 22, 2014

Rule Engine

Drools - steps to use it in a typical program
  1. create working memory
  2. asset objects (insert into working memory)
  3. fire all rules
  4. retrieve objects
Rule attributes
- name
- group
- description
- priority

Rules
  1. Production rules (inference rules) - if x, then y
  2. Reaction Riles - wait for a set of events
  3. Stateful
Rete algorithm (Forward Chaining)
  1. iterate through antecedants
  2. each time an antecedant is matched, add knowledge of consequence
  3. do this until goal is reached
Guvnor - Business rules management system

RESTful WebServices - API Keys


Http methods
Get (read), header, post(create), put(update), trace, delete(delete)

API keys are used for
  • Limit API usage, security

API flow sequence
  • Log in to PayPal developer site - register your application by logging into the PayPal Developer site using a PayPal account, and by going to the Applications tab. 
  • PayPal provides a client_id and secret - You will be issued a set of test credentials (‘client_id’ and ‘secret’) that you can use to authenticate your API calls using the OAuth 2.0 protocol.
  • Client calls /token endpoint with client_id and secret_key - You then obtain an access token for your application by sending a request to the ‘/v1/oauth2/token’ endpoint. You need to authenticate your access token request (using HTTP Basic Auth) with your application credentials (client_id and secret_key) obtained as described above. The ‘client_id’ and ‘secret’ becomes your user-id and password in HTTP Basic Auth.  If you’re using cURL, you can pass the client_id and secret as -u ":"
  • PayPal returns the access token - PayPal, acting as the “authorization server”, verifies your application credentials and returns an access token. The specific kind of access token that PayPal provides is a “Bearer Token”. PayPal also provides the token type in the response, which indicates the type as Bearer.
  • Client calls PayPal Rest API with access token - When you make the API calls, make request by adding the access token in the ‘Authorization’ header using the following syntax (as defined in the OAuth 2.0 protocol):
Authorization: {tokenType} {accessToken}
    Example: Authorization: Bearer EEwJ6tF9x5...4599F

    • Access token validity and expiration - PayPal-issued access tokens can be used to access all the REST API endpoints. These tokens have a finite lifetime and you must write code to detect when an access token expires. You can do this either by keeping track of the ‘expires_in’ value returned in the response from the token request (the value is expressed in seconds), or handle the error response (401 Unauthorized) from the API endpoint when an expired token is detected.
    link: https://developer.paypal.com/docs/integration/direct/paypal-oauth2/

    Common Web Security vulnerabilities


      Principle to remember: Filter input escape output
      SQL injection (user input is not filtered for special characters) 

      Example:
      1. A form is displayed on a web application that contains some text fields
      2. These text fields data are not validated on the client/server side so it lets any data in
      3. The data entered is directly used in building an SQL query
      4. A malicious user enters some bad data in the text field which contains an executable SQL statement to corrupt the database or display unintended results
      5. So, instead of executing "SELECT * FROM products WHERE id_product=$id_product", the application executes "SELECT * FROM products WHERE id_product=$id_product UNION Select * from USERS"
      Cross Site Scripting (code injection by malicious to pages used by real users) - this happens when developers do not check the input and let arbitrary data in to the database. Then, this arbitrary data (such as a javascript embedded in script tag)  is displayed on the web page. When a user performs an action on the web page, sensitive data such as sessionId or personal data is sent unknowingly to the malicious web site. This is very similar to SQL injection but instead of sending SQLs in text fields, a text containing script tag is sent to the server side to be stored in the database only to be displayed later.

      Example:
      1. A real website displays a form containing name etc.
      2. Since there is a bug in the server side code, it lets any text to be entered in the text field
      3. A malicious user enters a text which contains some javascript embedded in script tags
      4. The server side application persists this data into database
      5. The application has a web page that displays a list of all names
      6. When a real user retrieves this webpage, it displays the names but also executes javascript
      7. When the user submits or performs some action on this webpage, it sends cookies etc to a malicious website.
      CORS - Cross Origin Resource Sharing

      CSRF - Cross Site Request Forgery
      This attack happens if a user does not logout from a good website. Say for example, a good website transfers money using this URL: amount=100.00&routingNumber=1234&account=9876

      A evil website may have HTML code with hidden input fields for amount, routing number and account. It may have an image that says "Win MoneY" which when clicked will submit the hidden form fields to the URL for the good website. Since the user is already authenticated and forgot to logoff, the transaction will be executed without the user knowing about it.

      One solution is to use the Synchronizer Token Pattern. This solution is to ensure that each request requires, in addition to our session cookie, a randomly generated token as an HTTP parameter. When a request is submitted, the server must look up the expected value for the parameter and compare it against the actual value in the request. If the values do not match, the request should fail.

      We can relax the expectations to only require the token for each HTTP request that updates state. This can be safely done since the same origin policy ensures the evil site cannot read the response. Additionally, we do not want to include the random token in HTTP GET as this can cause the tokens to be leaked. So, the new URL looks like this:
      amount=100.00&routingNumber=1234&account=9876&_csrf=

      You will notice that we added the _csrf parameter with a random value. Now the evil website will not be able to guess the correct value for the _csrf parameter (which must be explicitly provided on the evil website) and the transfer will fail when the server compares the actual token to the expected token.

      https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html

      If you are only creating a service that is used by non-browser clients, you will likely want to disable CSRF protection.

      Improper identity and access management
      - forget password mgmt features

      Not implementing API keys for clients

      Denial of service
      - flooding a website/webservice with requests so that legitimate users can't use it

      Tuesday, July 15, 2014

      SOAP WebServices

      SOAP
      • port type is interface
      • binding is implementation

      Friday, February 01, 2013

      GoF Design Patterns



      Creational Patterns

      1.  Abstract Factory
      2.  Builder
      3.  Factory Method
      4.  Prototype
      5.  Singleton

      Structural Patterns

      1.  Adapter
      2.  Bridge
      3.  Composite
      4.  Decorator
      5.  Façade
      6.  Flyweight
      7.  Proxy

      Behavioral Patterns

      1.  Chain of Responsibility
      2.  Command
      3.  Interpreter
      4.  Iterator
      5.  Mediator
      6.  Memento
      7.  Observer
      8.  State
      9.  Strategy
      10.  Template Method
      11.  Visitor

      Friday, October 29, 2010

      General technical interview questions

      What is the Unix command for listening to ports?
      netstat -an (local, remote, state)

      What is "having" in reference to databases?
      when using group by, to aggregate

      What are the two flavors of Unix?
      i have used mostly solaris and linux a little..but in general, there are system V and BSD

      Give an overview of normalization
      Normalization is a physical data modelling process where we can avoid update-delete anamolies in db design.

      What is denormalization and why would you use it?
      some times, we may use denormalization to access data faster for aggregation, material views

      What are the ACID properties of a transaction?
      atomicity(all or nothing)
      consistency (takes db from one consistent state to other)
      isolaton (no concurrent updates to data)
      durability(once txn commits, it is durable and can be restored)

      How would you improve a poor performing query?
      use indexes, improve h/w, caching, tune it, hints,

      Write an SQL query that returns department name, and the number of employees in each department (it's a single table query)
      select dept_name, count(*) from dept group by dept_name

      How do you tune a sql statement?
      use explain plan or sql analyzer to see the execution path
      add hints
      rewrite sql
      replace sql with pl/sql

      What is a prepared statement?
      this is compiled and runs faster

      What is a hash join?
      in memory join

      What is a nested loop join?
      two for loops

      What is a merge sort join?
      you can optimize a join by eliminating some row according to sort order

      understand row level lock and how it migrates to table level lock

      understand user security authentication in unix and location of password files
      /etc/passwd

      what is chmode command in unix?
      file permissions....421 (user group other)

      give a jdbc example in a class
      load driver, drivermanager.getconnection, conn.preparestatement, statement.execute(exeUpdate)

      what is the difference between vector and array and what is best in implementation?
      vector can grow, holds objects, threadsafe

      what is an inode and what is missing (unix)
      inode contains file metadata

      where are passwords stored in unix?
      /etc/passwd

      What is a stored procedure?
      sql block

      about himself, how long has he been with the company, what he does and who does he report to general question. talk about general architecture related work, specific contributions to the project, achievements, with information about application and database tuning, J2EE, industry standards and best practices, common design patterns, software development methodology (like releases, bug fixes, marketing requirements, technical specifications, application-business-data modeling etc)

      is your current role and some of the projects you have worked on?

      how did you hear about xxx?

      what are some of the benefits of data virtualization?

      1) primarily to bring together data from disparate data sources (such as legacy apps, databases, xml-flat files, other datasources) and provide real time view of the data in a much faster way than db warehouse.
      2) provide an abstraction layer on top of real datasources for application developers to integrate their apps)

      how long does it take to implement a change in the data warehouse and what is the process followed?
      generally many days...need to change several batch jobs that may run during night and follow a enterprise configuration change procedure (depending on corp IT change policies)

      how long did it took for developing an etl process?
      generally several days...need to find what data needs to extracted by gathering business requirements and identify where data exists.

      what is the process followed to deploy code to production?
      follow corporate change control procedures...typically involves build, test, release cycle with appropriate approvals at each stage

      how do you test before deploying in production, do you prepare and execute test cases?
      yes. However, most developers only do unit testing..Testing beyond unit testing is done by specialized testing teams such as QA group that focuses on integration-system-performance testing

      what was the most challenging situation you have faced and how did you solve it?
      Most challenging situation is to understand at a high level the system requirements and design the system without focusing on too much details first

      what is a stored procedure, why would you use it instead of say another application program?
      A block of code that encapsulates a group of SQL statements and some logic...A stored procedure is preferred in some situations because it is executed on the database server, pre-compiled, and most work can be delegated to DB server

      what is a prepared statement?
      compiled sql statement

      how does database locks work, what is row level lock and table level lock and where is table level lock used?
      row level locks are very fine grained....table level locks are to be used sparingly..

      what are the different databases you have worked on?
      i have mostly worked on oracle...early during my career, I have worked a lot with DB2

      imagine yourself as the dba and user complained the report query is not performing well, what do you do?
      I will look at what SQL is executed for the report, try gethering execution plan data and tune it for performance

      what are the commands for optimizer, query plan generator?
      explain plan, tkprof

      what are the database statistics tools that you used?
      tkprof...oracle enterprize manager has some graphical user interfaces that can aid in viewing database statistics

      what kind of java programming have you done?
      primarily enterprise application development using j2ee based technologies...core java, jdbc, jms, xml parsing, UI (jsf, jsp, servlets), middleware development

      what are the key differences between java and other programming languages such as c++?
      for ex, java is for "write once, run anywhere" paradigm which is based on java virtual machine...with C++, you have to compile the same program for different operating systems..there are several other differences in terms of language constructs and syntax..

      what is garbage collection?
      garbage collection is freeing up space allocated on heap for storing java objects created during program execution

      what is the difference between interface and abstract class?
      interface defines what a system can do without specifying exact implementation...
      Abstract classes can have some implementation and some interface definition

      what is synchronization?
      synchronization is to allow access for data by multiple threads in a non-destructive way

      what is web services and what is your experience in the web services area?
      a webservice is a platform independent way to communicate over http with many disparate systems. I have developed many interafces using web services both on producing and consuming side.

      left outer join
      - returns all rows from left side
      equals, hashcode
      - Equal objects have equal hashcode

      exception hierarchy
      - throwable is at the top with two subclasses: Errors, Exceptions
      - Exceptions are two types: checked and unchecked(Runtime)

      Access modifiers
      - protected means within package and subclasses outside
      - nothing means within package only