Example usage for org.apache.ibatis.session ExecutorType REUSE

List of usage examples for org.apache.ibatis.session ExecutorType REUSE

Introduction

In this page you can find the example usage for org.apache.ibatis.session ExecutorType REUSE.

Prototype

ExecutorType REUSE

To view the source code for org.apache.ibatis.session ExecutorType REUSE.

Click Source Link

Usage

From source file:com.dmm.framework.basedb.apache.ibatis.session.Configuration.java

License:Apache License

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;/* w w  w.  j a va 2s .c om*/
    if (ExecutorType.BATCH == executorType) {
        executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
        executor = new ReuseExecutor(this, transaction);
    } else {
        executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
        executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
}

From source file:com.dvdprime.server.mobile.config.MyBatisConfig.java

License:Apache License

/**
 * MyBatis ?  Configuration? .//from   ww  w.j  av  a2  s .co m
 * 
 * @return
 */
public Configuration getConfig() {
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("master", transactionFactory, getTomcatDataSource());

    logger.info("MyBatis Configuration Initialization.");
    Configuration configuration = new Configuration(environment);
    configuration.setCacheEnabled(true);
    configuration.setLazyLoadingEnabled(false);
    configuration.setAggressiveLazyLoading(false);
    configuration.setUseColumnLabel(true);
    configuration.setUseGeneratedKeys(false);
    configuration.setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
    configuration.setDefaultExecutorType(ExecutorType.REUSE);
    configuration.setDefaultStatementTimeout(25000);
    configuration.setSafeRowBoundsEnabled(true);

    // Alias Type
    Iterator<String> it = TypeAliasProp.getProperties().keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        logger.info("typeAliasRegistry: [{}] -> [{}]", key, TypeAliasProp.getProperties().get(key));
        configuration.getTypeAliasRegistry().registerAlias(key,
                (String) TypeAliasProp.getProperties().get(key));
    }

    // Mapper
    it = MapperProp.getProperties().keySet().iterator();
    while (it.hasNext()) {
        String key = it.next();
        logger.info("mapper loaded: [{}]", MapperProp.getProperties().get(key));
        try {
            InputStream inputStream = Resources
                    .getResourceAsStream((String) MapperProp.getProperties().get(key));
            XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration,
                    (String) MapperProp.getProperties().get(key), configuration.getSqlFragments());
            mapperParser.parse();
        } catch (IOException e) {
            logger.error("mapper parsing   ?.");
        }
    }

    return configuration;
}

From source file:com.ibatis.sqlmap.engine.builder.XmlSqlMapConfigParser.java

License:Apache License

@NodeEvent("/sqlMapConfig/settings")
public void sqlMapConfigsettings(XNode context) throws Exception {
    boolean classInfoCacheEnabled = context.getBooleanAttribute("classInfoCacheEnabled", true);
    MetaClass.setClassCacheEnabled(classInfoCacheEnabled);

    boolean lazyLoadingEnabled = context.getBooleanAttribute("lazyLoadingEnabled", true);
    config.setLazyLoadingEnabled(lazyLoadingEnabled);

    boolean statementCachingEnabled = context.getBooleanAttribute("statementCachingEnabled", true);
    if (statementCachingEnabled) {
        config.setDefaultExecutorType(ExecutorType.REUSE);
    }/* w  ww . j ava 2  s  .  c o m*/

    boolean batchUpdatesEnabled = context.getBooleanAttribute("batchUpdatesEnabled", true);
    if (batchUpdatesEnabled) {
        config.setDefaultExecutorType(ExecutorType.BATCH);
    }

    boolean cacheModelsEnabled = context.getBooleanAttribute("cacheModelsEnabled", true);
    config.setCacheEnabled(cacheModelsEnabled);

    boolean useColumnLabel = context.getBooleanAttribute("useColumnLabel", false);
    config.setUseColumnLabel(useColumnLabel);

    boolean forceMultipleResultSetSupport = context.getBooleanAttribute("forceMultipleResultSetSupport", true);
    config.setMultipleResultSetsEnabled(forceMultipleResultSetSupport);

    useStatementNamespaces = context.getBooleanAttribute("useStatementNamespaces", false);

    Integer defaultTimeout = context.getIntAttribute("defaultStatementTimeout");
    config.setDefaultStatementTimeout(defaultTimeout);
}

From source file:org.apache.aurora.scheduler.storage.db.DbStorage.java

License:Apache License

/**
 * Creates the SQL schema during service start-up.
 * Note: This design assumes a volatile database engine.
 *///  ww w.j a v  a 2  s.c o m
@Override
@Transactional
protected void startUp() throws IOException {
    Configuration configuration = sessionFactory.getConfiguration();
    String createStatementName = "create_tables";
    configuration.setMapUnderscoreToCamelCase(true);

    // The ReuseExecutor will cache jdbc Statements with equivalent SQL, improving performance
    // slightly when redundant queries are made.
    configuration.setDefaultExecutorType(ExecutorType.REUSE);

    addMappedStatement(configuration, createStatementName, CharStreams.toString(
            new InputStreamReader(DbStorage.class.getResourceAsStream("schema.sql"), StandardCharsets.UTF_8)));

    try (SqlSession session = sessionFactory.openSession()) {
        session.update(createStatementName);
    }

    enumBackfill.backfill();

    createPoolMetrics();
}

From source file:org.jessma.dao.mybatis.MyBatisFacade.java

License:Apache License

/** ? {@link SqlSession}  {@link ExecutorType}  {@link ExecutorType#REUSE} */
protected void changeSessionExecutorTypeToReuse() {
    getManager().changeSessionExecutorType(ExecutorType.REUSE);
}

From source file:org.mybatis.guice.configuration.ConfigurationProviderTest.java

License:Apache License

