Project

General

Profile

h2_sync_1-4-197.patch

Eric Faulhaber, 05/30/2020 03:01 PM

Download (5.81 KB)

View differences:

h2/src/main/org/h2/table/RegularTable.java
534 534
    }
535 535

  
536 536
    private boolean doLock2(Session session, int lockMode, boolean exclusive) {
537
        if (exclusive) {
538
            if (lockExclusiveSession == null) {
539
                if (lockSharedSessions.isEmpty()) {
540
                    traceLock(session, exclusive, "added for");
541
                    session.addLock(this);
542
                    lockExclusiveSession = session;
543
                    return true;
544
                } else if (lockSharedSessions.size() == 1 &&
545
                        lockSharedSessions.contains(session)) {
546
                    traceLock(session, exclusive, "add (upgraded) for ");
547
                    lockExclusiveSession = session;
548
                    return true;
549
                }
550
            }
551
        } else {
552
            if (lockExclusiveSession == null) {
553
                if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
554
                    if (!database.isMultiThreaded() && !database.isMultiVersion()) {
555
                        // READ_COMMITTED: a read lock is acquired,
556
                        // but released immediately after the operation
557
                        // is complete.
558
                        // When allowing only one thread, no lock is
559
                        // required.
560
                        // Row level locks work like read committed.
537
        synchronized (this) {
538
            if (exclusive) {
539
                if (lockExclusiveSession == null) {
540
                    if (lockSharedSessions.isEmpty()) {
541
                        traceLock(session, exclusive, "added for");
542
                        session.addLock(this);
543
                        lockExclusiveSession = session;
544
                        return true;
545
                    } else if (lockSharedSessions.size() == 1 &&
546
                            lockSharedSessions.contains(session)) {
547
                        traceLock(session, exclusive, "add (upgraded) for ");
548
                        lockExclusiveSession = session;
561 549
                        return true;
562 550
                    }
563 551
                }
564
                if (!lockSharedSessions.contains(session)) {
565
                    traceLock(session, exclusive, "ok");
566
                    session.addLock(this);
567
                    lockSharedSessions.add(session);
552
            } else {
553
                if (lockExclusiveSession == null) {
554
                    if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
555
                        if (!database.isMultiThreaded() && !database.isMultiVersion()) {
556
                            // READ_COMMITTED: a read lock is acquired,
557
                            // but released immediately after the operation
558
                            // is complete.
559
                            // When allowing only one thread, no lock is
560
                            // required.
561
                            // Row level locks work like read committed.
562
                            return true;
563
                        }
564
                    }
565
                    if (!lockSharedSessions.contains(session)) {
566
                        traceLock(session, exclusive, "ok");
567
                        session.addLock(this);
568
                        lockSharedSessions.add(session);
569
                    }
570
                    return true;
568 571
                }
569
                return true;
570 572
            }
571 573
        }
572 574
        return false;
......
639 641
                    }
640 642
                }
641 643
            }
642
            if (error == null && lockExclusiveSession != null) {
643
                Table t = lockExclusiveSession.getWaitForLock();
644
                if (t != null) {
645
                    error = t.checkDeadlock(lockExclusiveSession, clash, visited);
646
                    if (error != null) {
647
                        error.add(session);
644
            synchronized (this)
645
            {
646
                if (error == null && lockExclusiveSession != null) {
647
                    Table t = lockExclusiveSession.getWaitForLock();
648
                    if (t != null) {
649
                        error = t.checkDeadlock(lockExclusiveSession, clash, visited);
650
                        if (error != null) {
651
                            error.add(session);
652
                        }
648 653
                    }
649 654
                }
650 655
            }
......
667 672
    @Override
668 673
    public void unlock(Session s) {
669 674
        if (database != null) {
670
            traceLock(s, lockExclusiveSession == s, "unlock");
671
            if (lockExclusiveSession == s) {
672
                lockExclusiveSession = null;
673
            }
674
            synchronized (database) {
675
            synchronized (this) {
676
                traceLock(s, lockExclusiveSession == s, "unlock");
677
                if (lockExclusiveSession == s) {
678
                    lockExclusiveSession = null;
679
                }
675 680
                if (!lockSharedSessions.isEmpty()) {
676 681
                    lockSharedSessions.remove(s);
677 682
                }
683
            }
684
            synchronized (database) {
678 685
                if (!waitingSessions.isEmpty()) {
679 686
                    database.notifyAll();
680 687
                }
......
720 727
        scanIndex.remove(session);
721 728
        database.removeMeta(session, getId());
722 729
        scanIndex = null;
723
        lockExclusiveSession = null;
730
        synchronized (this)
731
        {
732
            lockExclusiveSession = null;
733
        }
724 734
        lockSharedSessions = null;
725 735
        invalidate();
726 736
    }