use of org.dbflute.exception.DfProcedureExecutionMetaGettingFailureException in project dbflute-core by dbflute.
the class DfProcedureExecutionMetaExtractor method doExtractExecutionMetaData.
protected void doExtractExecutionMetaData(DataSource dataSource, DfProcedureMeta procedure) throws SQLException {
final List<DfProcedureColumnMeta> columnList = procedure.getProcedureColumnList();
if (!needsToCall(columnList)) {
final String name = procedure.buildProcedureLoggingName();
_log.info("...Skipping unneeded call: " + name + " params=" + buildParameterTypeView(columnList));
return;
}
final List<Object> testValueList = DfCollectionUtil.newArrayList();
setupTestValueList(columnList, testValueList);
final boolean existsReturn = existsReturnValue(columnList);
final String sql = createSql(procedure, existsReturn, true);
Connection conn = null;
CallableStatement cs = null;
boolean beginTransaction = false;
try {
_log.info("...Calling: " + sql);
conn = dataSource.getConnection();
conn.setAutoCommit(false);
beginTransaction = true;
cs = conn.prepareCall(sql);
final List<DfProcedureColumnMeta> boundColumnList = DfCollectionUtil.newArrayList();
setupBindParameter(conn, cs, columnList, testValueList, boundColumnList);
boolean executed;
try {
executed = cs.execute();
} catch (SQLException e) {
// retry without escape because Oracle sometimes hates escape
final String retrySql = createSql(procedure, existsReturn, false);
try {
try {
cs.close();
} catch (SQLException ignored) {
}
cs = conn.prepareCall(retrySql);
setupBindParameter(conn, cs, columnList, testValueList, boundColumnList);
executed = cs.execute();
_log.info(" (o) retry: " + retrySql);
} catch (SQLException ignored) {
_log.info(" (x) retry: " + retrySql);
throw e;
}
}
if (executed) {
int closetIndex = 0;
do {
ResultSet rs = null;
try {
rs = cs.getResultSet();
if (rs == null) {
break;
}
final Map<String, DfColumnMeta> columnMetaInfoMap = extractColumnMetaInfoMap(rs, sql);
final DfProcedureNotParamResultMeta notParamResult = new DfProcedureNotParamResultMeta();
final String propertyName;
if (procedure.isCalledBySelect() && closetIndex == 0) {
// for example, table valued function
// if the procedure of this type does not have
// second or more result set basically
// but checks closetIndex just in case
propertyName = "returnResult";
} else {
// basically here
propertyName = "notParamResult" + (closetIndex + 1);
}
notParamResult.setPropertyName(propertyName);
notParamResult.setResultSetColumnInfoMap(columnMetaInfoMap);
procedure.addNotParamResult(notParamResult);
++closetIndex;
} finally {
closeResult(rs);
}
} while (cs.getMoreResults());
}
int index = 0;
for (DfProcedureColumnMeta column : boundColumnList) {
final DfProcedureColumnType columnType = column.getProcedureColumnType();
if (DfProcedureColumnType.procedureColumnIn.equals(columnType)) {
++index;
continue;
}
final int paramIndex = (index + 1);
final Object obj;
if (column.isPostgreSQLCursor()) {
obj = _postgreSqlResultSetType.getValue(cs, paramIndex);
} else if (column.isOracleCursor()) {
obj = _oracleResultSetType.getValue(cs, paramIndex);
} else {
// as default
obj = cs.getObject(paramIndex);
}
if (obj instanceof ResultSet) {
ResultSet rs = null;
try {
rs = (ResultSet) obj;
final Map<String, DfColumnMeta> columnMetaInfoMap = extractColumnMetaInfoMap(rs, sql);
column.setResultSetColumnInfoMap(columnMetaInfoMap);
} finally {
closeResult(rs);
}
}
++index;
}
} catch (SQLException e) {
final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
br.addNotice("Failed to execute the procedure for getting meta data.");
br.addItem("SQL");
br.addElement(sql);
br.addItem("Parameter");
for (DfProcedureColumnMeta column : columnList) {
br.addElement(column.getColumnDisplayName());
}
br.addItem("Test Value");
br.addElement(buildTestValueDisp(testValueList));
br.addItem("Exception Message");
br.addElement(DfJDBCException.extractMessage(e));
final SQLException nextEx = e.getNextException();
if (nextEx != null) {
br.addElement(DfJDBCException.extractMessage(nextEx));
}
final String msg = br.buildExceptionMessage();
final DfOutsideSqlProperties prop = getProperties().getOutsideSqlProperties();
if (prop.hasSpecifiedExecutionMetaProcedure()) {
throw new DfProcedureExecutionMetaGettingFailureException(msg, e);
} else {
// if no specified, it continues
_continuedFailureMessageMap.put(procedure.getProcedureFullQualifiedName(), msg);
_log.info("*Failed to call so read the warning message displayed later");
}
} finally {
if (cs != null) {
cs.close();
}
if (conn != null) {
try {
conn.rollback();
} catch (SQLException continued) {
// one day Oracle suddenly threw it (by socket trouble?)
final String exp = DfJDBCException.extractMessage(continued);
_log.info("*Failed to roll-back the procedure call but continued: " + exp);
}
if (beginTransaction) {
try {
conn.setAutoCommit(true);
} catch (SQLException continued) {
final String exp = DfJDBCException.extractMessage(continued);
_log.info("*Failed to set auto-commit true: " + exp);
}
}
}
}
}
Aggregations