@Test
public void get_Optionals() throws Throwable {
    String databaseId = "test_database_id";
    final Integer defaultFetchSize = 200;
    final Integer defaultStatementTimeout = 2000;
    when(databaseIdProvider.getDatabaseId(dataSource)).thenReturn(databaseId);
    final Key<TypeHandler<Alias>> aliasTypeHandlerKey = Key.get(new TypeLiteral<TypeHandler<Alias>>() {
    });/*from   ww w  . j  a v  a2s .c  o  m*/
    injector = Guice.createInjector(new AbstractModule() {
        @Override
        protected void configure() {
            bind(Environment.class).toInstance(environment);
            bind(DataSource.class).toInstance(dataSource);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.lazyLoadingEnabled")).to(true);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.aggressiveLazyLoading")).to(false);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.multipleResultSetsEnabled"))
                    .to(false);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.useGeneratedKeys")).to(true);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.useColumnLabel")).to(false);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.cacheEnabled")).to(false);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.defaultExecutorType"))
                    .to(ExecutorType.REUSE);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.autoMappingBehavior"))
                    .to(AutoMappingBehavior.FULL);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.callSettersOnNulls")).to(true);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.defaultStatementTimeout"))
                    .to(defaultStatementTimeout);
            bindConstant().annotatedWith(Names.named("mybatis.configuration.mapUnderscoreToCamelCase"))
                    .to(true);
            bind(DatabaseIdProvider.class).toInstance(databaseIdProvider);
            bind(Configuration.class).toProvider(configurationProvider);
            bind(aliasTypeHandlerKey).toInstance(aliasTypeHandler);
            bind(Interceptor.class).toInstance(interceptor);
        }
    });
    configurationProvider.addConfigurationSetting(new AliasConfigurationSetting("alias", Alias.class));
    JavaTypeAndHandlerConfigurationSettingProvider aliasTypeHandlerSetting = JavaTypeAndHandlerConfigurationSettingProvider
            .create(Alias.class, aliasTypeHandlerKey);
    injector.injectMembers(aliasTypeHandlerSetting);
    configurationProvider.addConfigurationSetting(aliasTypeHandlerSetting.get());
    TypeHandlerConfigurationSettingProvider humanTypeHandlerSetting = new TypeHandlerConfigurationSettingProvider(
            Key.get(HumanTypeHandler.class));
    injector.injectMembers(humanTypeHandlerSetting);
    configurationProvider.addConfigurationSetting(humanTypeHandlerSetting.get());
    configurationProvider.addMapperConfigurationSetting(new MapperConfigurationSetting(TestMapper.class));
    InterceptorConfigurationSettingProvider interceptorSetting = new InterceptorConfigurationSettingProvider(
            Interceptor.class);
    injector.injectMembers(interceptorSetting);
    configurationProvider.addConfigurationSetting(interceptorSetting.get());
    configurationProvider.addConfigurationSetting((new ConfigurationSetting() {
        @Override
        public void applyConfigurationSetting(Configuration configuration) {
            configuration.setDefaultFetchSize(defaultFetchSize);
        }
    }));

    Configuration configuration = injector.getInstance(Configuration.class);

    configuration.getMappedStatementNames(); // Test that configuration is valid.
    assertEquals(environment, configuration.getEnvironment());
    assertEquals(Alias.class, configuration.getTypeAliasRegistry().getTypeAliases().get("alias"));
    assertTrue(configuration.getTypeHandlerRegistry().hasTypeHandler(Alias.class));
    assertTrue(configuration.getTypeHandlerRegistry().hasTypeHandler(Human.class));
    assertEquals(1, configuration.getMapperRegistry().getMappers().size());
    assertTrue(configuration.getMapperRegistry().getMappers().contains(TestMapper.class));
    assertEquals(1, configuration.getInterceptors().size());
    assertTrue(configuration.getInterceptors().contains(interceptor));
    verify(databaseIdProvider).getDatabaseId(dataSource);
    assertEquals(databaseId, configuration.getDatabaseId());
    assertTrue(configuration.isLazyLoadingEnabled());
    assertFalse(configuration.isAggressiveLazyLoading());
    assertFalse(configuration.isMultipleResultSetsEnabled());
    assertTrue(configuration.isUseGeneratedKeys());
    assertFalse(configuration.isUseColumnLabel());
    assertFalse(configuration.isCacheEnabled());
    assertEquals(ExecutorType.REUSE, configuration.getDefaultExecutorType());
    assertEquals(AutoMappingBehavior.FULL, configuration.getAutoMappingBehavior());
    assertTrue(configuration.isCallSettersOnNulls());
    assertEquals(defaultStatementTimeout, configuration.getDefaultStatementTimeout());
    assertTrue(configuration.isMapUnderscoreToCamelCase());
    assertEquals(defaultFetchSize, configuration.getDefaultFetchSize());
}

From source file:org.mybatis.guice.configuration.settings.DefaultExecutorTypeConfigurationSettingTest.java

License:Apache License

@Test
public void applyConfigurationSetting_Reuse() {
    DefaultExecutorTypeConfigurationSetting setting = new DefaultExecutorTypeConfigurationSetting(
            ExecutorType.REUSE);
    setting.applyConfigurationSetting(configuration);
    verify(configuration).setDefaultExecutorType(ExecutorType.REUSE);
}

From source file:org.mybatis.spring.boot.autoconfigure.MybatisAutoConfigurationTest.java

License:Apache License

