Skip to content

Commit

Permalink
Merge pull request #3128 from epochcoder/feat/custom-sql-session-usin…
Browse files Browse the repository at this point in the history
…g-defaults

feat: allow `DefaultSqlSessionFactory` to provide a custom `SqlSession`
  • Loading branch information
harawata committed May 28, 2024
2 parents 9839141 + 67c6bab commit 94e185b
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 94e185b

Please sign in to comment.