@@ -397,28 +397,36 @@ private Model parseModel(ModelChange modelChange) {
397
397
private InputStream getRDFResultStream (String query , boolean construct ,
398
398
ModelSerializationFormat resultFormat ) throws RDFServiceException {
399
399
Dataset d = getDataset ();
400
- Query q = createQuery (query );
401
- QueryExecution qe = createQueryExecution (query , q , d );
402
- ByteArrayOutputStream serializedModel = new ByteArrayOutputStream ();
403
400
try {
404
- // TODO pipe this
405
- Model m = construct ? qe .execConstruct () : qe .execDescribe ();
406
- m .write (serializedModel , getSerializationFormatString (resultFormat ));
407
- InputStream result = new ByteArrayInputStream (serializedModel .toByteArray ());
408
- return result ;
401
+ Query q = createQuery (query );
402
+ QueryExecution qe = createQueryExecution (query , q , d );
403
+ ByteArrayOutputStream serializedModel = new ByteArrayOutputStream ();
404
+ try {
405
+ // TODO pipe this
406
+ Model m = construct ? qe .execConstruct () : qe .execDescribe ();
407
+ m .write (serializedModel , getSerializationFormatString (resultFormat ));
408
+ InputStream result = new ByteArrayInputStream (serializedModel .toByteArray ());
409
+ return result ;
410
+ } finally {
411
+ qe .close ();
412
+ }
409
413
} finally {
410
- qe .close ();
414
+ d .close ();
411
415
}
412
416
}
413
417
414
418
private void getRDFModel (String query , boolean construct , Model model ) throws RDFServiceException {
415
419
Dataset d = getDataset ();
416
- Query q = createQuery (query );
417
- QueryExecution qe = createQueryExecution (query , q , d );
418
420
try {
419
- Model m = construct ? qe .execConstruct (model ) : qe .execDescribe (model );
421
+ Query q = createQuery (query );
422
+ QueryExecution qe = createQueryExecution (query , q , d );
423
+ try {
424
+ Model m = construct ? qe .execConstruct (model ) : qe .execDescribe (model );
425
+ } finally {
426
+ qe .close ();
427
+ }
420
428
} finally {
421
- qe .close ();
429
+ d .close ();
422
430
}
423
431
}
424
432
@@ -449,56 +457,68 @@ public InputStream sparqlDescribeQuery(String query,
449
457
public InputStream sparqlSelectQuery (String query , ResultFormat resultFormat )
450
458
throws RDFServiceException {
451
459
Dataset d = getDataset ();
452
- Query q = createQuery (query );
453
- QueryExecution qe = createQueryExecution (query , q , d );
454
460
try {
455
- ResultSet resultSet = qe .execSelect ();
456
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
457
- switch (resultFormat ) {
458
- case CSV :
459
- ResultSetFormatter .outputAsCSV (outputStream ,resultSet );
460
- break ;
461
- case TEXT :
462
- ResultSetFormatter .out (outputStream ,resultSet );
463
- break ;
464
- case JSON :
465
- ResultSetFormatter .outputAsJSON (outputStream , resultSet );
466
- break ;
467
- case XML :
468
- ResultSetFormatter .outputAsXML (outputStream , resultSet );
469
- break ;
470
- default :
471
- throw new RDFServiceException ("unrecognized result format" );
461
+ Query q = createQuery (query );
462
+ QueryExecution qe = createQueryExecution (query , q , d );
463
+ try {
464
+ ResultSet resultSet = qe .execSelect ();
465
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
466
+ switch (resultFormat ) {
467
+ case CSV :
468
+ ResultSetFormatter .outputAsCSV (outputStream ,resultSet );
469
+ break ;
470
+ case TEXT :
471
+ ResultSetFormatter .out (outputStream ,resultSet );
472
+ break ;
473
+ case JSON :
474
+ ResultSetFormatter .outputAsJSON (outputStream , resultSet );
475
+ break ;
476
+ case XML :
477
+ ResultSetFormatter .outputAsXML (outputStream , resultSet );
478
+ break ;
479
+ default :
480
+ throw new RDFServiceException ("unrecognized result format" );
481
+ }
482
+ InputStream result = new ByteArrayInputStream (outputStream .toByteArray ());
483
+ return result ;
484
+ } finally {
485
+ qe .close ();
472
486
}
473
- InputStream result = new ByteArrayInputStream (outputStream .toByteArray ());
474
- return result ;
475
487
} finally {
476
- qe .close ();
488
+ d .close ();
477
489
}
478
490
}
479
491
480
492
@ Override
481
493
public void sparqlSelectQuery (String query , ResultSetConsumer consumer )
482
494
throws RDFServiceException {
483
495
Dataset d = getDataset ();
484
- Query q = createQuery (query );
485
- QueryExecution qe = createQueryExecution (query , q , d );
486
496
try {
487
- consumer .processResultSet (qe .execSelect ());
497
+ Query q = createQuery (query );
498
+ QueryExecution qe = createQueryExecution (query , q , d );
499
+ try {
500
+ consumer .processResultSet (qe .execSelect ());
501
+ } finally {
502
+ qe .close ();
503
+ }
488
504
} finally {
489
- qe .close ();
505
+ d .close ();
490
506
}
491
507
}
492
508
493
509
@ Override
494
510
public boolean sparqlAskQuery (String query ) throws RDFServiceException {
495
511
Dataset d = getDataset ();
496
- Query q = createQuery (query );
497
- QueryExecution qe = createQueryExecution (query , q , d );
498
512
try {
499
- return qe .execAsk ();
513
+ Query q = createQuery (query );
514
+ QueryExecution qe = createQueryExecution (query , q , d );
515
+ try {
516
+ return qe .execAsk ();
517
+ } finally {
518
+ qe .close ();
519
+ }
500
520
} finally {
501
- qe .close ();
521
+ d .close ();
502
522
}
503
523
}
504
524
@@ -507,15 +527,16 @@ public List<String> getGraphURIs() throws RDFServiceException {
507
527
if (rebuildGraphURICache ) {
508
528
synchronized (RDFServiceJena .class ) {
509
529
if (rebuildGraphURICache ) {
530
+ Dataset d = getDataset ();
510
531
try {
511
- Dataset d = getDataset ();
512
532
Iterator <String > nameIt = d .listNames ();
513
533
graphURIs .clear ();
514
534
while (nameIt .hasNext ()) {
515
535
graphURIs .add (nameIt .next ());
516
536
}
517
537
return graphURIs ;
518
538
} finally {
539
+ d .close ();
519
540
rebuildGraphURICache = false ;
520
541
}
521
542
}
@@ -546,19 +567,23 @@ public void serializeGraph(String graphURI, OutputStream outputStream)
546
567
547
568
private void serialize (OutputStream outputStream , String query ) throws RDFServiceException {
548
569
Dataset d = getDataset ();
549
- Query q = createQuery (query );
550
- QueryExecution qe = createQueryExecution (query , q , d );
551
570
try {
552
- ResultSet resultSet = qe .execSelect ();
553
- if (resultSet .getResultVars ().contains ("g" )) {
554
- Iterator <Quad > quads = new ResultSetQuadsIterator (resultSet );
555
- RDFDataMgr .writeQuads (outputStream , quads );
556
- } else {
557
- Iterator <Triple > triples = new ResultSetTriplesIterator (resultSet );
558
- RDFDataMgr .writeTriples (outputStream , triples );
571
+ Query q = createQuery (query );
572
+ QueryExecution qe = createQueryExecution (query , q , d );
573
+ try {
574
+ ResultSet resultSet = qe .execSelect ();
575
+ if (resultSet .getResultVars ().contains ("g" )) {
576
+ Iterator <Quad > quads = new ResultSetQuadsIterator (resultSet );
577
+ RDFDataMgr .writeQuads (outputStream , quads );
578
+ } else {
579
+ Iterator <Triple > triples = new ResultSetTriplesIterator (resultSet );
580
+ RDFDataMgr .writeTriples (outputStream , triples );
581
+ }
582
+ } finally {
583
+ qe .close ();
559
584
}
560
585
} finally {
561
- qe .close ();
586
+ d .close ();
562
587
}
563
588
}
564
589
@@ -612,6 +637,8 @@ public long countTriples(RDFNode subject, RDFNode predicate, RDFNode object) thr
612
637
Literal literal = soln .getLiteral ("count" );
613
638
return literal .getLong ();
614
639
}
640
+ } finally {
641
+ d .close ();
615
642
}
616
643
617
644
return 0 ;
@@ -637,8 +664,12 @@ public Model getTriples(RDFNode subject, RDFNode predicate, RDFNode object, long
637
664
Model triples = ModelFactory .createDefaultModel ();
638
665
639
666
Dataset d = getDataset ();
640
- try (QueryExecution qexec = QueryExecutionFactory .create (query , d , map )) {
641
- qexec .execConstruct (triples );
667
+ try {
668
+ try (QueryExecution qexec = QueryExecutionFactory .create (query , d , map )) {
669
+ qexec .execConstruct (triples );
670
+ }
671
+ } finally {
672
+ d .close ();
642
673
}
643
674
644
675
return triples ;
0 commit comments