use of org.unidal.dal.jdbc.DalRuntimeException 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.DalRuntimeException 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;
}
}
}
Aggregations