Commit Priority for transaction Resources in Websphere!

PROBLEM

Recently we faced a situation where in DB commits which were issued before the JMS puts, were not actually reflecting in the database.

To elaborate further, consider this…

  1. A MQ message arrives on a Queue say Q1.
  2. Corresponding MDB(for Q1) gets the message and starts processing.
    1. Creates some DB records.
    2. Puts a MQ message with DB information, say primary key, on another queue Q2.
  3. MDB, for Q2, gets the message and starts processing.
    1. Fetches DB information as created in Step 2.a and FAILS with “ENTITY WITH KEY NOT FOUND IN THE DATABASE” ???????????

Where the hell did it go?

After some digging/retesting with enhanced logging we concluded that even though the commit to DB was issue before MQ put, because there was a DB failover which happened at the same time and since the DB was not available momentarily WAS proceeded with the next resource commit, which in this case happen to be the MQ PUT.

What was supposed to happen?

  1. A global transaction is started
  2. MQ resource branch …0000000000000000000000000001 (to GET a message from the Q1)
  3. Oracle resource branch …0000000000000000000000000002 (is added for the database work)
  4. MQ resource branch …0000000000000000000000000003 (is added to PUT a message to Q2)

 

  1. WebSphere TM prepares …0000000000000000000000000003(MQ.PUT)
  2. WebSphere TM prepares …0000000000000000000000000002(DB.COMMIT)
  3. WebSphere TM prepares …0000000000000000000000000001(MQ.GET)

 

  1. WebSphere Tm commits …0000000000000000000000000001(MQ.GET)
  2. WebSphere Tm commits …0000000000000000000000000002(DB.COMMIT)
  3. WebSphere Tm commits …0000000000000000000000000003(MQ.PUT)

What ACTUALLY happened?

  1. WebSphere Tm commits …0000000000000000000000000001 (MQ.GET)
  2. WebSphere Tm commits …0000000000000000000000000002 (DB) —-> Oracle commit delay, so ignored for now and move to the next resource.
  3. WebSphere Tm commits …0000000000000000000000000003 (MQ.PUT)
  4. WebSphere Tm commits …0000000000000000000000000002 (DB) -> retried/wait longer and success

The order would have been accurate when and only when there is no oracle commit delay caused by the DB data source. However if there is a commit delay/failover probably, the logic of the IBM TRANSACTION MANAGER is to temporarily skip it (it will retry/wait later) and move on to the next resource with the same commit order. ALTHOUGH WE ARE NOT ALLOWED TO DO SO, and to avoid even further delays with IBM support, we know for sure this logic because we’ve reverse engineered the IBM TRANSACTION MANAGER code. Because of this logic, if all the resources have the same commit order or no commit order specified, the commit steps that you will see in case of oracle commit delay is the following.

POSSIBLE SOLUTION

Specify a commit order on your resources. More on commit priority…

