2

I am using apache tomcat with eclipse keplee .this is my jsp file which runs a java file which queries from tdb using sparql . jsp file :

<%@ page import="check.test4query"  %>
            <% test4query demo = new test4query();
            test4query dem = new test4query();
            String[] id =new String[20];
            String[] dat =new String[20];
            int i;
           demo.mai("SELECT ?x WHERE { ?y <TO:> 'hjcooljohny75@gmail.com' . ?y <SUB:> ?x} LIMIT 20 ");
           for(i=0;i<20;i++)
           {   id[i]=test4query.arr[i];
               id[i] = id[i].substring(0, Math.min(id[i].length(), 30));
           }
 for(i=0;i<20;i++)
            { //id[i]=test4query.arr[i];
                out.println("<tr>"+"&nbsp&nbsp&nbsp&nbsp"+id[i]+"<hr style='border-color:#E6E6E6;padding:0px;margin:0px'>"+"</tr>");
            }   
            %>

this is my test4query :

public static String[] arr=new String[20];
public  void  mai (String s) {
    //String s;
    //load the dataset 
    //String query1; 
    //query1="hjcooljohny75@gmail.com";
    //query1 = (String)(subjectentry.getText());
      //  s="SELECT ?x WHERE { ?y <TO:> '"+query1+"' . ?y <SUB:> ?x} LIMIT 20 ";
        System.out.println(s);
    String directory = "EMAILADDRESS" ;
    Dataset ds = TDBFactory.createDataset(directory) ;
    Model model = ds.getDefaultModel() ;
   ds.begin(ReadWrite.READ) ;
     QueryExecution qExec = QueryExecutionFactory.create(s, ds) ;
     int i=0;
     try{
     ResultSet rs = qExec.execSelect() ;
   String x=rs.toString();



    while (rs.hasNext()) {
        QuerySolution qs = rs.next();
        String rds;
        if(qs.get("x")!=null)
        rds = qs.get("x").toString();
        else rds="hi";
       // String em = (String)rs.getString();
        if(rds==null)
            break;
        //System.out.println(rds);
       arr[i] = rds;
       i++;
       //for (int i =0; i < arr.length; i++){


   }
   } finally
    {qExec.close() ;
    ds.commit();
    ds.end();
   }
    for( i=0;i<20;i++)
        System.out.println(arr[i]);
    //arr[0]="hi";


// return arr;
         //   try { 


          //      ResultSetFormatter.out(rs) ;
           // } finally { qExec.close() ; }

        // Another query - same view of the data.

}

The problem is when I start the tomcat server it runs perfectly showing the results but after that if I refresh the page it shows 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)
    check.test4query.mai(test4query.java:59)
    org.apache.jsp.grayscale.gmail_005flike_jsp._jspService(gmail_005flike_jsp.java:210)
    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)

and this happens all the time but when I restart the server it shows correct ans for the first time and then error..I don't know what is the problem and how to correct it??

cooljohny
  • 656
  • 5
  • 13
  • 31

2 Answers2

3

This is the wrong way round:

Model model = ds.getDefaultModel() ;
ds.begin(ReadWrite.READ) ;

Try instead:

ds.begin(ReadWrite.READ) ;
Model model = ds.getDefaultModel() ;
AndyS
  • 16,345
  • 17
  • 21
  • Thanks a lot andy...I never expected that this could be the reason for this problem...I wasted hours solving this and your solution just made my day... :) – cooljohny Jun 13 '14 at 17:34
  • I am facing one more problem...when I try to write...your solution doesn't gives error but don't write anything to my database but when I first create model and then write ds.begin(ReadWrite.WRITE) ; then it works fine but gives error in tomcat..what should I do? – cooljohny Jun 27 '14 at 06:40
  • This is the link to the question that I asked... http://stackoverflow.com/questions/24349035/errors-in-transaction-in-jena-tdb – cooljohny Jun 27 '14 at 13:05
1

The problem is that you are leaking resources and not cleaning up after yourself

You start a transaction with your ds.begin(ReadWrite.READ) call but you don't ever close the transaction with a ds.end() call which is why subsequent calls fail with the error you see. You also fail to ever close your QueryExecution

You should use a try { } catch { } finally { } to ensure that you clean up relevant resources

RobV
  • 28,022
  • 11
  • 77
  • 119