Search in sources :

Example 1 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class DefaultSqlSessionFactory method openSessionFromDataSource.

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
        final Environment environment = configuration.getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
        final Executor executor = configuration.newExecutor(tx, execType);
        return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
        // may have fetched a connection so lets call close()
        closeTransaction(tx);
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) Transaction(org.apache.ibatis.transaction.Transaction) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) ManagedTransactionFactory(org.apache.ibatis.transaction.managed.ManagedTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SQLException(java.sql.SQLException)

Example 2 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class DefaultSqlSessionFactory method openSessionFromConnection.

private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
    try {
        boolean autoCommit;
        try {
            autoCommit = connection.getAutoCommit();
        } catch (SQLException e) {
            // Failover to true, as most poor drivers
            // or databases won't support transactions
            autoCommit = true;
        }
        final Environment environment = configuration.getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        final Transaction tx = transactionFactory.newTransaction(connection);
        final Executor executor = configuration.newExecutor(tx, execType);
        return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
        throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
        ErrorContext.instance().reset();
    }
}
Also used : Executor(org.apache.ibatis.executor.Executor) Transaction(org.apache.ibatis.transaction.Transaction) SQLException(java.sql.SQLException) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) ManagedTransactionFactory(org.apache.ibatis.transaction.managed.ManagedTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SQLException(java.sql.SQLException)

Example 3 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class FlushTest method setup.

@BeforeClass
public static void setup() throws Exception {
    DataSource dataSource = BaseDataTest.createBlogDataSource();
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("Production", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.setDefaultExecutorType(ExecutorType.BATCH);
    configuration.getTypeAliasRegistry().registerAlias(Post.class);
    configuration.getTypeAliasRegistry().registerAlias(Author.class);
    configuration.addMapper(BoundAuthorMapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
Also used : TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) DataSource(javax.sql.DataSource) BeforeClass(org.junit.BeforeClass)

Example 4 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class XmlConfigBuilderTest method shouldSuccessfullyLoadXMLConfigFile.

@Test
public void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
    String resource = "org/apache/ibatis/builder/CustomizedSettingsMapperConfig.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    Properties props = new Properties();
    props.put("prop2", "cccc");
    XMLConfigBuilder builder = new XMLConfigBuilder(inputStream, null, props);
    Configuration config = builder.parse();
    assertThat(config.getAutoMappingBehavior(), is(AutoMappingBehavior.NONE));
    assertThat(config.getAutoMappingUnknownColumnBehavior(), is(AutoMappingUnknownColumnBehavior.WARNING));
    assertThat(config.isCacheEnabled(), is(false));
    assertThat(config.getProxyFactory(), is(instanceOf(CglibProxyFactory.class)));
    assertThat(config.isLazyLoadingEnabled(), is(true));
    assertThat(config.isAggressiveLazyLoading(), is(true));
    assertThat(config.isMultipleResultSetsEnabled(), is(false));
    assertThat(config.isUseColumnLabel(), is(false));
    assertThat(config.isUseGeneratedKeys(), is(true));
    assertThat(config.getDefaultExecutorType(), is(ExecutorType.BATCH));
    assertThat(config.getDefaultStatementTimeout(), is(10));
    assertThat(config.getDefaultFetchSize(), is(100));
    assertThat(config.isMapUnderscoreToCamelCase(), is(true));
    assertThat(config.isSafeRowBoundsEnabled(), is(true));
    assertThat(config.getLocalCacheScope(), is(LocalCacheScope.STATEMENT));
    assertThat(config.getJdbcTypeForNull(), is(JdbcType.NULL));
    assertThat(config.getLazyLoadTriggerMethods(), is((Set<String>) new HashSet<String>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx"))));
    assertThat(config.isSafeResultHandlerEnabled(), is(false));
    assertThat(config.getDefaultScriptingLanguageInstance(), is(instanceOf(RawLanguageDriver.class)));
    assertThat(config.isCallSettersOnNulls(), is(true));
    assertThat(config.getLogPrefix(), is("mybatis_"));
    assertThat(config.getLogImpl().getName(), is(Slf4jImpl.class.getName()));
    assertThat(config.getVfsImpl().getName(), is(JBoss6VFS.class.getName()));
    assertThat(config.getConfigurationFactory().getName(), is(String.class.getName()));
    assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("blogauthor").equals(Author.class));
    assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("blog").equals(Blog.class));
    assertTrue(config.getTypeAliasRegistry().getTypeAliases().get("cart").equals(Cart.class));
    assertThat(config.getTypeHandlerRegistry().getTypeHandler(Integer.class), is(instanceOf(CustomIntegerTypeHandler.class)));
    assertThat(config.getTypeHandlerRegistry().getTypeHandler(Long.class), is(instanceOf(CustomLongTypeHandler.class)));
    assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class), is(instanceOf(CustomStringTypeHandler.class)));
    assertThat(config.getTypeHandlerRegistry().getTypeHandler(String.class, JdbcType.VARCHAR), is(instanceOf(CustomStringTypeHandler.class)));
    ExampleObjectFactory objectFactory = (ExampleObjectFactory) config.getObjectFactory();
    assertThat(objectFactory.getProperties().size(), is(1));
    assertThat(objectFactory.getProperties().getProperty("objectFactoryProperty"), is("100"));
    assertThat(config.getObjectWrapperFactory(), is(instanceOf(CustomObjectWrapperFactory.class)));
    assertThat(config.getReflectorFactory(), is(instanceOf(CustomReflectorFactory.class)));
    ExamplePlugin plugin = (ExamplePlugin) config.getInterceptors().get(0);
    assertThat(plugin.getProperties().size(), is(1));
    assertThat(plugin.getProperties().getProperty("pluginProperty"), is("100"));
    Environment environment = config.getEnvironment();
    assertThat(environment.getId(), is("development"));
    assertThat(environment.getDataSource(), is(instanceOf(UnpooledDataSource.class)));
    assertThat(environment.getTransactionFactory(), is(instanceOf(JdbcTransactionFactory.class)));
    assertThat(config.getDatabaseId(), is("derby"));
    assertThat(config.getMapperRegistry().getMappers().size(), is(4));
    assertThat(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class), is(true));
    assertThat(config.getMapperRegistry().hasMapper(CustomMapper.class), is(true));
    assertThat(config.getMapperRegistry().hasMapper(BlogMapper.class), is(true));
    assertThat(config.getMapperRegistry().hasMapper(NestedBlogMapper.class), is(true));
}
Also used : NestedBlogMapper(org.apache.ibatis.domain.blog.mappers.NestedBlogMapper) BlogMapper(org.apache.ibatis.domain.blog.mappers.BlogMapper) HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) Configuration(org.apache.ibatis.session.Configuration) InputStream(java.io.InputStream) NestedBlogMapper(org.apache.ibatis.domain.blog.mappers.NestedBlogMapper) Properties(java.util.Properties) CustomMapper(org.apache.ibatis.builder.mapper.CustomMapper) XMLConfigBuilder(org.apache.ibatis.builder.xml.XMLConfigBuilder) Author(org.apache.ibatis.domain.blog.Author) Environment(org.apache.ibatis.mapping.Environment) Blog(org.apache.ibatis.domain.blog.Blog) Cart(org.apache.ibatis.domain.jpetstore.Cart) Test(org.junit.Test)