(http://www.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.base.doc/ae/cjta_rescom.html)

If you have specified a commit order as indicated earlier, the IBM TRANSACTION MANAGER will not skip the commit and not move on to the next resource commit, it will wait/retry until the DB is effectively committed.

  1. WebSphere Tm commits …0000000000000000000000000001 (MQ.GET)
  2. WebSphere Tm commits …0000000000000000000000000002 (DB) —-> Oracle commit delay, but wait and retry before attempting the commit on MQ.PUT data source

10. WebSphere Tm commits …0000000000000000000000000003 (MQ.PUT)

Method Local Inner Class ! REWORKED!

Today I got a comment on my blog entry HERE.

When I went through my blog again, I was surprised to see that the thing I explained there was not right, probably at that time my understanding was not good enough. So I apologize for providing the wrong information. I will explain it again here :
Considering the same example :

class MyClass{
public void showValue(){
final int x = 0;
class MyInnerClass{
public void showOuterVar(){
System.out.println(“Hi I am method local Inner Class” + x);
}
}
}
}
Now as I explained earlier, the local variables of the method live on the stack, and exist only for the lifetime of the method. As we know that the scope of a local variable is limited to the method the variable is declared in so, When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an some variable. Because the local variables are not guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them.
So we mark the local variables as final. When marked final now our class can use it.

Here is the correct reason :
As a part of optimizing the java code, what JAVA Programmers at SUN did, they made the JVM do some WORK, So in this case when this class is compiled there will be two class files (MyClass.class & MyClass$MyInnerClass.class) and the the value of the final variable is stuffed in the inner class i:e: we will have a variable “val$x” in the inner class. Compiler does this because it knows that the variable is final so there is no harm in inserting the value as it is, in the class. You probably won’t be able to see this in your compiler class but it does this way only. The fact can be verified in another way. When you open you class file of the inner class you will see a variable “this$0”, this is actually a reference to the outer class, this is the reason that you can access the outer class variables without any reference. So this is it works. I had a lot of discussion among my friend before writing this and we decided on this only. 🙂
So if anyone gets to know a better reason, Please do share with us. Thanx. enjoy.

Axis2-SOAP-Header problem

Hello everybody again.
Currently I am working with web-services, and my current implementation of the project uses Axis2. Few days back I was stuck with a very weird problem. Scenario was like this.
I have a Web-Application(WA) which is supposed to call a web-service (WS1) which in turn will forward the request to another web-service (WS2) which in turn will forward the request to final web-service(WS3). I have made a generic stub which is used to call any web-service(And I have disabled the header adding code inside this stub because of project requirement.) So the WA was preparing a document and sending it to the WS1 using the generic stub, Now WS1 does some processing and send it to WS2 using the same generic stub, and WS2 again sends it to WS3 using the same generic stub. Now my requirement was that there should not be any SOAP Header in my requests because if it exists then the document will fail the validation. So the problem was that when the document was reaching WS1 it was not having any header and it passed the validation test but when it reaches the WS2 it failed validation. When I traced the log I found that it was having a SOAP Header.
It was very weird since the same stub was being used so how cum there were two documents being generated one with header and the other without header.
After tracing all the logs I didn’t found anything. So finally I used the soap-monitoring tool to see what actually is being transmitted on wires from one WS to the other. Then I came to know, when one WS is sending the document to the other WS it is adding SOAP Header into the document and after a some brain storming I concluded that it does so because by default addressing is enabled at the global level and it is the addressing which was responsible for doing this.
So I disabled the addressing module and it worked. Now the document being passed from one WS to the other WS is not having the SOAP header. 😀
To disable the addressing module you simply go to axis2.xml file and find this line.

<module ref=”addressing”/>

Comment this line and the addressing module is disabled.

Axis2 Hibernate Problem

Hi all, So I am back with another problem->solution, Some days back a guy pinged me up and asked for help with a problem which I found that I also have faced a long time back while in one of my project. The problem is integration of Hibernate with Axis2. Though I myself didn’t solve the problem at that time because we have dropped the idea of using hibernate for some other reasons. But this time this guy himself solved the problem (cheers for him) and I am sharing the solution with you all as I also tried my hand on it and it worked.

I will explain with an example.

I have to create a web service which will receive 3 parameters from the client and persist them into the database using Hibernate. Not a big task :P. So I created a wsdl, generated the classes using axis2’s “wsdl2java“ command and put my own business logic, created the hibernate configuration and mapping files and simply deployed the aar file with the axis2 which further was deployed as a web application into the JBoss AS . The structure of my application is like this :

testService.aar

data (package)

o service (package

§ business ( package )

· DataBean.java (class)

· DataBeam.hbm.xml (class mapping file)

· DataBusinessClass.java ( business class)

§ Core (package with generated classes for web service)

· DSServiceCallBackHandler.java

· DSServiceMessageReciever.java

· DSServiceSkeleton.java

· DSServiceSkeletonInterface.java

· DSServiceStub.java

§ Persistence

· HibernateUtil.java

Lib ( libraries used in my application)

META-INF

o DSService.wsdl

o Services.xml

hibernate.cfg.xml

And finally I successfully deployed my application. But when I tried to execute it using a client, it gave an error, it didn’t found the hibernate.cfg.xml file. Here was what I saw on the JBoss console

2009-02-26 09:41:08,609 INFO [data.service.core.DSServiceSkeleton] Document recieved : <wsdl:dataRequest xmlns:wsdl=”http://www.example.com/DataService/wsdl&#8221; xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”&gt;

<name>bhupesh</name>

<age>25</age>

<sex>male</sex>

</wsdl:dataRequest>

2009-02-26 09:41:08,656 INFO [data.service.core.DSServiceSkeleton] Values recieved :

2009-02-26 09:41:08,656 INFO [data.service.core.DSServiceSkeleton] Name : bhupesh

2009-02-26 09:41:08,656 INFO [data.service.core.DSServiceSkeleton] Age : 25

2009-02-26 09:41:08,671 INFO [data.service.core.DSServiceSkeleton] Sex : male

2009-02-26 09:41:08,671 INFO [data.service.business.DataBusinessClass] insertData() method called…

2009-02-26 09:41:08,671 INFO [data.service.business.DataBusinessClass] execute() method called…

2009-02-26 09:41:08,687 INFO [data.service.persistence.HibernateUtil] static block of HibernateUtil class

2009-02-26 09:41:08,781 INFO [org.hibernate.cfg.Environment] Hibernate 3.2.4.sp1

2009-02-26 09:41:08,796 INFO [org.hibernate.cfg.Environment] hibernate.properties not found

2009-02-26 09:41:08,812 INFO [org.hibernate.cfg.Environment] Bytecode provider name : javassist

2009-02-26 09:41:08,812 INFO [org.hibernate.cfg.Environment] using JDK 1.4 java.sql.Timestamp handling

2009-02-26 09:41:09,000 INFO [org.hibernate.cfg.Configuration] configuring from resource: /hibernate.cfg.xml

2009-02-26 09:41:09,000 INFO [org.hibernate.cfg.Configuration] Configuration resource: /hibernate.cfg.xml

2009-02-26 09:41:09,000 ERROR [org.apache.axis2.transport.http.AxisServlet] java.lang.ExceptionInInitializerError

2009-02-26 09:41:09,015 ERROR [org.apache.catalina.core.ContainerBase] Servlet.service() for servlet AxisServlet threw exception

java.lang.NullPointerException

So I found that it is not able to load the configuration from the hibernate.cfg.xml file.

Now this problem is resolved if you put your file configuration file inside the Axis2/WEB-INF folder as the axis2 class loader and also if you have to put the hibernate jars inside Axis2/WEB-INF/lib. But this is not a good solution.

Solution to this problem :

Axis2’s service class loader prime use case is service isolation, hot deployment and hot updates. So Axis2 creates a new class loader for every service and carefully manages which jars are available in that class loader. One common problem is that an external project uses a Thread Context class loader to load classes or resources from its environment. (TCCL-Thread Context Class Loader ) instead of trying to use the classloader that the class itself was loaded with. So to facilitate this mechanism you can specify a parameter in your services.xml file as under.( from the wso2 site )

<serviceGroup>

<service name=“DSService”>

<!– added for loading libraries files from the lib folder inside aar file –>

<parameter name=“ServiceTCCL”>composite</parameter>

. . . .

. . . .

<!– More configurations as per your service à

</service>

</serviceGroup>

By doing this libraries inside your aar file will be accessed.

Another Problem

Now class loading problem was solved but still axis2 was not able to find the mapping file.

Since I have explained before that Axis2 has the notion of service isolation where each service/modules gets its own class loader. Using this class loader you can access any resource that you put into your service archive.

You can do this as under within your HibernateUtil class.

AxisService axisService = MessageContext.getCurrentMessageContext().getAxisService();

ClassLoader serviceClassLoader = axisService.getClassLoader();

URL configURL = serviceClassLoader.getResource(“hibernate.cfg.xml”);

sessionFactory = new Configuration().configure(configURL).buildSessionFactory();

Work Done 😉

Now When you deploy your application and play with it.

My data was successfully persisting into the database.

Deploying Entity Bean on JBoss 5

Hello everyone, Now a days I am trying my hands on EJB 3.0. I have been experimenting
with EJB 3.0 on JBoss application Server. I went on many forums and found that some of
you people are facing problems deploying Entity Beans same as I did in the begining,
So i thought of writing on it n giving a working example.
I have gone through “Enterprise java beans 3.0” book by oreilly. its a very nice book.
My example is also from the same book.
I will start with the example as you people can go through the definition n all from
the book. Though you can get this code from book too m also writng it so that everything
should be in a rytdhm 😛

One more thing you can code entity bean either using annotations or by specifying the
mappings in a mapping file named “orm.xml”. I will give an example based on the mapping
file, I you guys want an example using annotations refer to the unlined link. He is my
colleague and a good friend, he has explained clearly about the example and much more
concepts of the entity beans.
http://ranjankumar.com/2008/08/11/ejb3-mapping-persistent-objects/

we will start with code now
This is our customer class : ENTITY BEAN

package com.titan.domain;
import java.io.Serializable;
/**
* @author bhupesh
*
*/
public class Customer implements Serializable {
private int id;
private String firstName;
private String lastName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

We Will need a session bean which will be responsible for persisting the object into
the database, though the actual work is done by an ENTITY MANAGER
SESSION BEAN :
INTERFACE –
package com.titan.travelagent;
import com.titan.domain.Customer;;
/**
* @author bhupesh
*
*/
public interface TravelAgentRemote {
public void createCustomer(Customer p);
public Customer findCustomer(int id);
}
CLASS-
package com.titan.travelagent;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.titan.domain.Customer;
/**
* @author bhupesh
*
*/
@Stateless
@Remote(TravelAgentRemote.class)
public class TravelAgentBean {
@PersistenceContext(unitName=”titan”)private EntityManager manager;
public void createCustomer(Customer cust){
manager.persist(cust);
}
public Customer findCustomer(int id){
return manager.find(Customer.class, id);
}
}

ORM Mapping file is :
<?xml version=”1.0″ encoding=”UTF-8″?>
<orm:entity-mappings version=”1.0″
xmlns:orm=”http://java.sun.com/xml/ns/persistence/orm&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd “>
<orm:description>ORM file for mapping Customer entity!</orm:description>
<orm:entity access=”PROPERTY” class=”com.titan.domain.Customer” name=”customer”>
<orm:table name=”CUSTOMER_INFO” />
<orm:attributes>
<orm:id name=”id”>
<orm:column name=”id” nullable=”false” column-definition=”integer”/>
</orm:id>
<orm:basic name=”firstName” >
<orm:column length=”20″ name=”FIRST_NAME” nullable=”false” />
</orm:basic>
<orm:basic name=”lastName” >
<orm:column length=”20″ name=”LAST_NAME” nullable=”false” />
</orm:basic>
</orm:attributes>
</orm:entity>
</orm:entity-mappings>

Now we also need a persistence.xml file which goes into the META-INF directory
here is it :

<persistence:persistence version=”1.0″
xmlns:persistence=”http://java.sun.com/xml/ns/persistence&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence persistence_1_0.xsd “>
<persistence:persistence-unit name=”titan”>
<persistence:description>
Persistence unit name is titan
</persistence:description>
<persistence:jta-data-source>java:OracleDS</persistence:jta-data-source>
<persistence:mapping-file>META-INF/orm.xml</persistence:mapping-file>
<persistence:properties>
<persistence:property name=”hibernate.hbm2ddl.auto” value=”create” />
<persistence:property name=”hibernate.dialect” value=”org.hibernate.dialect.Oracle9iDialect” />
</persistence:properties>
</persistence:persistence-unit>
</persistence:persistence>

Note one thing
<persistence:mapping-file>META-INF/orm.xml</persistence:mapping-file>
you have to give like this. Firstly I tried by giving only “orm.xml” and when I packaged &
deployed the jar it gave an error stating that the mapping file could not be found.
Then i tried another bad thing, i put this file into the src directory which ultimately came into
the bin folder. When I deployed the jar to my surprise it worked. 😛 Anyway its not a good
programming practice. So i tried giving in the above way META-INF/orm.xml and it worked fine.

JAR Structure
customer.jar
–com
—-titan
——domain
——–Customer.class
——travelagent
——–TravelAgentRemote.class
——–TravelAgaentBean.class
–META-INF
—-orm.xml
—-persistence.xml

Thats all about in how to deploy Entity Beans on JBoss 5. Wait for more to come in EJB 3.0
If you still have any queries then feel free to contact me.

Method Local inner Classes! Confusion.

Last month when I was preparing for SCJP exam, I came through many new things in Java Language, some things were pretty confusing too. While going through the Inner Classes chapter I came through a problem. As we know we can define a class within a method, likewise :

Class MyClass{

private int x;

public showValue(){

class MyInnerClass{

System.out.println(“Hi I am method local Inner Class”);

{

}

A method-local inner class can be instantiated only within the method where the inner class is defined i:e: no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class. But, the inner class object cannot use the local variables of the method the inner class is in. The local variables of the method live on the stack, and exist only for the lifetime of the method. As we know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is blown away and the variable is history. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them.

So we mark the local variables as final. When marked final now our class can use it.

But How?

I didn’t knew where the local final variables are stored. Actually if you go through the specification of the JVM you will come to know that JVM defines various runtime data areas that are used during execution of a program. Some of these data areas are created on Java virtual machine start-up and are destroyed only when the Java virtual machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

Different areas are :

1. The PC Register

2. Stack

3. Heap

4. Method Area

5. Runtime Constant Pool

6. Native Method Stack

I will not explain each of them here, for this you can refer to the Java virtual Machine Specifications.

The point of our discussion is where are final variables stored. They are stored in Runtime Constant Pool .

A runtime constant pool is a per-class or per-interface runtime representation of the constant_pool table in a class file. It contains several kinds of constants, ranging from numeric literals known at compile time to method and field references that must be resolved at run time. The runtime constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.

Each runtime constant pool is allocated from the Java virtual machine’s method area. The runtime constant pool for a class or interface is constructed when the class or interface is created by the Java virtual machine. The following exceptional condition is associated with the construction of the runtime constant pool for a class or interface:

  • When creating a class or interface, if the construction of the runtime constant pool requires more memory than can be made available in the method area of the Java virtual machine, the Java virtual machine throws an OutOfMemoryError.

Accessing SSL enabled Web Services

<!– @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } –>

This document will guide you in Consuming a Web Service over a HTTPS protocol.

I am using AXIS2 for deploying my Web Service and hence here I will present you with an example of making a client for a Web Service ( Version ) which comes deployed in AXIS2.

For security we usually deploy out Web Service over the HTTPS protocol. As you must be knowing HTTPS protocol uses Digital Certificate for the authentication and to ensure that no one else except the the authenticated parties may access/tamper the data.

Since in my previous post i:e: https://ravishbhupesh.wordpress.com/2008/05/06/enabling-ssl-on-jboss-2/ I described how to enable the JBOSS Application Server.

There we created a keystore ( which contains a certificate ).

Now we have to export the server certificate so that clients can use it. For exporting the certificate we will write

keytool – export -alias <keystore-alias-name> -keystore <keystore-name> -rfc -file <your-cert-name.cer>

then it will prompt for the password ( of the keystore ) : give it and your certificate will be generated by the name.

In my case I did :

keytool -export -alias nsdg_ssl -keystore nsdg.keystore -rfc -file nsdg_temp.cer

and a certificate by name nsdg_temp.cer was generated.

If you want to see the certificate you can write at the terminal :

cat you-cert-name.cer

If a client has to communicate to a server over SSL, it must have a truststore file. Below is given the command which will import the certificate to the client truststore :

keytool -import -alias <trust-store-name> -file <cert-file-name.cer> -keystore <client-trust-store>

FINALLY We will write a JAVA CLIENT which will call a web service.

To access a secure website from a web service client you need to set the following properties :

javax.net.ssl.trustStore and

javax.net.ssl.trustStorePassword

The value of javax.net.ssl.trustStore should be the path of your truststore file. You must have already imported the certificate to this truststore from th server to which you are going to communicate.

This is how i did :

System.setProperty(“javax.net.ssl.trustStore”,”E:\\Workplace\\workspace\\nsdgClientTrustStore”);

System.setProperty(“javax.net.ssl.trustStorePassword”, “rmi+ssl”);

My Client looked like :

import javax.xml.namespace.QName;

import javax.xml.stream.XMLOutputFactory;

import javax.xml.stream.XMLStreamWriter;

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.*;

/**

* @author bhupesh

*/

public class VersionJavaClient {

private EndpointReference targetEPR;

private QName operationName = new QName(“version”);

public static void main(String[] args) throws AxisFault {

System.setProperty(“javax.net.ssl.trustStore”,

“E:\\Workplace\\workspace\\nsdgClientTrustStore”);

System.setProperty(“javax.net.ssl.trustStorePassword”, “rmi+ssl”);

VersionJavaClient client = new VersionJavaClient();

client.getAxisVersion();

}

private void getAxisVersion() throws AxisFault {

System.out.println(“getAxisVersion() STARTS”);

OMElement payload = createRequestMethod();

ServiceClient serviceClient = new ServiceClient();

Options options = new Options();

options.setTo(new EndpointReference(“https://202.141.151.147:8443/axis2/services/Version&#8221;));

options.setAction(“getVersion”);

serviceClient.setOptions(options);

OMElement result = serviceClient.sendReceive(payload);

System.out.println(“getAxisVersion() ENDS”);

if (result != null) {

System.out.println(“RESULT WILL BE DISPLAYED NOW”);

displayResult(result);

} else {

System.out.println(“Got Null Result”);

}}

private OMElement createRequestMethod() {

System.out.println(“createRequestMethod() :: creating envelope”);

OMFactory fac = OMAbstractFactory.getSOAP11Factory();

OMNamespace omNs = fac.createOMNamespace(“http://localhost/d2&#8221;, “d2”);

OMElement requestMethod = fac.createOMElement(“getVersion”, omNs);

OMElement firstElement = fac.createOMElement(“firstElement”, null);

requestMethod.addChild(firstElement);

//System.out.println(requestMethod.getText());

System.out.println(“createRequestMethod() :: request method returned..”);

return requestMethod;

}

private void displayResult(OMElement result) {

System.out.println(“displayResult() :: STARTS”);

try {

XMLStreamWriter writer = XMLOutputFactory.newInstance()

.createXMLStreamWriter(System.out);

//result.serialize(new OMOutputImpl(writer));

result.serialize(writer);

System.out.println(“trying to serialize the result. FLUSHING….”);

System.out.println(“displayResult() :: ENDS”);

writer.flush();

} catch (Exception e) {

System.out.println(“got Exception while printing”);

e.printStackTrace();

}}}