-
Notifications
You must be signed in to change notification settings - Fork 361
DATAJDBC-161: Share an SqlSession into a same transaction #29
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/test/java/org/springframework/data/jdbc/mybatis/MyBatisBatchHsqlIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright 2017 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc.mybatis; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Iterator; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.ExecutorType; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mybatis.spring.SqlSessionFactoryBean; | ||
import org.mybatis.spring.SqlSessionTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; | ||
import org.springframework.data.jdbc.testing.TestConfiguration; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.rules.SpringClassRule; | ||
import org.springframework.test.context.junit4.rules.SpringMethodRule; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
/** | ||
* Tests the integration with Mybatis batch mode. | ||
* | ||
* @author Kazuki Shimizu | ||
*/ | ||
@ContextConfiguration | ||
@Transactional | ||
public class MyBatisBatchHsqlIntegrationTests { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again I don't understand the connection to the issue this tries to fix. |
||
|
||
@org.springframework.context.annotation.Configuration | ||
@Import(TestConfiguration.class) | ||
@EnableJdbcRepositories(considerNestedRepositories = true) | ||
static class Config { | ||
|
||
@Bean | ||
Class<?> testClass() { | ||
return MyBatisBatchHsqlIntegrationTests.class; | ||
} | ||
|
||
@Bean | ||
SqlSessionFactoryBean createSessionFactory(EmbeddedDatabase db) { | ||
|
||
Configuration configuration = new Configuration(); | ||
configuration.setDefaultExecutorType(ExecutorType.BATCH); | ||
configuration.getTypeAliasRegistry().registerAlias("MyBatisContext", MyBatisContext.class); | ||
|
||
configuration.getTypeAliasRegistry().registerAlias(DummyEntity.class); | ||
configuration.addMapper(DummyEntityMapper.class); | ||
|
||
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); | ||
sqlSessionFactoryBean.setDataSource(db); | ||
sqlSessionFactoryBean.setConfiguration(configuration); | ||
|
||
return sqlSessionFactoryBean; | ||
} | ||
|
||
@Bean | ||
SqlSessionTemplate SqlSessionTemplate(SqlSessionFactory factory) { | ||
return new SqlSessionTemplate(factory); | ||
} | ||
|
||
@Bean | ||
MyBatisDataAccessStrategy dataAccessStrategy(SqlSessionTemplate template) { | ||
return new MyBatisDataAccessStrategy(template); | ||
} | ||
} | ||
|
||
@ClassRule public static final SpringClassRule classRule = new SpringClassRule(); | ||
@Rule public SpringMethodRule methodRule = new SpringMethodRule(); | ||
|
||
@Autowired DummyEntityRepository repository; | ||
|
||
@Test | ||
public void batchInsertAndSelect() { | ||
|
||
DummyEntity entity1 = new DummyEntity("some name"); | ||
DummyEntity saved1 = repository.save(entity1); | ||
|
||
DummyEntity entity2 = new DummyEntity("some name"); | ||
DummyEntity saved2 = repository.save(entity2); | ||
|
||
assertThat(saved1.id).isEqualTo(0); | ||
assertThat(saved2.id).isEqualTo(0); | ||
|
||
Iterator<DummyEntity> loadedEntities = repository.findAll().iterator(); | ||
|
||
DummyEntity loaded1 = loadedEntities.next(); | ||
assertThat(saved1.id).isNotEqualTo(0); | ||
assertThat(loaded1.id).isEqualTo(saved1.id); | ||
assertThat(loaded1).isNotNull().extracting(e -> e.id, e -> e.name); | ||
|
||
DummyEntity loaded2 = loadedEntities.next(); | ||
assertThat(saved2.id).isNotEqualTo(0); | ||
assertThat(loaded2.id).isEqualTo(saved2.id); | ||
assertThat(loaded2).isNotNull().extracting(e -> e.id, e -> e.name); | ||
|
||
assertThat(loadedEntities.hasNext()).isFalse(); | ||
|
||
} | ||
|
||
interface DummyEntityRepository extends CrudRepository<DummyEntity, Long> { | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...resources/org.springframework.data.jdbc.mybatis/MyBatisBatchHsqlIntegrationTests-hsql.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE TABLE dummyentity(id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,19 @@ | |
<insert id="insert" parameterType="MyBatisContext" useGeneratedKeys="true" keyProperty="instance.id" keyColumn="ID"> | ||
INSERT INTO DummyEntity (id) VALUES (DEFAULT) | ||
</insert> | ||
<select id="findById" resultType="MyBatisContext" resultMap="dummyEntityMap"> | ||
<select id="findById" resultMap="dummyEntityMap"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again possibly unrelated to the issue at hand. |
||
SELECT | ||
id, | ||
'Name based on an id' || id AS name | ||
FROM DummyEntity | ||
WHERE id = #{id} | ||
</select> | ||
<select id="findAll" resultMap="dummyEntityMap"> | ||
SELECT | ||
id, | ||
'Name based on an id' || id AS name | ||
FROM DummyEntity | ||
ORDER BY id | ||
</select> | ||
|
||
</mapper> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems unrelated to DATAJDBC-161, possibly related to DATAJDBC-160