Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change in PR #2044 has a risk of death lock #2049

Closed
wants to merge 10 commits into from
Expand Up @@ -16,8 +16,10 @@
package org.apache.ibatis.submitted.blocking_cache;

import java.io.Reader;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
Expand Down Expand Up @@ -99,4 +101,45 @@ void ensureLockIsReleasedOnRollback() {
mapper.findAll();
}
}

@Test
void blockCacheDeathLockTest() throws InterruptedException {
CountDownLatch step = new CountDownLatch(2);
CountDownLatch waitForExecute = new CountDownLatch(2);

new Thread(() -> {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
mapper.findById(1);
step.countDown();
step.await();

mapper.findById(2);
sqlSession.commit();
} catch (InterruptedException e) {
//ignore
}
waitForExecute.countDown();
}).start();

new Thread(() -> {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
mapper.findById(2);
step.countDown();
step.await();

mapper.findById(1);
sqlSession.commit();
} catch (InterruptedException e) {
//ignore
}
waitForExecute.countDown();
}).start();

boolean await = waitForExecute.await(30, TimeUnit.SECONDS);
if (!await) {
throw new RuntimeException("BlockingCache has a risk of death lock!");
}
}
}
Expand Up @@ -27,6 +27,9 @@ public interface PersonMapper {
@Select("select id, firstname, lastname from person")
List<Person> findAll();

@Select("select id, firstname, lastname from person where id = #{id}")
List<Person> findById(int id);

@Delete("delete from person where id = #{id}")
int delete(int id);
}