Example usage for org.apache.ibatis.session SqlSession getConnection

List of usage examples for org.apache.ibatis.session SqlSession getConnection

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession getConnection.

Prototype

Connection getConnection();

Source Link

Document

Retrieves inner database connection.

Usage

From source file:org.craftercms.studio.impl.v1.dal.DataSourceInitializerImpl.java

License:Open Source License

@Override
public void initDataSource() {
    if (isEnabled()) {
        String scriptPath = getScriptPath();
        SqlSession session = sqlSessionFactory.openSession();
        Connection conn = session.getConnection();

        ScriptRunner sr = new ScriptRunner(conn);

        sr.setDelimiter(delimiter);//  w  ww.ja v a2 s.  c om
        InputStream is = getClass().getClassLoader().getResourceAsStream(scriptPath);
        Reader reader = new InputStreamReader(is);
        sr.runScript(reader);
        try {
            conn.close();
        } catch (SQLException e) {
            logger.error("Error while closing connection with database", e);
        }
    }
}

From source file:org.fdmtech.mybatis.jdbc.TestJdbc.java

License:Apache License

private SqlSessionFactory createFactory() throws Exception {
    Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
    reader.close();//from   w  w  w  . j  a v  a 2s  .  co  m
    SqlSession session = factory.openSession();
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader("CreateDB_1.sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();
    return factory;
}

From source file:org.fdmtech.mybatis.jdbc.TestJdbc.java

License:Apache License

@Test
public void basic() throws Exception {

    SqlSessionFactory factory = createFactory();
    JdbcStatementFactory jdbc = new JdbcStatementFactory(factory);
    JdbcStatement statement = jdbc.getStatement("org.fdmtech.mybatis.jdbc.UserMapper.getUser");

    SqlSession session = factory.openSession();
    Connection connection = session.getConnection();

    ResultSet rs = statement.query(connection, 2);
    Assert.assertTrue(rs.next());/*from  www . ja  v  a  2 s  .  c  o m*/
    Assert.assertEquals("1-User2", rs.getString("name"));

    session.close();

}

From source file:org.flowable.engine.test.db.MetaDataTest.java

License:Apache License

public void testMetaData() {
    ((ProcessEngineImpl) processEngine).getProcessEngineConfiguration().getCommandExecutor()
            .execute(new Command<Object>() {
                public Object execute(CommandContext commandContext) {
                    // PRINT THE TABLE NAMES TO CHECK IF WE CAN USE METADATA INSTEAD
                    // THIS IS INTENDED FOR TEST THAT SHOULD RUN ON OUR QA
                    // INFRASTRUCTURE TO SEE IF METADATA
                    // CAN BE USED INSTEAD OF PERFORMING A QUERY THAT MIGHT FAIL
                    try {
                        SqlSession sqlSession = commandContext.getDbSqlSession().getSqlSession();
                        ResultSet tables = sqlSession.getConnection().getMetaData().getTables(null, null, null,
                                null);/*from  w  ww .jav  a2s.  c o m*/
                        while (tables.next()) {
                            ResultSetMetaData resultSetMetaData = tables.getMetaData();
                            int columnCount = resultSetMetaData.getColumnCount();
                            for (int i = 1; i <= columnCount; i++) {
                                log.info("result set column {}|{}|{}|{}", i, resultSetMetaData.getColumnName(i),
                                        resultSetMetaData.getColumnLabel(i), tables.getString(i));
                            }
                            log.info("-------------------------------------------------------");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            });
}

From source file:org.flowable.form.engine.impl.db.FormDbSchemaManager.java

License:Apache License

protected static Liquibase createLiquibaseInstance() {
    try {//  w w w  .  j av a  2s  .  c o  m
        DbSqlSession dbSqlSession = CommandContextUtil.getDbSqlSession();
        SqlSession sqlSession = dbSqlSession.getSqlSession();
        DatabaseConnection connection = new JdbcConnection(sqlSession.getConnection());
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        database.setDatabaseChangeLogTableName(
                FormEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
        database.setDatabaseChangeLogLockTableName(FormEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX
                + database.getDatabaseChangeLogLockTableName());

        if (StringUtils.isNotEmpty(sqlSession.getConnection().getSchema())) {
            database.setDefaultSchemaName(sqlSession.getConnection().getSchema());
            database.setLiquibaseSchemaName(sqlSession.getConnection().getSchema());
        }

        if (StringUtils.isNotEmpty(sqlSession.getConnection().getCatalog())) {
            database.setDefaultCatalogName(sqlSession.getConnection().getCatalog());
            database.setLiquibaseCatalogName(sqlSession.getConnection().getCatalog());
        }

        Liquibase liquibase = new Liquibase("org/flowable/form/db/liquibase/flowable-form-db-changelog.xml",
                new ClassLoaderResourceAccessor(), database);
        return liquibase;

    } catch (Exception e) {
        throw new FlowableException("Error creating liquibase instance", e);
    }
}

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

License:Apache License

/** ?{@link AbstractSessionMgr#loadDefalutTransIsoLevel()} */
@Override// w  w  w  .  j a  v a  2  s .  c  o  m
protected void loadDefalutTransIsoLevel() {
    try {
        SqlSession session = getSession();
        Connection conn = session.getConnection();
        int level = conn.getTransactionIsolation();
        defaultTransIsoLevel = TransIsoLevel.fromInt(level);
    } catch (SQLException e) {
        throw new SqlSessionException(e);
    } finally {
        closeSession();
    }
}

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

License:Apache License

/** ?{@link SessionMgr#setSessionTransIsoLevel(TransIsoLevel)} */
@Override//ww w. ja  v  a 2 s .c  o m
public void setSessionTransIsoLevel(TransIsoLevel level) {
    try {
        SqlSession session = getSession();
        Connection conn = session.getConnection();

        conn.setTransactionIsolation(level.toInt());
    } catch (SQLException e) {
        throw new SqlSessionException(e);
    }
}

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

License:Apache License

/** ? {@link SqlSession}  {@link ExecutorType}  {@literal type} */
public void changeSessionExecutorType(ExecutorType type) {
    SqlSession session = localSession.get();

    if (session == null)
        localExecutorType.set(type);/*from   w ww  .  j a v  a  2s  . c o  m*/
    else {
        if (type == null)
            type = getDefaultExecutorType();

        ExecutorType currentType = localExecutorType.get();

        if (type != currentType) {
            SqlSession newSession = sessionFactory.openSession(type, session.getConnection());

            session.clearCache();
            localSession.set(newSession);
            localExecutorType.set(type);
        }
    }
}

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

License:Apache License

/**
 * //ww  w . j  a v  a  2 s  .  c om
 * ?? {@link SqlSession}  {@link Connection}
 * 
 */
public final Connection getConnection() {
    Connection conn = null;
    SqlSession session = localSession.get();

    if (session != null)
        conn = session.getConnection();

    return conn;
}

From source file:org.mybatis.cdi.ManagerProducers.java

License:Apache License

private SqlSessionFactory createSessionManager(int n) throws IOException {
    Reader reader = Resources.getResourceAsReader("org/mybatis/cdi/mybatis-config_" + n + ".xml");
    SqlSessionFactory manager = new SqlSessionFactoryBuilder().build(reader);
    reader.close();//from   w  ww  .  j  a va  2  s. c om

    SqlSession session = manager.openSession();
    Connection conn = session.getConnection();
    reader = Resources.getResourceAsReader("org/mybatis/cdi/CreateDB_" + n + ".sql");
    ScriptRunner runner = new ScriptRunner(conn);
    runner.setLogWriter(null);
    runner.runScript(reader);
    reader.close();
    session.close();

    return manager;
}