Skip to content

Commit 94e185b

Browse files
authored
Merge pull request #3128 from epochcoder/feat/custom-sql-session-using-defaults
feat: allow `DefaultSqlSessionFactory` to provide a custom `SqlSession`
2 parents 9839141 + 67c6bab commit 94e185b

File tree

5 files changed

+137
-3
lines changed

5 files changed

+137
-3
lines changed

src/main/java/org/apache/ibatis/session/defaults/DefaultSqlSessionFactory.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 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.
@@ -87,6 +87,10 @@ public Configuration getConfiguration() {
8787
return configuration;
8888
}
8989

90+
protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
91+
return new DefaultSqlSession(configuration, executor, autoCommit);
92+
}
93+
9094
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level,
9195
boolean autoCommit) {
9296
Transaction tx = null;
@@ -95,7 +99,7 @@ private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionI
9599
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
96100
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
97101
final Executor executor = configuration.newExecutor(tx, execType);
98-
return new DefaultSqlSession(configuration, executor, autoCommit);
102+
return createSqlSession(configuration, executor, autoCommit);
99103
} catch (Exception e) {
100104
closeTransaction(tx); // may have fetched a connection so lets call close()
101105
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
@@ -118,7 +122,7 @@ private SqlSession openSessionFromConnection(ExecutorType execType, Connection c
118122
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
119123
final Transaction tx = transactionFactory.newTransaction(connection);
120124
final Executor executor = configuration.newExecutor(tx, execType);
121-
return new DefaultSqlSession(configuration, executor, autoCommit);
125+
return createSqlSession(configuration, executor, autoCommit);
122126
} catch (Exception e) {
123127
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
124128
} finally {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2009-2024 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.session.defaults;
17+
18+
import org.apache.ibatis.executor.Executor;
19+
import org.apache.ibatis.session.Configuration;
20+
21+
public class ExtendedSqlSession extends DefaultSqlSession {
22+
23+
public ExtendedSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
24+
super(configuration, executor, autoCommit);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2009-2024 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.session.defaults;
17+
18+
import org.apache.ibatis.executor.Executor;
19+
import org.apache.ibatis.session.Configuration;
20+
import org.apache.ibatis.session.SqlSession;
21+
22+
public class ExtendedSqlSessionFactory extends DefaultSqlSessionFactory {
23+
24+
public ExtendedSqlSessionFactory(Configuration configuration) {
25+
super(configuration);
26+
}
27+
28+
@Override
29+
protected SqlSession createSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
30+
return new ExtendedSqlSession(configuration, executor, autoCommit);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2009-2024 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.session.defaults;
17+
18+
import org.apache.ibatis.session.Configuration;
19+
import org.apache.ibatis.session.SqlSessionFactory;
20+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
21+
22+
public class ExtendedSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
23+
24+
@Override
25+
public SqlSessionFactory build(Configuration config) {
26+
return new ExtendedSqlSessionFactory(config);
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2009-2024 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.session.defaults;
17+
18+
import java.io.Reader;
19+
20+
import org.apache.ibatis.BaseDataTest;
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.session.SqlSession;
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.assertj.core.api.Assertions;
25+
import org.junit.Test;
26+
27+
public class ExtendedSqlSessionFactoryTest extends BaseDataTest {
28+
29+
@Test
30+
public void testCanUseExtendedSqlSessionUsingDefaults() throws Exception {
31+
createBlogDataSource();
32+
33+
final String resource = "org/apache/ibatis/builder/MapperConfig.xml";
34+
final Reader reader = Resources.getResourceAsReader(resource);
35+
36+
SqlSessionFactory sessionFactory = new ExtendedSqlSessionFactoryBuilder().build(reader);
37+
Assertions.assertThat(sessionFactory).isNotNull();
38+
39+
try (SqlSession sqlSession = sessionFactory.openSession()) {
40+
Assertions.assertThat(sqlSession).isNotNull().isInstanceOf(ExtendedSqlSession.class);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)