Search in sources :

Example 21 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method executeIntQuery.

/**
 * exectues query that returns an integer. Returns -1 if no results are found.
 */
public static int executeIntQuery(Connection connection, String query, String param1, String param2) throws JdbcException {
    PreparedStatement stmt = null;
    try {
        if (log.isDebugEnabled())
            log.debug("prepare and execute query [" + query + "]" + displayParameters(param1, param2));
        stmt = connection.prepareStatement(query);
        applyParameters(stmt, param1, param2);
        ResultSet rs = stmt.executeQuery();
        try {
            if (!rs.next()) {
                return -1;
            }
            return rs.getInt(1);
        } finally {
            rs.close();
        }
    } catch (Exception e) {
        throw new JdbcException("could not obtain value using query [" + query + "]" + displayParameters(param1, param2), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw new JdbcException("could not close statement of query [" + query + "]" + displayParameters(param1, param2), e);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 22 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class Locker method lock.

public String lock() throws JdbcException, SQLException, InterruptedException {
    Connection conn = getConnection();
    try {
        if (!JdbcUtil.tableExists(conn, "ibisLock")) {
            if (isIgnoreTableNotExist()) {
                log.info("table [ibisLock] does not exist, ignoring lock");
                return LOCK_IGNORED;
            } else {
                throw new JdbcException("table [ibisLock] does not exist");
            }
        }
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            log.error("error closing JdbcConnection", e);
        }
    }
    String objectIdWithSuffix = null;
    int r = -1;
    while (objectIdWithSuffix == null && (numRetries == -1 || r < numRetries)) {
        r++;
        if (r == 0 && firstDelay > 0) {
            Thread.sleep(firstDelay);
        }
        if (r > 0) {
            Thread.sleep(retryDelay);
        }
        Date date = new Date();
        objectIdWithSuffix = getObjectId();
        if (StringUtils.isNotEmpty(getDateFormatSuffix())) {
            String formattedDate = formatter.format(date);
            objectIdWithSuffix = objectIdWithSuffix.concat(formattedDate);
        }
        log.debug("preparing to set lock [" + objectIdWithSuffix + "]");
        conn = getConnection();
        try {
            PreparedStatement stmt = conn.prepareStatement(insertQuery);
            stmt.clearParameters();
            stmt.setString(1, objectIdWithSuffix);
            stmt.setString(2, getType());
            stmt.setString(3, Misc.getHostname());
            stmt.setTimestamp(4, new Timestamp(date.getTime()));
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            if (getType().equalsIgnoreCase("T")) {
                cal.add(Calendar.HOUR_OF_DAY, getRetention());
            } else {
                cal.add(Calendar.DAY_OF_MONTH, getRetention());
            }
            stmt.setTimestamp(5, new Timestamp(cal.getTime().getTime()));
            stmt.executeUpdate();
            log.debug("lock [" + objectIdWithSuffix + "] set");
        } catch (SQLException e) {
            log.debug(getLogPrefix() + "error executing insert query (as part of locker): " + e.getMessage());
            if (numRetries == -1 || r < numRetries) {
                log.debug(getLogPrefix() + "will try again");
                objectIdWithSuffix = null;
            } else {
                log.debug(getLogPrefix() + "will not try again");
                throw e;
            }
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                log.error("error closing JdbcConnection", e);
            }
        }
    }
    return objectIdWithSuffix;
}
Also used : SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 23 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method executeStatement.

public static void executeStatement(Connection connection, String query, int param1, int param2, String param3, String param4) throws JdbcException {
    PreparedStatement stmt = null;
    try {
        if (log.isDebugEnabled())
            log.debug("prepare and execute query [" + query + "]" + displayParameters(param1, param2, param3, param4));
        stmt = connection.prepareStatement(query);
        applyParameters(stmt, param1, param2, param3, param4);
        stmt.execute();
    } catch (Exception e) {
        throw new JdbcException("could not execute query [" + query + "]" + displayParameters(param1, param2, param3, param4), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw new JdbcException("could not close statement for query [" + query + "]" + displayParameters(param1, param2, param3, param4), e);
            }
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 24 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class JdbcUtil method executeIntQuery.

public static int executeIntQuery(Connection connection, String query, int param1, String param2, String param3) throws JdbcException {
    PreparedStatement stmt = null;
    try {
        if (log.isDebugEnabled())
            log.debug("prepare and execute query [" + query + "]" + displayParameters(param1, param2, param3));
        stmt = connection.prepareStatement(query);
        applyParameters(stmt, param1, param2, param3);
        ResultSet rs = stmt.executeQuery();
        try {
            if (!rs.next()) {
                return -1;
            }
            return rs.getInt(1);
        } finally {
            rs.close();
        }
    } catch (Exception e) {
        throw new JdbcException("could not obtain value using query [" + query + "]" + displayParameters(param1, param2, param3), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw new JdbcException("could not close statement of query [" + query + "]" + displayParameters(param1, param2, param3), e);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 25 with JdbcException

use of nl.nn.adapterframework.jdbc.JdbcException in project iaf by ibissource.

the class ShowSecurityItems method addJmsRealms.

private ArrayList<Object> addJmsRealms() {
    List<String> jmsRealms = JmsRealmFactory.getInstance().getRegisteredRealmNamesAsList();
    ArrayList<Object> jmsRealmList = new ArrayList<Object>();
    String confResString;
    try {
        confResString = Misc.getConfigurationResources();
        if (confResString != null) {
            confResString = XmlUtils.removeNamespaces(confResString);
        }
    } catch (IOException e) {
        log.warn("error getting configuration resources [" + e + "]");
        confResString = null;
    }
    for (int j = 0; j < jmsRealms.size(); j++) {
        Map<String, Object> realm = new HashMap<String, Object>();
        String jmsRealm = (String) jmsRealms.get(j);
        String dsName = null;
        String qcfName = null;
        String tcfName = null;
        String dsInfo = null;
        String qcfInfo = null;
        DirectQuerySender qs = (DirectQuerySender) ibisManager.getIbisContext().createBeanAutowireByName(DirectQuerySender.class);
        qs.setJmsRealm(jmsRealm);
        try {
            dsName = qs.getDataSourceNameToUse();
            dsInfo = qs.getDatasourceInfo();
        } catch (JdbcException jdbce) {
        // no datasource
        }
        if (StringUtils.isNotEmpty(dsName)) {
            realm.put("name", jmsRealm);
            realm.put("datasourceName", dsName);
            realm.put("info", dsInfo);
            if (confResString != null) {
                String connectionPoolProperties = Misc.getConnectionPoolProperties(confResString, "JDBC", dsName);
                if (StringUtils.isNotEmpty(connectionPoolProperties)) {
                    realm.put("connectionPoolProperties", connectionPoolProperties);
                }
            }
        }
        JmsSender js = new JmsSender();
        js.setJmsRealm(jmsRealm);
        try {
            qcfName = js.getConnectionFactoryName();
            qcfInfo = js.getConnectionFactoryInfo();
        } catch (JmsException jmse) {
        // no connectionFactory
        }
        if (StringUtils.isNotEmpty(qcfName)) {
            realm.put("name", jmsRealm);
            realm.put("queueConnectionFactoryName", qcfName);
            realm.put("info", qcfInfo);
            if (confResString != null) {
                String connectionPoolProperties = Misc.getConnectionPoolProperties(confResString, "JMS", qcfName);
                if (StringUtils.isNotEmpty(connectionPoolProperties)) {
                    realm.put("connectionPoolProperties", connectionPoolProperties);
                }
            }
        }
        tcfName = js.getTopicConnectionFactoryName();
        if (StringUtils.isNotEmpty(tcfName)) {
            realm.put("name", jmsRealm);
            realm.put("topicConnectionFactoryName", tcfName);
        }
        jmsRealmList.add(realm);
    }
    return jmsRealmList;
}
Also used : HashMap(java.util.HashMap) JmsException(nl.nn.adapterframework.jms.JmsException) ArrayList(java.util.ArrayList) DirectQuerySender(nl.nn.adapterframework.jdbc.DirectQuerySender) IOException(java.io.IOException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JmsSender(nl.nn.adapterframework.jms.JmsSender)

Aggregations

JdbcException (nl.nn.adapterframework.jdbc.JdbcException)26 SQLException (java.sql.SQLException)19 IOException (java.io.IOException)15 PreparedStatement (java.sql.PreparedStatement)13 ResultSet (java.sql.ResultSet)9 JMSException (javax.jms.JMSException)9 DataFormatException (java.util.zip.DataFormatException)8 Connection (java.sql.Connection)7 InputStream (java.io.InputStream)6 SenderException (nl.nn.adapterframework.core.SenderException)6 FileNotFoundException (java.io.FileNotFoundException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)4 InputStreamReader (java.io.InputStreamReader)3 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)3 JmsException (nl.nn.adapterframework.jms.JmsException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2