error in transaction in Sparql and jena tdb?

653 Views Asked by At

This is my jena code:

package neww;
import static com.hp.hpl.jena.query.ReadWrite.READ ;
import static com.hp.hpl.jena.query.ReadWrite.WRITE ;
import com.hp.hpl.jena.query.ReadWrite ;
import com.hp.hpl.jena.query.Dataset ;
import com.hp.hpl.jena.query.Query ;
import com.hp.hpl.jena.query.QueryExecution ;
import com.hp.hpl.jena.query.QueryExecutionFactory ;
import com.hp.hpl.jena.query.QueryFactory ;
import com.hp.hpl.jena.query.QuerySolution ;
import com.hp.hpl.jena.query.ResultSet ;
import com.hp.hpl.jena.tdb.TDBFactory ;
import com.hp.hpl.jena.query.ResultSetFormatter;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
import java.util.Iterator;
import java.util.Scanner;
import com.hp.hpl.jena.sparql.sse.SSE ;

import com.hp.hpl.jena.update.* ;

import java.util.*;
public class Si{
public String retur(){
String query1;
            query1="[email protected]";
            //query1 = (String)(subjectentry.getText());
            String s="SELECT ?x WHERE { ?x <TO:> '"+query1+"'}";
            //   subjectoutput.setText(s);
            String directory = "/usr/local/apache-tomcat-8.0.8/webapps/examples/WEB-INF/classes/neww/MYDATABASES/DATA2" ;
            Dataset ds = TDBFactory.createDataset(directory) ;
            Model model = ds.getDefaultModel() ;
           ds.begin(ReadWrite.READ) ;
             QueryExecution qExec = QueryExecutionFactory.create(s, ds) ;
           ResultSet rs = qExec.execSelect() ;
           String x=rs.toString();
          String[] arr = null;

            while (rs.hasNext()) 
            {
                QuerySolution qs = rs.next();
                String rds = qs.get("x").toString();
               // String em = (String)rs.getString(0);
               //arr = em.split("\n");
               //for (int i =0; i < arr.length; i++){
              return rds; 
            }
             //if(rs==null)
                //subjectoutput.setText("sorry!!");
           String f="h";
           return x;

and this is giving error:

com.hp.hpl.jena.tdb.transaction.TDBTransactionException: Not in a transaction
    com.hp.hpl.jena.tdb.transaction.DatasetGraphTransaction.get(DatasetGraphTransaction.java:106)
    com.hp.hpl.jena.tdb.transaction.DatasetGraphTransaction.get(DatasetGraphTransaction.java:40)
    com.hp.hpl.jena.sparql.core.DatasetGraphTrackActive.getDefaultGraph(DatasetGraphTrackActive.java:91)
    com.hp.hpl.jena.sparql.core.DatasetImpl.getDefaultModel(DatasetImpl.java:103)
    neww.Si.retur(Si.java:35)
    org.apache.jsp.jsp.test1_jsp._jspService(test1_jsp.java:69)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:403)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:347)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)

I am sure that my sparql query is correct as this code works fine!! This code works fine when I run it at a command line but gives above error when I import it in jsp using tomcat.

0

There are 0 best solutions below