ramesh vanka

Greenhorn
+ Follow
since Mar 13, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ramesh vanka

class rameshservlet extends HttpServlet{
}

raemshservlet does not have any state means, its not having the instance variable and static variable, any object state depends on the instance variables and static variables.

People get doubt on rameshservlet extends HttpServlet, HttpServlet having state variables ?
HttpServlet is a abstract class and also its having instance and statice variables are private

public abstract class More ...HttpServlet extends GenericServlet
implements java.io.Serializable
{
private static final String METHOD_DELETE = "DELETE";
private static final String METHOD_HEAD = "HEAD";
...
}

rameshservlet cant inherited those static variables because they are private.

rameshservlet is stateless.

Ex:2 class nitservlet extends httpservlet{
private int count = 0;
}

nitservlet having the instance variable, so that nitservlet having the state.


Hope, you understand to make the servlet as a stateless or stateful.
10 years ago
This example comes under "A reference to the object containing the final field did not escape the constructor"

When you instantiate a new Holder object with the new operator,
1) the Java virtual machine first will allocate (at least) enough space on the heap to hold all the instance variables declared in Holder and its superclasses.
2) Second, the virtual machine will initialize all the instance variables to their default initial values.
3) Third, the virtual machine will invoke the <init> method in the Holder class.

please refer for above: http://www.artima.com/designtechniques/initializationP.html

Assume: 1st Thread starts 10:00 am, it calls instatied the Holder object by making the call of new Holer(42),
1) the Java virtual machine first will allocate (at least) enough space on the heap to hold all the instance variables declared in Holder and its superclasses. -- it will 10:01 time
2) Second, the virtual machine will initialize all the instance variables to their default initial values -- it will start 10:02 time
3) Third, the virtual machine will invoke the <init> method in the Holder class.-- it will start 10:04 time

Now Thread2 started at --> 10:02:01 time, and it will make a call assertSanity() 10:03, by that time n was initialized with default of Zero, Second thread reading the stale data.

//unsafe publication
public Holder holder;

if you make the public final Holder holder will resolve this issue

or

private int n; if you make the private final int n; will resole this issue.

please refer : http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html under section of How do final fields work under the new JMM?

I am pasting here inside ThreadLocal snippet:

public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}

public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}

ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}

void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}


run(){
ThreadLocal a = new ThreadLocal();
a.set(Thread.currentThread().getName());
}
Two threads t1,t2 threads are running.
T1 Scenario:
ThreadLocal internally maintain map. In map(KEY, VALUE) ,

My Comments:
When the user called a.set(Thread.currentThread().getName()) Initially,
1) if it will check corresponding thread's any threadlocalmap is there or not
2) For 1st time corresponding threadlocalmap is null, so that it will call createMap(Thread t, T firstValue) , here you are creating new Threadlocalmap with this ThreadLocal and setter value and assigning to Thread's Threadlocals
like t.threadLocals = new ThreadLocalMap(this, firstValue);

Here parameterized constructor of ThreadLocalMap first argument is ThreadLocal Object. Now you can see the below ThreadLocalMap Constructor

ThreadLocalMap(ThreadLocal firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}

Inside of ThreadLocalMap it is taking the index as hash of the ThreadLocal only.

3) So when ever you are calling get(Thread t), it will check thread t's threadlocalmap, then it will return value.

Hope it will clear the doubts on the hash...

Relationship between two tables will happen with foreignkey / joins /jointables.

Hibernate Supports Uni-directional, not bi-direction like ejb 2.1 cmp (Drawback of ejb 2.1),

means

For ex:
we have two tables
1) goods – (Parent )
2) goods_record – (Child, foreign-key - goodsid)
Relationship between goods to goods_record is the one to many.
Relationship between goods_record to goods is the many to one.

Relationship of goods to goods_record Not Equals(!=) Relationship of goods_record to goods

Foreign-key goodsid maintain relationship between goods to goods_record

Same Foreign-key goodsid maintain relationship between goods_record to goods.

Relationship of Goods to Goods_Record:
Here Relationship owner is Goods, this relationshipowner will take care of parent and child relationship with updation of foreign-key, because foreign-key maintain relationship both of them.

Relationship Goods_Record to Goods
Here Relationship owner is Goods_Record, this relationshipowner will take care of parent and child relationship with updation of foreign-key, because foreign-key maintain relationship both of them.


