views:

168

answers:

1

I have this configuration in ibatis-config.xml

<configuration>
    <properties resource="collector.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${dev.jdbc.driver}" />
                <property name="url" value="${dev.jdbc.url}" />
            </dataSource>
        </environment>
        <environment id="test">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${test.jdbc.driver}" />
                <property name="url" value="${test.jdbc.url}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
    </mappers>
</configuration>

As shown it will load datasource from <environment id="development">

QUESTION: Is it possible at run time switch to use <environment id="test"> without modifying XML? For example - I have a test file where I'm using SqlSessionFactory and want to set it programmatically to use test environment?

+1  A: 

SqlSessionFactoryBuilder.build() method can select a specific environment in XML.

For example,

private Reader reader;
private SqlSessionFactory sqlSessionFactorys;
private SqlSession session;

reader = Resources.getResourceAsReader("ibatis-config.xml");

sqlSessionFactorys = new SqlSessionFactoryBuilder().build(reader, "test");
testSession = sqlSessionFactorys.openSession(); // test env

sqlSessionFactorys = new SqlSessionFactoryBuilder().build(reader, "dev");
devSession = sqlSessionFactorys.openSession(); // dev env
Alex Park
Alex - muchas gracias! I was just about to post here your own answer from the mailing list :)
DroidIn.net