use of com.wplatform.ddal.excutor.Optional in project jdbc-shards by wplatform.
the class JdbcCallWorker method doWork.
@Override
public Integer doWork() {
Connection conn = null;
PreparedStatement stmt = null;
try {
DataSource dataSource = getDataSource();
Optional optional = Optional.build().shardName(shardName).readOnly(false);
if (trace.isDebugEnabled()) {
trace.debug("{0} Fetching connection from DataSource.", shardName);
}
conn = session.applyConnection(dataSource, optional);
attach(conn);
if (trace.isDebugEnabled()) {
trace.debug("{0} Preparing: {1};", shardName, sql);
}
stmt = conn.prepareStatement(sql);
attach(stmt);
applyQueryTimeout(stmt);
if (params != null) {
for (int i = 0, size = params.size(); i < size; i++) {
Value v = params.get(i);
v.set(stmt, i + 1);
if (trace.isDebugEnabled()) {
trace.debug("{0} setParameter: {1} -> {2};", shardName, i + 1, v.getSQL());
}
}
}
int rows = stmt.executeUpdate();
if (trace.isDebugEnabled()) {
trace.debug("{0} executeUpdate: {1} affected.", shardName, rows);
}
return rows;
} catch (SQLException e) {
StatementBuilder buff = new StatementBuilder();
buff.append(shardName).append(" executing executeUpdate error:").append(sql);
if (params != null && params.size() > 0) {
buff.append("\n{");
int i = 1;
for (Value v : params) {
buff.appendExceptFirst(", ");
buff.append(i++).append(": ").append(v.getSQL());
}
buff.append('}');
}
buff.append(';');
trace.error(e, buff.toString());
throw wrapException(sql, e);
}
}
use of com.wplatform.ddal.excutor.Optional in project jdbc-shards by wplatform.
the class TableMate method readMataData.
/**
* @param session
*/
public void readMataData(Session session, TableNode matadataNode) {
for (int retry = 0; ; retry++) {
try {
Connection conn = null;
String tableName = matadataNode.getCompositeObjectName();
String shardName = matadataNode.getShardName();
try {
DataSourceRepository dsRepository = session.getDataSourceRepository();
DataSource dataSource = dsRepository.getDataSourceByShardName(shardName);
Optional optional = Optional.build().shardName(shardName).readOnly(false);
conn = session.applyConnection(dataSource, optional);
tableName = database.identifier(tableName);
tryReadMetaData(conn, tableName);
return;
} catch (Exception e) {
throw DbException.convert(e);
} finally {
JdbcUtils.closeSilently(conn);
}
} catch (DbException e) {
if (retry >= MAX_RETRY) {
throw e;
}
}
}
}
Aggregations