Skip to content

Commit 1e22484

Browse files
committed
ErrorContext#store() did not store the outer context correctly. This makes the error message slightly inaccurate when an error occurs in <insert /> with <selectKey order="BEFORE" />.
1 parent ecba29d commit 1e22484

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

src/main/java/org/apache/ibatis/executor/ErrorContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public static ErrorContext instance() {
4444
}
4545

4646
public ErrorContext store() {
47-
stored = this;
48-
LOCAL.set(new ErrorContext());
47+
ErrorContext newContext = new ErrorContext();
48+
newContext.stored = this;
49+
LOCAL.set(newContext);
4950
return LOCAL.get();
5051
}
5152

src/test/java/org/apache/ibatis/binding/BindingTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.ibatis.binding;
1717

18+
import static com.googlecode.catchexception.apis.BDDCatchException.*;
19+
import static org.assertj.core.api.BDDAssertions.then;
1820
import static org.junit.Assert.assertEquals;
1921
import static org.junit.Assert.assertFalse;
2022
import static org.junit.Assert.assertNotNull;
@@ -45,6 +47,7 @@
4547
import org.apache.ibatis.domain.blog.DraftPost;
4648
import org.apache.ibatis.domain.blog.Post;
4749
import org.apache.ibatis.domain.blog.Section;
50+
import org.apache.ibatis.exceptions.PersistenceException;
4851
import org.apache.ibatis.executor.result.DefaultResultHandler;
4952
import org.apache.ibatis.mapping.Environment;
5053
import org.apache.ibatis.session.Configuration;
@@ -141,6 +144,40 @@ public void shouldInsertAuthorWithSelectKey() {
141144
}
142145
}
143146

147+
@Test
148+
public void verifyErrorMessageFromSelectKey() {
149+
try (SqlSession session = sqlSessionFactory.openSession()) {
150+
try {
151+
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
152+
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
153+
when(mapper).insertAuthorInvalidSelectKey(author);
154+
then(caughtException()).isInstanceOf(PersistenceException.class).hasMessageContaining(
155+
"### The error may exist in org/apache/ibatis/binding/BoundAuthorMapper.xml\n" +
156+
"### The error may involve org.apache.ibatis.binding.BoundAuthorMapper.insertAuthorInvalidSelectKey!selectKey\n" +
157+
"### The error occurred while executing a query");
158+
} finally {
159+
session.rollback();
160+
}
161+
}
162+
}
163+
164+
@Test
165+
public void verifyErrorMessageFromInsertAfterSelectKey() {
166+
try (SqlSession session = sqlSessionFactory.openSession()) {
167+
try {
168+
BoundAuthorMapper mapper = session.getMapper(BoundAuthorMapper.class);
169+
Author author = new Author(-1, "cbegin", "******", "[email protected]", "N/A", Section.NEWS);
170+
when(mapper).insertAuthorInvalidInsert(author);
171+
then(caughtException()).isInstanceOf(PersistenceException.class).hasMessageContaining(
172+
"### The error may exist in org/apache/ibatis/binding/BoundAuthorMapper.xml\n" +
173+
"### The error may involve org.apache.ibatis.binding.BoundAuthorMapper.insertAuthorInvalidInsert\n" +
174+
"### The error occurred while executing an update");
175+
} finally {
176+
session.rollback();
177+
}
178+
}
179+
}
180+
144181
@Test
145182
public void shouldInsertAuthorWithSelectKeyAndDynamicParams() {
146183
try (SqlSession session = sqlSessionFactory.openSession()) {

src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,10 @@ public interface BoundAuthorMapper {
3939

4040
int insertAuthor(Author author);
4141

42+
int insertAuthorInvalidSelectKey(Author author);
43+
44+
int insertAuthorInvalidInsert(Author author);
45+
4246
int insertAuthorDynamic(Author author);
4347

4448
//======================================================

src/test/java/org/apache/ibatis/binding/BoundAuthorMapper.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
4-
Copyright 2009-2016 the original author or authors.
4+
Copyright 2009-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -52,6 +52,26 @@
5252
)
5353
</insert>
5454

55+
<insert id="insertAuthorInvalidSelectKey">
56+
<selectKey keyProperty="id" resultType="int" order="BEFORE">
57+
select CCAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
58+
</selectKey>
59+
insert into Author (id,username,password,email,bio,favourite_section)
60+
values(
61+
#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection:VARCHAR}
62+
)
63+
</insert>
64+
65+
<insert id="insertAuthorInvalidInsert">
66+
<selectKey keyProperty="id" resultType="int" order="BEFORE">
67+
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
68+
</selectKey>
69+
insert into Author (id,username,password,email,bio,favourite_section_xyz)
70+
values(
71+
#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection:VARCHAR}
72+
)
73+
</insert>
74+
5575
<insert id="insertAuthorDynamic" parameterType="org.apache.ibatis.domain.blog.Author">
5676
<selectKey keyProperty="id" resultType="int" order="BEFORE">
5777
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1

src/test/java/org/apache/ibatis/executor/ErrorContextTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.ibatis.executor;
1717

18+
import static org.junit.Assert.*;
19+
1820
import org.junit.Test;
1921

2022
public class ErrorContextTest {
@@ -44,4 +46,13 @@ public void shouldShowProgressiveErrorContextBuilding() {
4446

4547
}
4648

49+
@Test
50+
public void verifyStoreRecall() throws Exception {
51+
ErrorContext outer = ErrorContext.instance();
52+
ErrorContext inner = ErrorContext.instance().store();
53+
assertEquals(inner, ErrorContext.instance());
54+
ErrorContext recalled = ErrorContext.instance().recall();
55+
assertEquals(outer, recalled);
56+
assertEquals(outer, ErrorContext.instance());
57+
}
4758
}

0 commit comments

Comments
 (0)