SqlSessionFactoryBuilder.java

  1. /*
  2.  *    Copyright 2009-2023 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;

  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.Reader;
  20. import java.util.Properties;

  21. import org.apache.ibatis.builder.xml.XMLConfigBuilder;
  22. import org.apache.ibatis.exceptions.ExceptionFactory;
  23. import org.apache.ibatis.executor.ErrorContext;
  24. import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

  25. /**
  26.  * Builds {@link SqlSession} instances.
  27.  *
  28.  * @author Clinton Begin
  29.  */
  30. public class SqlSessionFactoryBuilder {

  31.   public SqlSessionFactory build(Reader reader) {
  32.     return build(reader, null, null);
  33.   }

  34.   public SqlSessionFactory build(Reader reader, String environment) {
  35.     return build(reader, environment, null);
  36.   }

  37.   public SqlSessionFactory build(Reader reader, Properties properties) {
  38.     return build(reader, null, properties);
  39.   }

  40.   public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
  41.     try {
  42.       XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
  43.       return build(parser.parse());
  44.     } catch (Exception e) {
  45.       throw ExceptionFactory.wrapException("Error building SqlSession.", e);
  46.     } finally {
  47.       ErrorContext.instance().reset();
  48.       try {
  49.         if (reader != null) {
  50.           reader.close();
  51.         }
  52.       } catch (IOException e) {
  53.         // Intentionally ignore. Prefer previous error.
  54.       }
  55.     }
  56.   }

  57.   public SqlSessionFactory build(InputStream inputStream) {
  58.     return build(inputStream, null, null);
  59.   }

  60.   public SqlSessionFactory build(InputStream inputStream, String environment) {
  61.     return build(inputStream, environment, null);
  62.   }

  63.   public SqlSessionFactory build(InputStream inputStream, Properties properties) {
  64.     return build(inputStream, null, properties);
  65.   }

  66.   public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
  67.     try {
  68.       XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
  69.       return build(parser.parse());
  70.     } catch (Exception e) {
  71.       throw ExceptionFactory.wrapException("Error building SqlSession.", e);
  72.     } finally {
  73.       ErrorContext.instance().reset();
  74.       try {
  75.         if (inputStream != null) {
  76.           inputStream.close();
  77.         }
  78.       } catch (IOException e) {
  79.         // Intentionally ignore. Prefer previous error.
  80.       }
  81.     }
  82.   }

  83.   public SqlSessionFactory build(Configuration config) {
  84.     return new DefaultSqlSessionFactory(config);
  85.   }

  86. }