View Javadoc
1   /**
2    *    Copyright 2009-2019 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    *       http://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  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.Reader;
21  import java.util.Properties;
22  
23  import org.apache.ibatis.builder.xml.XMLConfigBuilder;
24  import org.apache.ibatis.exceptions.ExceptionFactory;
25  import org.apache.ibatis.executor.ErrorContext;
26  import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
27  
28  /**
29   * Builds {@link SqlSession} instances.
30   *
31   * @author Clinton Begin
32   */
33  public class SqlSessionFactoryBuilder {
34  
35    public SqlSessionFactory build(Reader reader) {
36      return build(reader, null, null);
37    }
38  
39    public SqlSessionFactory build(Reader reader, String environment) {
40      return build(reader, environment, null);
41    }
42  
43    public SqlSessionFactory build(Reader reader, Properties properties) {
44      return build(reader, null, properties);
45    }
46  
47    public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
48      try {
49        XMLConfigBuildergBuilder.html#XMLConfigBuilder">XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
50        return build(parser.parse());
51      } catch (Exception e) {
52        throw ExceptionFactory.wrapException("Error building SqlSession.", e);
53      } finally {
54        ErrorContext.instance().reset();
55        try {
56          reader.close();
57        } catch (IOException e) {
58          // Intentionally ignore. Prefer previous error.
59        }
60      }
61    }
62  
63    public SqlSessionFactory build(InputStream inputStream) {
64      return build(inputStream, null, null);
65    }
66  
67    public SqlSessionFactory build(InputStream inputStream, String environment) {
68      return build(inputStream, environment, null);
69    }
70  
71    public SqlSessionFactory build(InputStream inputStream, Properties properties) {
72      return build(inputStream, null, properties);
73    }
74  
75    public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
76      try {
77        XMLConfigBuildergBuilder.html#XMLConfigBuilder">XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
78        return build(parser.parse());
79      } catch (Exception e) {
80        throw ExceptionFactory.wrapException("Error building SqlSession.", e);
81      } finally {
82        ErrorContext.instance().reset();
83        try {
84          inputStream.close();
85        } catch (IOException e) {
86          // Intentionally ignore. Prefer previous error.
87        }
88      }
89    }
90  
91    public SqlSessionFactory build(Configuration config) {
92      return new DefaultSqlSessionFactory(config);
93    }
94  
95  }