Sunday 29 March 2015

Why can't I define a static method in a Java interface?

There are two questions here:
  1. Why can't interfaces contain static methods?
  2. Why can't static methods be overridden?

Tuesday 10 March 2015

What is ServletRequestWrapper and How do we use it.

public class ServletRequestWrapper
extends java.lang.Object
implements ServletRequest
Provides a convenient implementation of the ServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.
more:-

https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequestWrapper.html


This is the Best Link to knwo more about ServeltRequestWrapper


https://vangjee.wordpress.com/2009/02/25/how-to-modify-request-headers-in-a-j2ee-web-application/

Tuesday 3 March 2015

Jdbc 3.0 vs Jdbc 4.0

Jdbc 3.0:-
Jdbc RowSet We have done the great discussion on JdbcRowSet in the previous page.
Savepoint in transaction management Now you are able to create, rollback and release the savepoint by Connection.setSavepoint(), Connection.rollback(Savepoint svpt) and Connection.releaseSavepoint(Savepoint svpt) methods.
Statement and ResultSet Caching for Connection Pooling Now you are able to reuse the statement and result set because jdbc 3 provides you the facility of statement caching and result set caching.
Switching between Global and Local Transactions
Retrieval of auto generated keys Now you are able to get the auto generated keys by the method getGeneratedKeys().

Jdbc 4.0:-
The important features of JDBC API 4.0 are given below:
Automatic Loading of Driver class You don't need to write Class.forName() now because it is loaded bydefault since jdbc4.
Subclasses of SQLException Jdbc 4 provides new subclasses of SQLException class for better readability and handling.
New methods There are many new methods introduced in Connection, PreparedStatement, CallableStatement, ResultSet etc.
Improved DataSource Now data source implementation is improved.
Event Handling support in Statement for Connection Pooling Now Connection Pooling can
listen statement error and statement closing events.

Working with the JDBC RowSet API

ntroduction

RowSet interface is basically an extension of JDBC ResultSet and is a part of thejavax.sql package. Built on the standard contextual structure of the JavaBeans component model, the core design structure is inherently reflected in the subjectivity of RowSet interface. It makes sense that RowSet has a set of JavaBeans properties, which can be set and retrieved with the available setter and getter methods. In addition to that RowSet also implements the JavaBeans mechanism of event notification that allows other components registered to the instance to receive notification when a certain event is triggered. These additional capabilities make it more flexible and leverage productivity when used effectively. In this article we shall try to get a glimpse of what RowSet is all about and how to implement one in Java.

Types of RowSet

There are two types of RowSet objects – connected and disconnected. Connected RowSet are those which establish a connection with the database and retain it until the application terminates. Disconnected RowSet on the other hand establishes a connection, executes a query such as retrieving data from the database and closes the connection.

more you can get from here:-

Why Need of serialization in Java

Serialization is usually used When the need arises to send your data over network or stored in files. By data I mean objects and not text.
Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not JAVA objects.
Serialization is the translation of your Java object's values/states to bytes to send it over network or save it.
This is analogous to how your voice is transmitted over PSTN telephone lines...
---------------
  • A webservice requires serialization of objects to xml before they can be transmitted.

Sunday 1 March 2015

Why wait() ,notify() and notifyAll() methods are in Object class instead of Thread class?

These methods works on the locks and locks are associated with Object and not Threads. Hence, it is in Object class.

The methods wait(), notify() and notifyAll() are not only just methods, these are synchronization utility and used in communication mechanism among threads in Java.

Synchronization: The concept of synchronization will make sure that only one Thread is accessing an object/resource at the same time.
Java concurrency model uses locks to implement mutually exclusive access to objects in a multi-threaded environment. Locks are associated with objects not with the Threads. Hence, Locks are made available on per Object basis.
 In Java, to achieve mutually exclusive access on objects, a threads needs to acquire lock and other threads needs to wait to acquire lock. And they don’t know which threads holds lock instead they just know the lock is hold by some other thread and they should wait for lock instead of knowing which thread is inside the synchronized block and asking them to release lock. This analogy fits with wait and notify being on object class rather than thread in Java.

Example:


                                               
I would like to discuss with a well known example to get a clear understanding of this topic. Let’s take a real time banking scenario. Suppose two or more customers having a joint account, which can be permitted to perform the transactions through multiple channels like Teller, ATM, Mobile Banking, and Internet Banking. Currently the account having the balance 2000/- and the first user trying to purchase worth of 1500/- through Internet Banking other user trying to withdraw 1500/- through ATM. Here whichever the channel first access the account to perform the transaction will acquires a lock and other channel will wait for the lock. The channel which is waiting for lock status is not aware of which channel acquires the lock and at the same time the channel which is already acquired the lock is not aware of that who are waiting to acquire lock on the particular account. Here lock is applied on the account and not on channel.

ref: