Search in sources :

Example 1 with DefaultSqlSessionFactory

use of org.apache.ibatis.session.defaults.DefaultSqlSessionFactory in project camunda-bpm-platform by camunda.

the class ProcessEngineConfigurationImpl method initSqlSessionFactory.

protected void initSqlSessionFactory() {
    // to protect access to cachedSqlSessionFactory see CAM-6682
    synchronized (ProcessEngineConfigurationImpl.class) {
        if (isUseSharedSqlSessionFactory) {
            sqlSessionFactory = cachedSqlSessionFactory;
        }
        if (sqlSessionFactory == null) {
            InputStream inputStream = null;
            try {
                inputStream = getMyBatisXmlConfigurationSteam();
                // update the jdbc parameters to the configured ones...
                Environment environment = new Environment("default", transactionFactory, dataSource);
                Reader reader = new InputStreamReader(inputStream);
                Properties properties = new Properties();
                if (isUseSharedSqlSessionFactory) {
                    properties.put("prefix", "${@org.camunda.bpm.engine.impl.context.Context@getProcessEngineConfiguration().databaseTablePrefix}");
                } else {
                    properties.put("prefix", databaseTablePrefix);
                }
                initSqlSessionFactoryProperties(properties, databaseTablePrefix, databaseType);
                XMLConfigBuilder parser = new XMLConfigBuilder(reader, "", properties);
                Configuration configuration = parser.getConfiguration();
                configuration.setEnvironment(environment);
                configuration = parser.parse();
                configuration.setDefaultStatementTimeout(jdbcStatementTimeout);
                if (isJdbcBatchProcessing()) {
                    configuration.setDefaultExecutorType(ExecutorType.BATCH);
                }
                sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
                if (isUseSharedSqlSessionFactory) {
                    cachedSqlSessionFactory = sqlSessionFactory;
                }
            } catch (Exception e) {
                throw new ProcessEngineException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
            } finally {
                IoUtil.closeSilently(inputStream);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Configuration(org.apache.ibatis.session.Configuration) ProcessEngineConfiguration(org.camunda.bpm.engine.ProcessEngineConfiguration) DmnEngineConfiguration(org.camunda.bpm.dmn.engine.DmnEngineConfiguration) DefaultDmnEngineConfiguration(org.camunda.bpm.dmn.engine.impl.DefaultDmnEngineConfiguration) InputStream(java.io.InputStream) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) Environment(org.apache.ibatis.mapping.Environment) ScriptingEnvironment(org.camunda.bpm.engine.impl.scripting.env.ScriptingEnvironment) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) Properties(java.util.Properties) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 2 with DefaultSqlSessionFactory

use of org.apache.ibatis.session.defaults.DefaultSqlSessionFactory in project Activiti by Activiti.

the class ProcessEngineConfigurationImpl method initSqlSessionFactory.

public void initSqlSessionFactory() {
    if (sqlSessionFactory == null) {
        InputStream inputStream = null;
        try {
            inputStream = getMyBatisXmlConfigurationStream();
            Environment environment = new Environment("default", transactionFactory, dataSource);
            Reader reader = new InputStreamReader(inputStream);
            Properties properties = new Properties();
            properties.put("prefix", databaseTablePrefix);
            String wildcardEscapeClause = "";
            if ((databaseWildcardEscapeCharacter != null) && (databaseWildcardEscapeCharacter.length() != 0)) {
                wildcardEscapeClause = " escape '" + databaseWildcardEscapeCharacter + "'";
            }
            properties.put("wildcardEscapeClause", wildcardEscapeClause);
            // set default properties
            properties.put("limitBefore", "");
            properties.put("limitAfter", "");
            properties.put("limitBetween", "");
            properties.put("limitOuterJoinBetween", "");
            properties.put("limitBeforeNativeQuery", "");
            properties.put("orderBy", "order by ${orderByColumns}");
            properties.put("blobType", "BLOB");
            properties.put("boolValue", "TRUE");
            if (databaseType != null) {
                properties.load(getResourceAsStream("org/activiti/db/properties/" + databaseType + ".properties"));
            }
            Configuration configuration = initMybatisConfiguration(environment, reader, properties);
            sqlSessionFactory = new DefaultSqlSessionFactory(configuration);
        } catch (Exception e) {
            throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
        } finally {
            IoUtil.closeSilently(inputStream);
        }
    }
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) InputStreamReader(java.io.InputStreamReader) ProcessEngineConfiguration(org.activiti.engine.ProcessEngineConfiguration) Configuration(org.apache.ibatis.session.Configuration) InputStream(java.io.InputStream) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) Environment(org.apache.ibatis.mapping.Environment) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) Properties(java.util.Properties) ActivitiException(org.activiti.engine.ActivitiException) SQLException(java.sql.SQLException)

