* @throws SQLException on error
*/
protected PreparedStatement getPreparedStatement(Statement stmnt,
MessageContext msgCtx) throws SQLException {
SynapseLog synLog = getLog(msgCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Getting a connection from DataSource " + getDSName() +
" and preparing statement : " + stmnt.getRawStatement());
}
Connection con = getDataSource().getConnection();
if (con == null) {
String msg = "Connection from DataSource " + getDSName() + " is null.";
log.error(msg);
throw new SynapseException(msg);
}
if (dataSource instanceof BasicDataSource) {
BasicDataSource basicDataSource = (BasicDataSource) dataSource;
int numActive = basicDataSource.getNumActive();
int numIdle = basicDataSource.getNumIdle();
String connectionId = Integer.toHexString(con.hashCode());
DBPoolView dbPoolView = getDbPoolView();
if (dbPoolView != null) {
dbPoolView.setNumActive(numActive);
dbPoolView.setNumIdle(numIdle);
dbPoolView.updateConnectionUsage(connectionId);
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("[ DB Connection : " + con + " ]");
synLog.traceOrDebug("[ DB Connection instance identifier : " +
connectionId + " ]");
synLog.traceOrDebug("[ Number of Active Connection : " + numActive + " ]");
synLog.traceOrDebug("[ Number of Idle Connection : " + numIdle + " ]");
}
}
PreparedStatement ps = con.prepareStatement(stmnt.getRawStatement());
// set parameters if any
List<Statement.Parameter> params = stmnt.getParameters();
int column = 1;
for (Statement.Parameter param : params) {
if (param == null) {
continue;
}
String value = (param.getPropertyName() != null ?
param.getPropertyName() : param.getXpath().stringValueOf(msgCtx));
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Setting as parameter : " + column + " value : " + value +
" as JDBC Type : " + param.getType() + "(see java.sql.Types for valid " +
"types)");
}
switch (param.getType()) {
// according to J2SE 1.5 /docs/guide/jdbc/getstart/mapping.html
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR: {
if (value != null && value.length() != 0) {
ps.setString(column++, value);
} else {
ps.setString(column++, null);
}
break;
}
case Types.NUMERIC:
case Types.DECIMAL: {
if (value != null && value.length() != 0) {
ps.setBigDecimal(column++, new BigDecimal(value));
} else {
ps.setBigDecimal(column++, null);
}
break;
}
case Types.BIT: {
if (value != null && value.length() != 0) {
ps.setBoolean(column++, Boolean.parseBoolean(value));
} else {
ps.setNull(column++, Types.BIT);
}
break;
}
case Types.TINYINT: {
if (value != null && value.length() != 0) {
ps.setByte(column++, Byte.parseByte(value));
} else {
ps.setNull(column++, Types.TINYINT);
}
break;
}
case Types.SMALLINT: {
if (value != null && value.length() != 0) {
ps.setShort(column++, Short.parseShort(value));
} else {
ps.setNull(column++, Types.SMALLINT);
}
break;
}
case Types.INTEGER: {
if (value != null && value.length() != 0) {
ps.setInt(column++, Integer.parseInt(value));
} else {
ps.setNull(column++, Types.INTEGER);
}
break;
}
case Types.BIGINT: {
if (value != null && value.length() != 0) {
ps.setLong(column++, Long.parseLong(value));
} else {
ps.setNull(column++, Types.BIGINT);
}
break;
}
case Types.REAL: {
if (value != null && value.length() != 0) {
ps.setFloat(column++, Float.parseFloat(value));
} else {
ps.setNull(column++, Types.REAL);
}
break;
}
case Types.FLOAT: {
if (value != null && value.length() != 0) {
ps.setDouble(column++, Double.parseDouble(value));
} else {
ps.setNull(column++, Types.FLOAT);
}
break;
}
case Types.DOUBLE: {
if (value != null && value.length() != 0) {
ps.setDouble(column++, Double.parseDouble(value));
} else {
ps.setNull(column++, Types.DOUBLE);
}
break;
}
// skip BINARY, VARBINARY and LONGVARBINARY
case Types.DATE: {
if (value != null && value.length() != 0) {
ps.setDate(column++, Date.valueOf(value));
} else {
ps.setNull(column++, Types.DATE);
}
break;
}
case Types.TIME: {
if (value != null && value.length() != 0) {
ps.setTime(column++, Time.valueOf(value));
} else {
ps.setNull(column++, Types.TIME);
}
break;
}
case Types.TIMESTAMP: {
if (value != null && value.length() != 0) {
ps.setTimestamp(column++, Timestamp.valueOf(value));
} else {
ps.setNull(column++, Types.TIMESTAMP);
}
break;
}
// skip CLOB, BLOB, ARRAY, DISTINCT, STRUCT, REF, JAVA_OBJECT
default: {
String msg = "Trying to set an un-supported JDBC Type : " + param.getType() +
" against column : " + column + " and statement : " +
stmnt.getRawStatement() +
" used by a DB mediator against DataSource : " + getDSName() +
" (see java.sql.Types for valid type values)";
handleException(msg, msgCtx);
}
}
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Successfully prepared statement : " + stmnt.getRawStatement() +
" against DataSource : " + getDSName());
}
return ps;
}