In Bi-direction means we have 2 uni-directions like
1)Goods to Goods_Record
2) Goods_Record to Goods

In this scenario foreign-key will by updated by twice, which is not necessary because one uni-direction is enough maintain relationship between two tables.

To avoid the foreign-key updation twice, hibernate introduces “inverse=true”, it will maintain the relationship means nothing like uni-directional, so that we are avoided updation foreign-key twice.

Hope it will clarify all your doubts.

For source code with explanation, please contact me: ramesh.niwas@gmail.com
ThreadLocal instance associated Thread,
run(){
ThreadLocal a = new ThreadLocal();
a.set(Thread.currentThread().getName());
}
Two threads t1,t2 threads are running.
T1 Scenario:
ThreadLocal internally maintain map. In map(KEY, VALUE) ,
KEY = THREAD IDENTIFIER,Based thread hashcode and some logic, it will generate the Thread Identifier
Value= a.set(Thread.currentThread().getName());

so when ever T1 issues t1.get(), first it will check internal map, In the map it have thread identifier it will give the value.

T1 Scenario:
Same like that.

Hope It will clear the doubts.

Now I am explaning the command pattern:

1) Initial Program we have one sender and one receiver.
For example:
Receiver is Calculator:
Class Calculator{
add(){}
sub{}
mul{}
}

Sender is the Client:
-------------------------
//Client want add function in Calculator, He raises add request through calculator object.
Class Test{
Calculator calc = new Calculator();
//add request
calc.add();
//sub request
calc.sub():

}

}

In the above scenario, Client(Sender) directly calls the Calculator(Receiver) through sending request.

In the command design pattern:
----------------------------------------
Third Person will enter here, who takes the command from client(Sender) and do work with receiver(Calculator).
Sender --> Thirdparty --> Receiver.
Here Thirdparty means Invoker.
a) ThirdParty decouples the Sender and Receiver.


In command desing pattern 4 roles are there:
1) command
2) commandHoler
3) Invoker
4) Receiver

Intent:
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Encapsulate a request as an object means
Client wants add request:
In command design pattern, Receiver(Calculator) have functions/operations. For each/combination of operations we create speration commandHolder.

class AddCommandHolder implements Command{
**private Calculator calc ; // encapsulated receiver object

public AddCommandHolder(Calculator calc){
this.calc = calc;
}

public void execute(){
calc.add();
}

}

In general for add request --> calc.add
But in command pattern -->public void execute(){
calc.add();
}


class client{
Calculator calc = new Calculator();
//calc.add();--> this is for general thing.
AddCommandHolder add = new AddCommandHolder(calc);
}

AddCommandHolder: encapusulate my request in an object.
here encapusalted object is receiver object.

Command Holder --> encapuslate receiver object
Invoker --> encapsulate command object.




Both are different pattern usage.
1) State Pattern: All people know about the state flow diagram. how it's work based on the condition it will go one state to another state.
State Pattern means flow of states.
I am giving below examples, it will clear when to go for state pattern and when to go for strategy pattern.
public abstract class Sort {
public abstract void sortAlgorithm();
}
public class MergeSort extends Sort {
public void sortAlgorithm(){
System.out.println("MergeSorting...");
}
}
public class QuickSort extends Sort {
public void sortAlgorithm(){
System.out.println("QuickSorting...");
}
}
public class BubbleSort extends Sort{
public void sortAlgorithm(){
System.out.println("BubbleSorting...");
}
}

In the above example, we have Sort abstract class is there BubbleSort,MergeSort,QuickSort are extending that Sort.
StatePattern: My scenario is if(i==1) then BubbleSort state,if(i==2) then MergeSort state,if(i==3) then QuickSort state. my flow of states are defined like that.
public class Context {

private Sort state;

private Sort quickSort;
private Sort mergeSort;
private Sort bubbleSort;

public Context(){
quickSort = new QuickSort();
mergeSort = new MergeSort();
bubbleSort = new BubbleSort();

}

public void startWorkflow(int i){

if(i==1){
state = bubbleSort;
state.sortAlgorithm();
i=2;
}

if(i==2){
state = mergeSort;
state.sortAlgorithm();
i=3;
}

if(i==3){
state = quickSort;
state.sortAlgorithm();

}

}
}
public class StateTest {

public static void main(String args[]){

Context ctx = new Context();
ctx.startWorkflow(1);

}

}