Example 3 with DefaultSqlSessionFactory

use of org.apache.ibatis.session.defaults.DefaultSqlSessionFactory in project mybatis-3 by mybatis.

the class SqlSessionTest method shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers.

@Test
void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
    Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
    configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
    SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
    try (SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession()) {
        DefaultResultHandler handler1 = new DefaultResultHandler();
        DefaultResultHandler handler2 = new DefaultResultHandler();
        AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
        Assertions.assertThrows(BindingException.class, () -> mapper.selectAuthor(101, handler1, handler2));
    }
}
Also used : DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) AuthorMapperWithMultipleHandlers(org.apache.ibatis.domain.blog.mappers.AuthorMapperWithMultipleHandlers) DefaultResultHandler(org.apache.ibatis.executor.result.DefaultResultHandler) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultSqlSessionFactory

use of org.apache.ibatis.session.defaults.DefaultSqlSessionFactory in project mybatis-3 by mybatis.

the class SqlSessionTest method shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds.

@Test
void shouldFailSelectOneAuthorUsingMapperClassWithTwoRowBounds() {
    Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
    configuration.addMapper(AuthorMapperWithRowBounds.class);
    SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
    try (SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession()) {
        RowBounds bounds1 = new RowBounds(0, 1);
        RowBounds bounds2 = new RowBounds(0, 1);
        AuthorMapperWithRowBounds mapper = sqlSession.getMapper(AuthorMapperWithRowBounds.class);
        Assertions.assertThrows(BindingException.class, () -> mapper.selectAuthor(101, bounds1, bounds2));
    }
}
Also used : AuthorMapperWithRowBounds(org.apache.ibatis.domain.blog.mappers.AuthorMapperWithRowBounds) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) AuthorMapperWithRowBounds(org.apache.ibatis.domain.blog.mappers.AuthorMapperWithRowBounds) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultSqlSessionFactory

use of org.apache.ibatis.session.defaults.DefaultSqlSessionFactory in project mybatis-3 by mybatis.

the class ProviderTest method shouldUseProvider.

@Test
void shouldUseProvider() throws Exception {
    Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/multidb/ProviderConfig.xml");
    DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) new SqlSessionFactoryBuilder().build(reader);
    Configuration c = sqlSessionFactory.getConfiguration();
    assertEquals("translated", c.getDatabaseId());
}
Also used : Configuration(org.apache.ibatis.session.Configuration) DefaultSqlSessionFactory(org.apache.ibatis.session.defaults.DefaultSqlSessionFactory) Reader(java.io.Reader) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)8 Configuration (org.apache.ibatis.session.Configuration)6 Test (org.junit.jupiter.api.Test)5 Reader (java.io.Reader)4 Properties (java.util.Properties)4 Environment (org.apache.ibatis.mapping.Environment)4 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 SQLException (java.sql.SQLException)2 BaseDataTest (org.apache.ibatis.BaseDataTest)2 UnpooledDataSourceFactory (org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory)2 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)2 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)2 ParseException (java.text.ParseException)1 ActivitiException (org.activiti.engine.ActivitiException)1 ProcessEngineConfiguration (org.activiti.engine.ProcessEngineConfiguration)1 XMLConfigBuilder (org.apache.ibatis.builder.xml.XMLConfigBuilder)1 AuthorMapperWithMultipleHandlers (org.apache.ibatis.domain.blog.mappers.AuthorMapperWithMultipleHandlers)1 AuthorMapperWithRowBounds (org.apache.ibatis.domain.blog.mappers.AuthorMapperWithRowBounds)1 DefaultResultHandler (org.apache.ibatis.executor.result.DefaultResultHandler)1