Search in sources :

Example 1 with DBPoolView

use of org.apache.synapse.commons.datasource.DBPoolView in project wso2-synapse by wso2.

the class AbstractDBMediator method getPreparedStatement.

/**
 * Return a Prepared statement for the given Statement object, which is ready to be executed
 *
 * @param stmnt  SQL stataement to be executed
 * @param con    The connection to be used
 * @param msgCtx Current message context
 * @return a PreparedStatement
 * @throws SQLException on error
 */
protected PreparedStatement getPreparedStatement(Statement stmnt, Connection con, MessageContext msgCtx) throws SQLException {
    SynapseLog synLog = getLog(msgCtx);
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Getting a connection from DataSource " + getDSName() + " and preparing statement : " + stmnt.getRawStatement());
    }
    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;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) DBPoolView(org.apache.synapse.commons.datasource.DBPoolView) SynapseException(org.apache.synapse.SynapseException) PreparedStatement(java.sql.PreparedStatement) PreparedStatement(java.sql.PreparedStatement) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) BigDecimal(java.math.BigDecimal)

Example 2 with DBPoolView

use of org.apache.synapse.commons.datasource.DBPoolView in project wso2-synapse by wso2.

the class AbstractDBMediator method lookupDataSource.

/**
 * Lookup the DataSource on JNDI using the specified name and optional properties
 *
 * @param dataSourceName the name of the data source to lookup
 * @param jndiProperties the JNDI properties identifying a data source provider
 * @return a DataSource looked up using the specified JNDI properties
 */
private DataSource lookupDataSource(String dataSourceName, Properties jndiProperties) {
    DataSource dataSource = null;
    RepositoryBasedDataSourceFinder finder = DataSourceRepositoryHolder.getInstance().getRepositoryBasedDataSourceFinder();
    if (finder.isInitialized()) {
        // first try a lookup based on the data source name only
        dataSource = finder.find(dataSourceName);
    }
    if (dataSource == null) {
        // decrypt the password if needed
        String password = jndiProperties.getProperty(Context.SECURITY_CREDENTIALS);
        if (password != null && !"".equals(password)) {
            jndiProperties.put(Context.SECURITY_CREDENTIALS, getActualPassword(password));
        }
        // lookup the data source using the specified jndi properties
        dataSource = DataSourceFinder.find(dataSourceName, jndiProperties);
    }
    if (dataSource != null) {
        MBeanRepository mBeanRepository = DatasourceMBeanRepository.getInstance();
        Object mBean = mBeanRepository.getMBean(dataSourceName);
        if (mBean instanceof DBPoolView) {
            setDbPoolView((DBPoolView) mBean);
        }
        log.info("Successfully looked up datasource " + dataSourceName + ".");
    }
    return dataSource;
}
Also used : DBPoolView(org.apache.synapse.commons.datasource.DBPoolView) RepositoryBasedDataSourceFinder(org.apache.synapse.commons.datasource.RepositoryBasedDataSourceFinder) MBeanRepository(org.apache.synapse.commons.jmx.MBeanRepository) DatasourceMBeanRepository(org.apache.synapse.commons.datasource.DatasourceMBeanRepository) PerUserPoolDataSource(org.apache.commons.dbcp.datasources.PerUserPoolDataSource) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Aggregations

BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 DBPoolView (org.apache.synapse.commons.datasource.DBPoolView)2 BigDecimal (java.math.BigDecimal)1 PreparedStatement (java.sql.PreparedStatement)1 DataSource (javax.sql.DataSource)1 PerUserPoolDataSource (org.apache.commons.dbcp.datasources.PerUserPoolDataSource)1 SynapseException (org.apache.synapse.SynapseException)1 SynapseLog (org.apache.synapse.SynapseLog)1 DatasourceMBeanRepository (org.apache.synapse.commons.datasource.DatasourceMBeanRepository)1 RepositoryBasedDataSourceFinder (org.apache.synapse.commons.datasource.RepositoryBasedDataSourceFinder)1 MBeanRepository (org.apache.synapse.commons.jmx.MBeanRepository)1