@Test
public void testWithExecutorType() {
    EnvironmentTestUtils.addEnvironment(this.context, "mybatis.config-location:mybatis-config.xml",
            "mybatis.executor-type:REUSE");
    this.context.register(EmbeddedDataSourceConfiguration.class, MybatisAutoConfiguration.class,
            MybatisMapperConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertEquals(ExecutorType.REUSE, this.context.getBean(SqlSessionTemplate.class).getExecutorType());
}

From source file:org.mybatis.spring.boot.autoconfigure.MybatisAutoConfigurationTest.java

License:Apache License

@Test
public void testMixedWithFullConfigurations() {
    EnvironmentTestUtils.addEnvironment(this.context,
            "mybatis.config-location:mybatis-config-settings-only.xml",
            "mybatis.type-handlers-package:org.mybatis.spring.boot.autoconfigure.handler",
            "mybatis.type-aliases-package:org.mybatis.spring.boot.autoconfigure.domain",
            "mybatis.mapper-locations:classpath:org/mybatis/spring/boot/autoconfigure/repository/CityMapper.xml",
            "mybatis.executor-type=REUSE");
    this.context.register(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class,
            MybatisInterceptorConfiguration.class, DatabaseProvidersConfiguration.class);
    this.context.refresh();

    org.apache.ibatis.session.Configuration configuration = this.context.getBean(SqlSessionFactory.class)
            .getConfiguration();//from ww w  .j  a va2s. co  m
    assertEquals(1, this.context.getBeanNamesForType(SqlSessionFactory.class).length);
    assertEquals(1, this.context.getBeanNamesForType(SqlSessionTemplate.class).length);
    assertEquals(1, this.context.getBeanNamesForType(CityMapper.class).length);
    assertEquals(Integer.valueOf(1000), configuration.getDefaultFetchSize());
    assertEquals(DummyTypeHandler.class,
            configuration.getTypeHandlerRegistry().getTypeHandler(BigInteger.class).getClass());
    assertEquals(4, configuration.getMappedStatementNames().size());
    assertTrue(configuration.getMappedStatementNames().contains("selectCityById"));
    assertTrue(configuration.getMappedStatementNames()
            .contains("org.mybatis.spring.boot.autoconfigure.repository.CityMapperImpl.selectCityById"));
    assertTrue(configuration.getMappedStatementNames().contains("findById"));
    assertTrue(configuration.getMappedStatementNames()
            .contains("org.mybatis.spring.boot.autoconfigure.mapper.CityMapper.findById"));
    assertEquals(ExecutorType.REUSE, this.context.getBean(SqlSessionTemplate.class).getExecutorType());
    assertEquals(1, configuration.getInterceptors().size());
    assertEquals(MyInterceptor.class, configuration.getInterceptors().get(0).getClass());
    assertEquals("h2", configuration.getDatabaseId());
}

From source file:org.mybatis.spring.mapper.MapperScannerConfigurerTest.java

License:Apache License

@Test
public void testScanWithPropertyPlaceholders() {
    GenericBeanDefinition definition = (GenericBeanDefinition) applicationContext
            .getBeanDefinition("mapperScanner");

    // use a property placeholder for basePackage
    definition.getPropertyValues().removePropertyValue("basePackage");
    definition.getPropertyValues().add("basePackage", "${basePackageProperty}");
    definition.getPropertyValues().add("processPropertyPlaceHolders", true);

    // also use a property placeholder for an SqlSessionFactory property
    // to make sure the configLocation was setup correctly and MapperScanner did not change
    // regular property placeholder substitution
    definition = (GenericBeanDefinition) applicationContext.getBeanDefinition("sqlSessionFactory");
    definition.getPropertyValues().removePropertyValue("configLocation");
    definition.getPropertyValues().add("configLocation", "${configLocationProperty}");

    Properties props = new java.util.Properties();
    props.put("basePackageProperty", "org.mybatis.spring.mapper");
    props.put("configLocationProperty", "classpath:org/mybatis/spring/mybatis-config.xml");

    GenericBeanDefinition propertyDefinition = new GenericBeanDefinition();
    propertyDefinition.setBeanClass(PropertyPlaceholderConfigurer.class);
    propertyDefinition.getPropertyValues().add("properties", props);

    applicationContext.registerBeanDefinition("propertiesPlaceholder", propertyDefinition);

    testInterfaceScan();/*from   w w w . jav  a  2 s. c o m*/

    // make sure the configLocation was setup correctly
    // mybatis-config.xml changes the executor from the default SIMPLE type
    SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
    assertSame(ExecutorType.REUSE, sessionFactory.getConfiguration().getDefaultExecutorType());
}