JDBC connection issue

Solapas principales

 

I am using the below code in my REST service and it throws the error "java.lang.NoClassDefFoundError: com/intersys/jdbc/CacheDataSource] with root cause
java.lang.ClassNotFoundException: com.intersys.jdbc.CacheDataSource
"

Surprisingly when i am using the same code in a static main method its running perfectly. 

package webapps.services.datamgmt.webapps;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.intersys.jdbc.CacheDataSource;

import webapps.services.datamgmt.CacheQueryUtilities;
import webapps.services.datamgmt.dto.UiTestCaseContainer;
import webapps.services.datamgmt.dto.WebAppContainer;

@Path("cache")
public class CosController {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public WebAppContainer getIt() {
        WebAppContainer jsonContainer = new WebAppContainer();
        jsonContainer.setErrorText("No Error");
        jsonContainer.setStatusCode("200");
        return jsonContainer;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("save")
    public WebAppContainer saveCase(UiTestCaseContainer uiTestCaseContainer) {
        try {
//            CacheQueryUtilities cacheQueryUtility = new CacheQueryUtilities();
//            cacheQueryUtility.InsertUpdateUiTestCase(uiTestCaseContainer);
            
            CacheDataSource cacheDataSource = new CacheDataSource();
            cacheDataSource.setURL("jdbc:Cache://127.0.0.1:1972/ABCDNS");
            cacheDataSource.setUser("USERNAME");
            cacheDataSource.setPassword("PASSWORD");
       
            
            Connection dbconnection = cacheDataSource.getConnection();
            Statement st = dbconnection.createStatement(); 
            ResultSet rs = st.executeQuery("SELECT * FROM Table1");
            while (rs.next()) {
                System.out.println(rs.getString(2)); 
            }
         dbconnection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        WebAppContainer jsonContainer = new WebAppContainer();
        jsonContainer.setErrorText("No Error");
        jsonContainer.setStatusCode("200");
        return jsonContainer;
    }
}

Respuestas

How are you running both scenarios? The error you're getting is mostly a symtom of the Cache JDBC jar not being present in the classpath. When you run locally it may be present in your IDE but it's not being included in your build thus the REST code is failing.

Thank you , i added the jars in tomcat lib directory and its fixed my issue.