Search in sources :

Example 1 with DataSource

use of org.unidal.dal.jdbc.datasource.DataSource 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 DataSource

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

the class XpipeDalTransactionManager method startTransaction.

@Override
public void startTransaction(String datasource) {
    TransactionInfo trxInfo = m_threadLocalData.get();
    if (trxInfo.getRecursiveLayer() < INITIAL_STATUS) {
        throw new DalRuntimeException("Cannot start transaction.");
    }
    if (INITIAL_STATUS == trxInfo.getRecursiveLayer()) {
        DataSource ds = m_dataSourceManager.getDataSource(datasource);
        Connection connection = null;
        try {
            connection = ds.getConnection();
            connection.setAutoCommit(false);
            trxInfo.setConnection(connection);
            trxInfo.setDataSourceName(datasource);
            trxInfo.setInTransaction(true);
            trxInfo.incrRecursiveLayer();
        } catch (SQLException e) {
            closeConnection(connection);
            throw new DalRuntimeException("Error when getting connection from DataSource(" + datasource + "), message: " + e, e);
        }
    } else {
        trxInfo.incrRecursiveLayer();
    }
}
Also used : DalRuntimeException(org.unidal.dal.jdbc.DalRuntimeException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DataSource(org.unidal.dal.jdbc.datasource.DataSource)

Example 3 with DataSource

use of org.unidal.dal.jdbc.datasource.DataSource 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 4 with DataSource

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

the class XpipeDalTransactionManager method getConnection.

@Override
public Connection getConnection(QueryContext ctx) {
    String logicalName = ctx.getEntityInfo().getLogicalName();
    TableProvider tableProvider = m_tableProviderManager.getTableProvider(logicalName);
    String dataSourceName = tableProvider.getDataSourceName(ctx.getQueryHints(), logicalName);
    TransactionInfo trxInfo = m_threadLocalData.get();
    ctx.setDataSourceName(dataSourceName);
    if (trxInfo.isInTransaction()) {
        if (dataSourceName.equals(trxInfo.getDataSourceName())) {
            return trxInfo.getConnection();
        } else {
            throw new DalRuntimeException("Only one datasource can participate in a transaction. Now: " + trxInfo.getDataSourceName() + ", you provided: " + dataSourceName);
        }
    } else {
        // Not in transaction
        DataSource dataSource = m_dataSourceManager.getDataSource(dataSourceName);
        Connection connection = null;
        SQLException exception = null;
        try {
            connection = trxInfo.getConnection();
            if (connection == null) {
                connection = dataSource.getConnection();
            }
            connection.setAutoCommit(true);
        } catch (SQLException e) {
            exception = e;
        }
        // retry once if pooled connection is closed by server side
        if (exception != null) {
            closeConnection(connection);
            m_logger.warn(String.format("Iffy database(%s) connection closed, try to reconnect.", dataSourceName), exception);
            try {
                connection = dataSource.getConnection();
                connection.setAutoCommit(true);
                exception = null;
            } catch (SQLException e) {
                closeConnection(connection);
                m_logger.warn(String.format("Unable to reconnect to database(%s).", dataSourceName), e);
            }
        }
        if (exception != null) {
            throw new DalRuntimeException("Error when getting connection from DataSource(" + dataSourceName + "), message: " + exception, exception);
        } else {
            trxInfo.setConnection(connection);
            trxInfo.setDataSourceName(dataSourceName);
            trxInfo.setInTransaction(false);
            return connection;
        }
    }
}
Also used : DalRuntimeException(org.unidal.dal.jdbc.DalRuntimeException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TableProvider(org.unidal.dal.jdbc.mapping.TableProvider) DataSource(org.unidal.dal.jdbc.datasource.DataSource)

Example 5 with DataSource

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

the class DalTransactionManagerTest method testGetConnectionExceptionForStartTransaction.

@Test
public void testGetConnectionExceptionForStartTransaction() throws SQLException {
    DataSource ds = mock(DataSource.class);
    Connection conn = mock(Connection.class);
    when(ds.getConnection()).thenReturn(conn);
    when(m_dataSourceManager.getDataSource("")).thenReturn(ds);
    doThrow(new SQLException()).when(conn).setAutoCommit(false);
    thrown.expect(DalRuntimeException.class);
    dalTM.startTransaction("");
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DataSource(org.unidal.dal.jdbc.datasource.DataSource) Test(org.junit.Test) AbstractConsoleTest(com.ctrip.xpipe.redis.console.AbstractConsoleTest)

Aggregations

DataSource (org.unidal.dal.jdbc.datasource.DataSource)6 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)3 AbstractConsoleTest (com.ctrip.xpipe.redis.console.AbstractConsoleTest)2 Test (org.junit.Test)2 DalRuntimeException (org.unidal.dal.jdbc.DalRuntimeException)2 DataSourceManager (org.unidal.dal.jdbc.datasource.DataSourceManager)2 TableProvider (org.unidal.dal.jdbc.mapping.TableProvider)1