Friday, March 21, 2014

Weblogic 11g deployment error , loader constraint violation: when resolving field "DATETIME"

Error :

 INFO com.sun.jersey.server.impl.application.WebApplicationImpl Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'
<Mar 20, 2014 3:08:39 PM CDT> <Error> <HTTP> <BEA-101216> <Servlet: "jersey-serlvet" failed to preload on startup in Web application: "sampleapp".
java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME" the class loader (instance of weblogic/utils/classloaders/ChangeAwareClassLoader) of the referring class, javax/xml/datatype/DatatypeConstants, and the class loader (instance of <bootloader>) for the field's resolved type, javax/xml/namespace/QName, have different Class objects for that type


Solution:

The error occurred due to a conflicts between the class provided by the weblogic and the jaxb-impl.jar packaged within ear file.

I added the jax-impl dependency in pom as 'provided' scope so that its excluded from packaging. The container provided classes are used then.

<dependency>
                <groupId>com.sun.xml.bind</groupId>
                  <artifactId>jaxb-impl</artifactId>
                  <version>2.3.1</version>
                  <scope>provided</scope>               
</dependency>   

com.sun.jersey.spi.service.ServiceConfigurationError: com.sun.jersey.spi.HeaderDelegateProvider

Error on adding Jersey jars for Restful Webservices:

Message icon - Error [HTTP:101216]Servlet: "jersey-serlvet" failed to preload on startup in Web application: "sampleapp". com.sun.jersey.spi.service.ServiceConfigurationError:
com.sun.jersey.spi.HeaderDelegateProvider: The class com.sun.jersey.core.impl.provider.header.LocaleProvider implementing provider interface
com.sun.jersey.spi.HeaderDelegateProvider could not be instantiated: null at com.sun.jersey.spi.service.ServiceFinder.fail(ServiceFinder.java:602) at
com.sun.jersey.spi.service.ServiceFinder.access$800(ServiceFinder.java:159) at com.sun.jersey.spi.service.ServiceFinder$LazyObjectIterator.hasNext(ServiceFinder.java:892) at
com.sun.jersey.core.spi.factory.AbstractRuntimeDelegate.<init>(AbstractRuntimeDelegate.java:76) at com.sun.jersey.server.impl.provider.RuntimeDelegateImpl.<init>
(RuntimeDelegateImpl.java:54) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at
java.lang.Class.newInstance0(Class.java:355) at java.lang.Class.newInstance(Class.java:308) at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:65) at
javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:117) at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:105) at
javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:91) at javax.ws.rs.core.EntityTag.<clinit>(EntityTag.java:35) at java.lang.Class.forName0(Native Method) at


Fix:

This exception is thrown when Jersey services can not find out specified package and class to instantiate. I verified the correct package where I had my restful service and corrected this path.

<servlet>
       <servlet-name>jersey-serlvet</servlet-name>
       <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
       <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
         <param-value>com.cccis.sampleapp.restful</param-value>
       </init-param>
       <init-param>
               <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
          </init-param>
       <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/
sampleapp/*</url-pattern>
    </servlet-mapping>
   

Sunday, March 9, 2014

MongoDB : Insert a DBRef for a given id in a DBCollection

Following example describes the use of DBRefs in a collection. DBRef can be used to define a reference to record in a different collection. This is similar to Foreign Key in Oracle but the advantage of MongoDB is its flexibility to allow DBRef from different collections in same column.

In the example below, a new employee record has to be added to HR department.

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;

....


    // Get the Database
    DB employeeDB = mongoClient.getDB("employeeDB");

    /* Get the Database Reference for the HR department. The HR is primary key/_id in department table. */
    DBRef departmentHR = new DBRef(employeeDB, "department", "HR");

    // Get the Employee Collection
    DBCollection employeeRecords = employeeDB.getCollection("employee");

                                   
   // Create a Database object to insert new Employee 
   BasicDBObject newEmployee = new BasicDBObject();
   newEmployee.put("employee_id", "1234");


   // Add the DBRef for HR department to this employee
   newEmployee.put("department_id", departmentHR);

   //Insert a new record for Employee.
   employeeRecords.insert(newEmployee);


Please leave a comment for queries/suggestions.