Example 5 with Environment

use of org.apache.ibatis.mapping.Environment in project mybatis-3 by mybatis.

the class BlobInputStreamTypeHandlerTest method setupSqlSessionFactory.

@BeforeClass
public static void setupSqlSessionFactory() throws Exception {
    DataSource dataSource = BaseDataTest.createUnpooledDataSource("org/apache/ibatis/type/jdbc.properties");
    BaseDataTest.runScript(dataSource, "org/apache/ibatis/type/BlobInputStreamTypeHandlerTest.sql");
    TransactionFactory transactionFactory = new JdbcTransactionFactory();
    Environment environment = new Environment("Production", transactionFactory, dataSource);
    Configuration configuration = new Configuration(environment);
    configuration.addMapper(Mapper.class);
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
Also used : Configuration(org.apache.ibatis.session.Configuration) TransactionFactory(org.apache.ibatis.transaction.TransactionFactory) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) Environment(org.apache.ibatis.mapping.Environment) SqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder) JdbcTransactionFactory(org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory) DataSource(javax.sql.DataSource) BeforeClass(org.junit.BeforeClass)

Aggregations

Environment (org.apache.ibatis.mapping.Environment)26 Configuration (org.apache.ibatis.session.Configuration)19 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)18 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)14 Connection (java.sql.Connection)10 UnpooledDataSource (org.apache.ibatis.datasource.unpooled.UnpooledDataSource)10 TransactionFactory (org.apache.ibatis.transaction.TransactionFactory)10 DataSource (javax.sql.DataSource)8 BeforeClass (org.junit.BeforeClass)6 Properties (java.util.Properties)5 SQLException (java.sql.SQLException)3 DefaultSqlSessionFactory (org.apache.ibatis.session.defaults.DefaultSqlSessionFactory)3 Transaction (org.apache.ibatis.transaction.Transaction)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 UnpooledDataSourceFactory (org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory)2 Executor (org.apache.ibatis.executor.Executor)2 ManagedTransactionFactory (org.apache.ibatis.transaction.managed.ManagedTransactionFactory)2 InputStreamReader (java.io.InputStreamReader)1