22
22
import java .util .concurrent .atomic .AtomicReference ;
23
23
import java .util .concurrent .atomic .LongAdder ;
24
24
import java .util .concurrent .locks .LockSupport ;
25
- import java .util .concurrent .locks .ReentrantLock ;
26
25
27
26
import org .eclipse .rdf4j .common .annotation .InternalUseOnly ;
27
+ import org .eclipse .rdf4j .common .concurrent .locks .ExclusiveReentrantLockManager ;
28
28
import org .eclipse .rdf4j .common .concurrent .locks .Lock ;
29
29
import org .eclipse .rdf4j .common .concurrent .locks .diagnostics .ConcurrentCleaner ;
30
30
import org .eclipse .rdf4j .common .iteration .CloseableIteration ;
@@ -98,17 +98,16 @@ public abstract class AbstractSailConnection implements SailConnection {
98
98
private final AtomicReference <Thread > activeThread = new AtomicReference <>();
99
99
100
100
@ SuppressWarnings ("FieldMayBeFinal" )
101
- private boolean isOpen = true ;
101
+ private volatile boolean isOpen = true ;
102
102
private static final VarHandle IS_OPEN ;
103
103
104
104
private Thread owner ;
105
105
106
106
/**
107
107
* Lock used to prevent concurrent calls to update methods like addStatement, clear, commit, etc. within a
108
108
* transaction.
109
- *
110
109
*/
111
- private final ReentrantLock updateLock = new ReentrantLock ();
110
+ private final ExclusiveReentrantLockManager updateLock = new ExclusiveReentrantLockManager ();
112
111
private final LongAdder iterationsOpened = new LongAdder ();
113
112
private final LongAdder iterationsClosed = new LongAdder ();
114
113
@@ -200,8 +199,7 @@ public void begin(IsolationLevel isolationLevel) throws SailException {
200
199
activeThread .setRelease (Thread .currentThread ());
201
200
202
201
verifyIsOpen ();
203
-
204
- updateLock .lock ();
202
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
205
203
try {
206
204
if (isActive ()) {
207
205
throw new SailException ("a transaction is already active on this connection." );
@@ -210,8 +208,11 @@ public void begin(IsolationLevel isolationLevel) throws SailException {
210
208
startTransactionInternal ();
211
209
txnActive = true ;
212
210
} finally {
213
- updateLock . unlock ();
211
+ exclusiveLock . release ();
214
212
}
213
+ } catch (InterruptedException e ) {
214
+ Thread .currentThread ().interrupt ();
215
+ throw new SailException (e );
215
216
} finally {
216
217
try {
217
218
activeThread .setRelease (null );
@@ -505,15 +506,19 @@ public final void prepare() throws SailException {
505
506
activeThread .setRelease (Thread .currentThread ());
506
507
verifyIsOpen ();
507
508
508
- updateLock .lock ();
509
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
510
+
509
511
try {
510
512
if (txnActive ) {
511
513
prepareInternal ();
512
514
txnPrepared = true ;
513
515
}
514
516
} finally {
515
- updateLock . unlock ();
517
+ exclusiveLock . release ();
516
518
}
519
+ } catch (InterruptedException e ) {
520
+ Thread .currentThread ().interrupt ();
521
+ throw new SailException (e );
517
522
} finally {
518
523
try {
519
524
activeThread .setRelease (null );
@@ -535,7 +540,8 @@ public final void commit() throws SailException {
535
540
536
541
verifyIsOpen ();
537
542
538
- updateLock .lock ();
543
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
544
+
539
545
try {
540
546
if (txnActive ) {
541
547
if (!txnPrepared ) {
@@ -546,8 +552,11 @@ public final void commit() throws SailException {
546
552
txnPrepared = false ;
547
553
}
548
554
} finally {
549
- updateLock . unlock ();
555
+ exclusiveLock . release ();
550
556
}
557
+ } catch (InterruptedException e ) {
558
+ Thread .currentThread ().interrupt ();
559
+ throw new SailException (e );
551
560
} finally {
552
561
try {
553
562
activeThread .setRelease (null );
@@ -572,7 +581,8 @@ public final void rollback() throws SailException {
572
581
573
582
verifyIsOpen ();
574
583
575
- updateLock .lock ();
584
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
585
+
576
586
try {
577
587
if (txnActive ) {
578
588
try {
@@ -586,8 +596,11 @@ public final void rollback() throws SailException {
586
596
debugEnabled ? new Throwable () : null );
587
597
}
588
598
} finally {
589
- updateLock . unlock ();
599
+ exclusiveLock . release ();
590
600
}
601
+ } catch (InterruptedException e ) {
602
+ Thread .currentThread ().interrupt ();
603
+ throw new SailException (e );
591
604
} finally {
592
605
try {
593
606
activeThread .setRelease (null );
@@ -694,13 +707,17 @@ public final void endUpdate(UpdateContext op) throws SailException {
694
707
695
708
verifyIsOpen ();
696
709
697
- updateLock .lock ();
710
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
711
+
698
712
try {
699
713
verifyIsActive ();
700
714
endUpdateInternal (op );
701
715
} finally {
702
- updateLock . unlock ();
716
+ exclusiveLock . release ();
703
717
}
718
+ } catch (InterruptedException e ) {
719
+ Thread .currentThread ().interrupt ();
720
+ throw new SailException (e );
704
721
} finally {
705
722
try {
706
723
activeThread .setRelease (null );
@@ -750,14 +767,18 @@ public final void clear(Resource... contexts) throws SailException {
750
767
751
768
verifyIsOpen ();
752
769
753
- updateLock .lock ();
770
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
771
+
754
772
try {
755
773
verifyIsActive ();
756
774
clearInternal (contexts );
757
775
statementsRemoved = true ;
758
776
} finally {
759
- updateLock . unlock ();
777
+ exclusiveLock . release ();
760
778
}
779
+ } catch (InterruptedException e ) {
780
+ Thread .currentThread ().interrupt ();
781
+ throw new SailException (e );
761
782
} finally {
762
783
try {
763
784
activeThread .setRelease (null );
@@ -820,13 +841,17 @@ public final void setNamespace(String prefix, String name) throws SailException
820
841
821
842
verifyIsOpen ();
822
843
823
- updateLock .lock ();
844
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
845
+
824
846
try {
825
847
verifyIsActive ();
826
848
setNamespaceInternal (prefix , name );
827
849
} finally {
828
- updateLock . unlock ();
850
+ exclusiveLock . release ();
829
851
}
852
+ } catch (InterruptedException e ) {
853
+ Thread .currentThread ().interrupt ();
854
+ throw new SailException (e );
830
855
} finally {
831
856
try {
832
857
activeThread .setRelease (null );
@@ -848,13 +873,17 @@ public final void removeNamespace(String prefix) throws SailException {
848
873
849
874
verifyIsOpen ();
850
875
851
- updateLock .lock ();
876
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
877
+
852
878
try {
853
879
verifyIsActive ();
854
880
removeNamespaceInternal (prefix );
855
881
} finally {
856
- updateLock . unlock ();
882
+ exclusiveLock . release ();
857
883
}
884
+ } catch (InterruptedException e ) {
885
+ Thread .currentThread ().interrupt ();
886
+ throw new SailException (e );
858
887
} finally {
859
888
try {
860
889
activeThread .setRelease (null );
@@ -873,13 +902,17 @@ public final void clearNamespaces() throws SailException {
873
902
874
903
verifyIsOpen ();
875
904
876
- updateLock .lock ();
905
+ Lock exclusiveLock = updateLock .getExclusiveLock ();
906
+
877
907
try {
878
908
verifyIsActive ();
879
909
clearNamespacesInternal ();
880
910
} finally {
881
- updateLock . unlock ();
911
+ exclusiveLock . release ();
882
912
}
913
+ } catch (InterruptedException e ) {
914
+ Thread .currentThread ().interrupt ();
915
+ throw new SailException (e );
883
916
} finally {
884
917
try {
885
918
activeThread .setRelease (null );
0 commit comments