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());
}
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();
}
}
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);
}
}
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;
}
}
}
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("");
}
Aggregations