Skip to content

Commit eccb9c6

Browse files
committed
Added failing tests
Some DBs support INSERT, UPDATE or DELETE statement that returns result set. PostgreSQL has RETURNING MS SQL Server has OUTPUT MyBatis can return results by using @select, @SelectProvider or <select />, however, rollback does not work as expected because SELECT does not mark the session 'dirty'.
1 parent aad75eb commit eccb9c6

File tree

5 files changed

+213
-0
lines changed

5 files changed

+213
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.dirty_select;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import org.apache.ibatis.BaseDataTest;
21+
import org.apache.ibatis.mapping.Environment;
22+
import org.apache.ibatis.session.Configuration;
23+
import org.apache.ibatis.session.SqlSession;
24+
import org.apache.ibatis.session.SqlSessionFactory;
25+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26+
import org.apache.ibatis.testcontainers.PgContainer;
27+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Tag;
30+
import org.junit.jupiter.api.Test;
31+
32+
@Tag("TestcontainersTests")
33+
class DirtySelectTest {
34+
35+
private static SqlSessionFactory sqlSessionFactory;
36+
37+
@BeforeAll
38+
static void setUp() throws Exception {
39+
Configuration configuration = new Configuration();
40+
Environment environment = new Environment("development", new JdbcTransactionFactory(),
41+
PgContainer.getUnpooledDataSource());
42+
configuration.setEnvironment(environment);
43+
configuration.addMapper(Mapper.class);
44+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
45+
46+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47+
"org/apache/ibatis/submitted/dirty_select/CreateDB.sql");
48+
}
49+
50+
@Test
51+
void shouldRollbackIfCalled() {
52+
Integer id;
53+
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
54+
Mapper mapper = sqlSession.getMapper(Mapper.class);
55+
User user = mapper.insertReturn("Jimmy");
56+
id = user.getId();
57+
assertNotNull(id);
58+
assertEquals("Jimmy", user.getName());
59+
sqlSession.rollback();
60+
}
61+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62+
Mapper mapper = sqlSession.getMapper(Mapper.class);
63+
User user = mapper.selectById(id);
64+
assertNull(user);
65+
}
66+
}
67+
68+
@Test
69+
void shouldRollbackIfCalled_Xml() {
70+
Integer id;
71+
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
72+
Mapper mapper = sqlSession.getMapper(Mapper.class);
73+
User user = mapper.insertReturnXml("Jimmy");
74+
id = user.getId();
75+
assertNotNull(id);
76+
assertEquals("Jimmy", user.getName());
77+
sqlSession.rollback();
78+
}
79+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
80+
Mapper mapper = sqlSession.getMapper(Mapper.class);
81+
User user = mapper.selectById(id);
82+
assertNull(user);
83+
}
84+
}
85+
86+
@Test
87+
void shouldRollbackIfCalled_Provider() {
88+
Integer id;
89+
try (SqlSession sqlSession = sqlSessionFactory.openSession(false)) {
90+
Mapper mapper = sqlSession.getMapper(Mapper.class);
91+
User user = mapper.insertReturnProvider("Jimmy");
92+
id = user.getId();
93+
assertNotNull(id);
94+
assertEquals("Jimmy", user.getName());
95+
sqlSession.rollback();
96+
}
97+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98+
Mapper mapper = sqlSession.getMapper(Mapper.class);
99+
User user = mapper.selectById(id);
100+
assertNull(user);
101+
}
102+
}
103+
104+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.dirty_select;
17+
18+
import org.apache.ibatis.annotations.Select;
19+
import org.apache.ibatis.annotations.SelectProvider;
20+
21+
public interface Mapper {
22+
23+
@Select("select * from users where id = #{id}")
24+
User selectById(Integer id);
25+
26+
@Select(value = "insert into users (name) values (#{name}) returning id, name")
27+
User insertReturn(String name);
28+
29+
User insertReturnXml(String name);
30+
31+
@SelectProvider(type = MyProvider.class, method = "getSql")
32+
User insertReturnProvider(String name);
33+
34+
static class MyProvider {
35+
public static String getSql() {
36+
return "insert into users (name) values (#{name}) returning id, name";
37+
}
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.dirty_select;
17+
18+
public class User {
19+
20+
private Integer id;
21+
private String name;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--
2+
-- Copyright 2009-2022 the original author or authors.
3+
--
4+
-- Licensed under the Apache License, Version 2.0 (the "License");
5+
-- you may not use this file except in compliance with the License.
6+
-- You may obtain a copy of the License at
7+
--
8+
-- https://www.apache.org/licenses/LICENSE-2.0
9+
--
10+
-- Unless required by applicable law or agreed to in writing, software
11+
-- distributed under the License is distributed on an "AS IS" BASIS,
12+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
-- See the License for the specific language governing permissions and
14+
-- limitations under the License.
15+
--
16+
17+
create table users (
18+
id serial primary key,
19+
name varchar(30)
20+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper
4+
namespace="org.apache.ibatis.submitted.dirty_select.Mapper">
5+
6+
<select id="insertReturnXml"
7+
resultType="org.apache.ibatis.submitted.dirty_select.User">
8+
insert into users (name) values (#{name})
9+
returning id, name
10+
</select>
11+
12+
</mapper>

0 commit comments

Comments
 (0)