Search in sources :

Example 1 with DataSourceManager

use of org.unidal.dal.jdbc.datasource.DataSourceManager in project x-pipe by ctripcorp.

the class AbstractConsoleH2DbTest method setUpTestDataSource.

private void setUpTestDataSource() throws ComponentLookupException, SQLException, IOException {
    DataSourceManager dsManager = ContainerLoader.getDefaultContainer().lookup(DataSourceManager.class);
    DataSource dataSource = dsManager.getDataSource(DATA_SOURCE);
    String driver = dataSource.getDescriptor().getProperty("driver", null);
    if (driver != null && driver.equals("org.h2.Driver")) {
        executeSqlScript(FileUtils.readFileAsString(TABLE_STRUCTURE));
        executeSqlScript(FileUtils.readFileAsString(TABLE_DATA));
    } else {
        logger.info("[setUpTestDataSource][do not clean]{}", driver);
    }
    executeSqlScript(prepareDatas());
}
Also used : DataSourceManager(org.unidal.dal.jdbc.datasource.DataSourceManager) DataSource(org.unidal.dal.jdbc.datasource.DataSource)

Example 2 with DataSourceManager

use of org.unidal.dal.jdbc.datasource.DataSourceManager in project x-pipe by ctripcorp.

the class H2Init method setUpTestDataSource.

private void setUpTestDataSource() throws ComponentLookupException, SQLException, IOException {
    DataSourceManager dsManager = ContainerLoader.getDefaultContainer().lookup(DataSourceManager.class);
    DataSource dataSource = dsManager.getDataSource(DATA_SOURCE);
    String driver = dataSource.getDescriptor().getProperty("driver", null);
    if (driver != null && driver.equals("org.h2.Driver")) {
        executeSqlScript(FileUtils.readFileAsString(TABLE_STRUCTURE));
        executeSqlScript(FileUtils.readFileAsString(TABLE_DATA));
        executeSqlScript(FileUtils.readFileAsString(DEMO_DATA));
    } else {
        logger.info("[setUpTestDataSource][do not clean]{}", driver);
    }
}
Also used : DataSourceManager(org.unidal.dal.jdbc.datasource.DataSourceManager) DataSource(org.unidal.dal.jdbc.datasource.DataSource)

Example 3 with DataSourceManager

use of org.unidal.dal.jdbc.datasource.DataSourceManager in project x-pipe by ctripcorp.

the class AbstractConsoleH2DbTest method executeSqlScript.

protected void executeSqlScript(String prepareSql) throws ComponentLookupException, SQLException {
    DataSourceManager dsManager = ContainerLoader.getDefaultContainer().lookup(DataSourceManager.class);
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dsManager.getDataSource(DATA_SOURCE).getConnection();
        conn.setAutoCommit(false);
        if (!Strings.isEmpty(prepareSql)) {
            for (String sql : prepareSql.split(";")) {
                logger.debug("[setup][data]{}", sql.trim());
                stmt = conn.prepareStatement(sql);
                stmt.executeUpdate();
            }
        }
        conn.commit();
    } catch (Exception ex) {
        logger.error("[SetUpTestDataSource][fail]:", ex);
        if (null != conn) {
            conn.rollback();
        }
    } finally {
        if (null != stmt) {
            stmt.close();
        }
        if (null != conn) {
            conn.setAutoCommit(true);
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DataSourceManager(org.unidal.dal.jdbc.datasource.DataSourceManager) IOException(java.io.IOException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) SQLException(java.sql.SQLException)

Example 4 with DataSourceManager

use of org.unidal.dal.jdbc.datasource.DataSourceManager in project x-pipe by ctripcorp.

the class H2Init method executeSqlScript.

protected void executeSqlScript(String prepareSql) throws ComponentLookupException, SQLException {
    DataSourceManager dsManager = ContainerLoader.getDefaultContainer().lookup(DataSourceManager.class);
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dsManager.getDataSource(DATA_SOURCE).getConnection();
        conn.setAutoCommit(false);
        if (!Strings.isEmpty(prepareSql)) {
            for (String sql : prepareSql.split(";")) {
                logger.debug("[setup][data]{}", sql.trim());
                stmt = conn.prepareStatement(sql);
                stmt.executeUpdate();
            }
        }
        conn.commit();
    } catch (Exception ex) {
        logger.error("[SetUpTestDataSource][fail]:", ex);
        if (null != conn) {
            conn.rollback();
        }
    } finally {
        if (null != stmt) {
            stmt.close();
        }
        if (null != conn) {
            conn.setAutoCommit(true);
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DataSourceManager(org.unidal.dal.jdbc.datasource.DataSourceManager) IOException(java.io.IOException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) SQLException(java.sql.SQLException)

Aggregations

DataSourceManager (org.unidal.dal.jdbc.datasource.DataSourceManager)4 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)2 DataSource (org.unidal.dal.jdbc.datasource.DataSource)2