From 66556f93c4ec1cfa6ea2bae50d3d4a7bd0025d39 Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Sun, 7 Jan 2018 21:45:48 +0900 Subject: [PATCH] DATAJDBC-161 - Share a SqlSession between statements. Original pull request: #29. --- .../jdbc/mybatis/MyBatisDataAccessStrategy.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java b/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java index 63957b0e5..45da7b450 100644 --- a/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java +++ b/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java @@ -20,6 +20,7 @@ import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionTemplate; import org.springframework.data.jdbc.core.DataAccessStrategy; import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty; import org.springframework.data.mapping.PropertyPath; @@ -33,16 +34,22 @@ import org.springframework.data.mapping.PropertyPath; * methods taking a {@link PropertyPath} the entityTyoe if the context is set to the class of the leaf type. * * @author Jens Schauder + * @author Kazuki Shimizu */ public class MyBatisDataAccessStrategy implements DataAccessStrategy { private static final String MAPPER_SUFFIX = "Mapper"; - private final SqlSessionFactory sqlSessionFactory; + private final SqlSession sqlSession; public MyBatisDataAccessStrategy(SqlSessionFactory sqlSessionFactory) { - this.sqlSessionFactory = sqlSessionFactory; + this(new SqlSessionTemplate(sqlSessionFactory)); + } + + public MyBatisDataAccessStrategy(SqlSessionTemplate sqlSessionTemplate) { + + this.sqlSession = sqlSessionTemplate; } @Override @@ -135,7 +142,7 @@ public class MyBatisDataAccessStrategy implements DataAccessStrategy { } private SqlSession sqlSession() { - return sqlSessionFactory.openSession(); + return this.sqlSession; } private String toDashPath(PropertyPath propertyPath) {