1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.heavy_initial_load;
17
18 import java.io.Reader;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.apache.ibatis.io.Resources;
24 import org.apache.ibatis.session.SqlSession;
25 import org.apache.ibatis.session.SqlSessionFactory;
26 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30
31 class HeavyInitialLoadTest {
32
33 private static SqlSessionFactory sqlSessionFactory;
34
35 @BeforeAll
36 static void initSqlSessionFactory() throws Exception {
37 try (Reader reader = Resources
38 .getResourceAsReader("org/apache/ibatis/submitted/heavy_initial_load/ibatisConfig.xml")) {
39 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40 }
41
42 sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection().close();
43 }
44
45 private static final int THREAD_COUNT = 5;
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @Test
62 void selectThingsConcurrently_mybatis_issue_224() throws Exception {
63 final List<Throwable> throwables = Collections.synchronizedList(new ArrayList<>());
64
65 Thread[] threads = new Thread[THREAD_COUNT];
66 for (int i = 0; i < THREAD_COUNT; i++) {
67 threads[i] = new Thread(() -> {
68 try {
69 selectThing();
70 } catch (Exception exception) {
71 throwables.add(exception);
72 }
73 });
74
75 threads[i].start();
76 }
77
78 for (int i = 0; i < THREAD_COUNT; i++) {
79 threads[i].join();
80 }
81
82 Assertions.assertTrue(throwables.isEmpty(), "There were exceptions: " + throwables);
83 }
84
85 void selectThing() {
86 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
87 ThingMapper mapper = sqlSession.getMapper(ThingMapper.class);
88 Thing selected = mapper.selectByCode(Code._1);
89 Assertions.assertEquals(1, selected.getId().longValue());
90 }
91 }
92 }