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.

Advertisements