From dddd9f731be3ef448900a6fbe865d8967877a704 Mon Sep 17 00:00:00 2001 From: willie Date: Wed, 3 Apr 2024 12:04:40 +0200 Subject: [PATCH 1/3] feat: allow `DefaultSqlSessionFactory` to provide a custom `SqlSession` --- .../session/defaults/DefaultSqlSession.java | 2 +- .../defaults/DefaultSqlSessionFactory.java | 10 +++-- .../session/defaults/ExtendedSqlSession.java | 27 ++++++++++++ .../defaults/ExtendedSqlSessionFactory.java | 32 ++++++++++++++ .../ExtendedSqlSessionFactoryBuilder.java | 28 ++++++++++++ .../ExtendedSqlSessionFactoryTest.java | 43 +++++++++++++++++++ 6 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSession.java create mode 100644 src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactory.java create mode 100644 src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryBuilder.java create mode 100644 src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java diff --git a/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java b/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java index 62b39becea5..4eb1bb20eb7 100644 --- a/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java +++ b/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 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. diff --git a/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSessionFactory.java b/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSessionFactory.java index f30f308746f..55281b7d6c8 100644 --- a/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSessionFactory.java +++ b/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSessionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2023 the original author or authors. + * Copyright 2009-2024 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. @@ -87,6 +87,10 @@ public Configuration getConfiguration() { return configuration; } + protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { + return new DefaultSqlSession(configuration, executor, autoCommit); + } + private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; @@ -95,7 +99,7 @@ private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionI final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType); - return new DefaultSqlSession(configuration, executor, autoCommit); + return createSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); @@ -118,7 +122,7 @@ private SqlSession openSessionFromConnection(ExecutorType execType, Connection c final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); final Transaction tx = transactionFactory.newTransaction(connection); final Executor executor = configuration.newExecutor(tx, execType); - return new DefaultSqlSession(configuration, executor, autoCommit); + return createSqlSession(configuration, executor, autoCommit); } catch (Exception e) { throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { diff --git a/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSession.java b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSession.java new file mode 100644 index 00000000000..27a31478fbc --- /dev/null +++ b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSession.java @@ -0,0 +1,27 @@ +/* + * Copyright 2009-2024 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 + * + * https://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.apache.ibatis.session.defaults; + +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.session.Configuration; + +public class ExtendedSqlSession extends DefaultSqlSession { + + public ExtendedSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { + super(configuration, executor, autoCommit); + } + +} diff --git a/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactory.java b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactory.java new file mode 100644 index 00000000000..0b75d43920b --- /dev/null +++ b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactory.java @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2024 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 + * + * https://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.apache.ibatis.session.defaults; + +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSession; + +public class ExtendedSqlSessionFactory extends DefaultSqlSessionFactory { + + public ExtendedSqlSessionFactory(Configuration configuration) { + super(configuration); + } + + @Override + protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) { + return new ExtendedSqlSession(configuration, executor, autoCommit); + } +} diff --git a/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryBuilder.java b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryBuilder.java new file mode 100644 index 00000000000..0ef0ddea1cd --- /dev/null +++ b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryBuilder.java @@ -0,0 +1,28 @@ +/* + * Copyright 2009-2024 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 + * + * https://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.apache.ibatis.session.defaults; + +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; + +public class ExtendedSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder { + + @Override + public SqlSessionFactory build(Configuration config) { + return new ExtendedSqlSessionFactory(config); + } +} diff --git a/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java new file mode 100644 index 00000000000..00bfcc7b498 --- /dev/null +++ b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java @@ -0,0 +1,43 @@ +/* + * Copyright 2009-2024 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 + * + * https://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.apache.ibatis.session.defaults; + +import java.io.Reader; + +import org.apache.ibatis.BaseDataTest; +import org.apache.ibatis.io.Resources; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.assertj.core.api.Assertions; +import org.junit.Test; + +public class ExtendedSqlSessionFactoryTest extends BaseDataTest { + + @Test + public void testCanUseExtendedSqlSessionUsingDefaults() throws Exception { + createBlogDataSource(); + + final String resource = "org/apache/ibatis/builder/MapperConfig.xml"; + final Reader reader = Resources.getResourceAsReader(resource); + + SqlSessionFactory sessionFactory = new ExtendedSqlSessionFactoryBuilder().build(reader); + Assertions.assertThat(sessionFactory).isNotNull(); + + try (SqlSession sqlSession = sessionFactory.openSession()) { + Assertions.assertThat(sqlSession).isNotNull().isInstanceOf(ExtendedSqlSession.class); + }; + } +} From 3a9321e459cc3d167db310d5175c2b25f0caa08e Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Wed, 29 May 2024 04:00:47 +0900 Subject: [PATCH 2/3] Removed unnecessary semicolon --- .../ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java index 00bfcc7b498..54c69e1cafc 100644 --- a/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java +++ b/src/test/java/org/apache/ibatis/session/defaults/ExtendedSqlSessionFactoryTest.java @@ -38,6 +38,6 @@ public void testCanUseExtendedSqlSessionUsingDefaults() throws Exception { try (SqlSession sqlSession = sessionFactory.openSession()) { Assertions.assertThat(sqlSession).isNotNull().isInstanceOf(ExtendedSqlSession.class); - }; + } } } From 67c6babee6d38522191b4b2bf6b051d4f150046c Mon Sep 17 00:00:00 2001 From: Iwao AVE! Date: Wed, 29 May 2024 07:30:55 +0900 Subject: [PATCH 3/3] Revert the license year to 2023 --- .../org/apache/ibatis/session/defaults/DefaultSqlSession.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java b/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java index 4eb1bb20eb7..62b39becea5 100644 --- a/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java +++ b/src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2024 the original author or authors. + * Copyright 2009-2023 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.