Search in sources :

Example 1 with SpringManagedTransactionFactory

use of org.mybatis.spring.transaction.SpringManagedTransactionFactory in project gocd by gocd.

the class SqlSessionFactoryBean method buildSqlSessionFactory.

private SqlSessionFactory buildSqlSessionFactory() throws IOException {
    SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
    XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(configLocation.getInputStream());
    Configuration configuration = xmlConfigBuilder.getConfiguration();
    configuration.setEnvironment(new Environment(getClass().getSimpleName(), new SpringManagedTransactionFactory(), this.dataSource));
    xmlConfigBuilder.parse();
    return factoryBuilder.build(configuration);
}
Also used : Configuration(org.apache.ibatis.session.Configuration) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) SpringManagedTransactionFactory(org.mybatis.spring.transaction.SpringManagedTransactionFactory)

Example 2 with SpringManagedTransactionFactory

use of org.mybatis.spring.transaction.SpringManagedTransactionFactory in project alfresco-repository by Alfresco.

the class HierarchicalSqlSessionFactoryBean method buildSqlSessionFactory.

/**
 * Build a {@code SqlSessionFactory} instance.
 * <p/>
 * The default implementation uses the standard MyBatis {@code XMLConfigBuilder} API to build a
 * {@code SqlSessionFactory} instance based on an Reader.
 *
 * @return SqlSessionFactory
 * @throws IOException if loading the config file failed
 */
protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
    Configuration configuration;
    HierarchicalXMLConfigBuilder xmlConfigBuilder = null;
    if (this.configLocation != null) {
        try {
            xmlConfigBuilder = new HierarchicalXMLConfigBuilder(resourceLoader, this.configLocation.getInputStream(), null, this.configurationProperties);
            configuration = xmlConfigBuilder.getConfiguration();
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration");
        }
        configuration = new Configuration();
        configuration.setVariables(this.configurationProperties);
    }
    if (this.objectFactory != null) {
        configuration.setObjectFactory(this.objectFactory);
    }
    if (this.objectWrapperFactory != null) {
        configuration.setObjectWrapperFactory(this.objectWrapperFactory);
    }
    if (hasLength(this.typeAliasesPackage)) {
        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeAliasPackageArray) {
            configuration.getTypeAliasRegistry().registerAliases(packageToScan, typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
            }
        }
    }
    if (!isEmpty(this.typeAliases)) {
        for (Class<?> typeAlias : this.typeAliases) {
            configuration.getTypeAliasRegistry().registerAlias(typeAlias);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered type alias: '" + typeAlias + "'");
            }
        }
    }
    if (!isEmpty(this.plugins)) {
        for (Interceptor plugin : this.plugins) {
            configuration.addInterceptor(plugin);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered plugin: '" + plugin + "'");
            }
        }
    }
    if (hasLength(this.typeHandlersPackage)) {
        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
        for (String packageToScan : typeHandlersPackageArray) {
            configuration.getTypeHandlerRegistry().register(packageToScan);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
            }
        }
    }
    if (!isEmpty(this.typeHandlers)) {
        for (TypeHandler<?> typeHandler : this.typeHandlers) {
            configuration.getTypeHandlerRegistry().register(typeHandler);
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Registered type handler: '" + typeHandler + "'");
            }
        }
    }
    if (xmlConfigBuilder != null) {
        try {
            xmlConfigBuilder.parse();
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed configuration file: '" + this.configLocation + "'");
            }
        } catch (Exception ex) {
            throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex);
        } finally {
            ErrorContext.instance().reset();
        }
    }
    if (this.transactionFactory == null) {
        this.transactionFactory = new SpringManagedTransactionFactory();
    }
    Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource);
    configuration.setEnvironment(environment);
    if (!isEmpty(this.mapperLocations)) {
        for (Resource mapperLocation : this.mapperLocations) {
            if (mapperLocation == null) {
                continue;
            }
            try {
                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments());
                xmlMapperBuilder.parse();
            } catch (Exception e) {
                throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e);
            } finally {
                ErrorContext.instance().reset();
            }
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
            }
        }
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Property 'mapperLocations' was not specified, only MyBatis mapper files specified in the config xml were loaded");
        }
    }
    return this.sqlSessionFactoryBuilder.build(configuration);
}
Also used : Configuration(org.apache.ibatis.session.Configuration) NestedIOException(org.springframework.core.NestedIOException) XMLMapperBuilder(org.apache.ibatis.builder.xml.XMLMapperBuilder) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) NestedIOException(org.springframework.core.NestedIOException) Environment(org.apache.ibatis.mapping.Environment) Interceptor(org.apache.ibatis.plugin.Interceptor) SpringManagedTransactionFactory(org.mybatis.spring.transaction.SpringManagedTransactionFactory)

Example 3 with SpringManagedTransactionFactory

use of org.mybatis.spring.transaction.SpringManagedTransactionFactory in project taskana by Taskana.

the class SpringTaskanaEngineImpl method init.

@PostConstruct
public void init() throws SQLException {
    this.transactionFactory = new SpringManagedTransactionFactory();
    this.sessionManager = createSqlSessionManager();
}
Also used : SpringManagedTransactionFactory(org.mybatis.spring.transaction.SpringManagedTransactionFactory) PostConstruct(javax.annotation.PostConstruct)

Aggregations

SpringManagedTransactionFactory (org.mybatis.spring.transaction.SpringManagedTransactionFactory)3 Environment (org.apache.ibatis.mapping.Environment)2 Configuration (org.apache.ibatis.session.Configuration)2 IOException (java.io.IOException)1 PostConstruct (javax.annotation.PostConstruct)1 XMLConfigBuilder (org.apache.ibatis.builder.xml.XMLConfigBuilder)1 XMLMapperBuilder (org.apache.ibatis.builder.xml.XMLMapperBuilder)1 Interceptor (org.apache.ibatis.plugin.Interceptor)1 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)1 NestedIOException (org.springframework.core.NestedIOException)1 Resource (org.springframework.core.io.Resource)1