State Patter:"Allow an object to alter its behavior when its internal state changes",

Explanation: Here in this example Context Internal state is Sort state, based on the if condition state is getting changes, Context object behaviour (startWorkflow) changes means getting different outputs.
Output:
/* output
BubbleSorting...
MergeSorting...
QuickSorting...

Strategy Design Pattern: Scenario is I want to choose any algorithm based on the client choice.
public class Context {
private Sort state;
public Context(Sort state){

this.state = state;
}
public void sortFromContext(){
state.sortAlgorithm();
}
}
public class StrategyTest {
public static void main(String args[]){
Context ctx = new Context(new QuickSort());
ctx.sortFromContext();
}
}
output:
QuickSorting...
Strategy Principle:"Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it."

Explanation: In abstract class we define one algorithm. Each one of the specified classes would usually encapsulate an algorithm, switching between classes would switch the used algorithm and thus change the behaviour of the application.

*****Difference between State and Strategy Pattern************
In Strategy pattern, Assume one base class is there, 10 subclasses extends/implements that base class. Client will choose any one of them as strategy.Means client should explicity mention which class he is going to use.
client will any one out of n subclasses .

In State Pattern main purpose state flow like 1 -> 2 ->4->3 based on the condition. **client will not choose any of them.State Pattern main purpose flow of state it's like our state flow diagram.

Hope it's clear the concept of state vs strategy.

Regards,
Ramesh V








In EJBs, we have 2 type of exceptions are there
1) System Exception
2) EJB Exception

Transaction {
BusinessMethod1();
BusinessMethod2();
BusinessMethod3();
}

While performing above transaction, if any exception has happend

1) If Exception is System Exception:
Then Container will come into picture, rollback the data.

2) If Exception is EJB Exception:
Then developer will handle that the exception.


In your case.

Inside Session Bean A Business Method, user calling the Session Bean B Business Method.

Session Bean B business Method throws Exception, so that Session Bean A business method not handle the exception, it is ejb exception. User should take care while handling these excepions.



StatefulSession Bean:

Once client calls the Home.create() method, container creates EJBObject, Instanstiate the Session Bean and makes relation between ejbobject and sessionbean, then it will send EJBObject stub to client.

For each every client, container creates unique session bean.

So here one point to observer is, container makes the relationship between ejbobject and sessionbean.

When ever client makes call business method (Stub EJBObject), then in server side container will come into picture pick the corresponding bean to perform the business method.

Regards,
Ramesh V
TargetNameSpace: main purpose to identify uniqueness of the schema.

Here I had created 2 schemas 1) Default Name Space 2) Default Name Space and TargetName Space

1.xsd -- DefaultNameSpace
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<complexType name="USAddress">
<sequence>
<element name="name" type="string"/>
<element name="street" type="string"/>
</sequence>
</complexType>
</schema>

2.xsd -- TargetNameSpace
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNameSpace="http://rameshvanka.com/"
elementFormDefault="qualified">

<complexType name="USAddress">
<sequence>
<element name="name" type="string"/>
<element name="street" type="string"/>
</sequence>
</complexType>
</schema>

Now I want to import the schema of 1.xsd and 2.xsd into my 3.xsd

But 1.xsd don't have targetName. it is not possible to import that schema, it don't uniqueness to identify the schema.

2.xsd have targetNameSpace, so that 3.xsd able to identify the target using the targetNameSpace of http://rameshvanka.com/

3.xsd:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ram="http://rameshvanka.com/"
targetNameSpace="http://megastar.com/"
elementFormDefault="qualified">

<import schemalocation="2.xsd"
namespace="http://rameshvanka.com/"/>

</schema>

Hope it will clear the all doubts on targetNamespace.











MDB are controlled by container. For container 2 options are there either start a transaction or not to start transaction(No Transaction). Start Transaction here means if pre existing transaction is there (txa), it will continue that transaction other wise start new transaction.

Container:
1) Start Transaction:

if pre existing transaction (TXA) is there, it will continue that transacation: (TXA) --> (TXA)
if no pre existing transaction is there, it will start new transaction (___) --> (TXA)

2) NO transaction

if pre existing transaction (TXA) is there, it will not support: (TXA) --> (___).
if no pre existing transaction is there, it will continue that one.(____) --> (____).


Regards,
Ramesh V