Search in sources :

Example 16 with JdbcException

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

the class EsbUtils method getPoolingDataSource.

public static PoolingDataSource getPoolingDataSource(JdbcTransactionalStorage errorStorage) {
    String dsUrl = null;
    String dsUserName = null;
    String dsPassword = null;
    java.sql.Connection errorStorageConnection = null;
    try {
        errorStorageConnection = errorStorage.getConnection();
    } catch (JdbcException e) {
        log.warn("error occured during getting errorStorage connection: " + e.getMessage());
    }
    if (errorStorageConnection == null) {
        log.warn("could not get errorStorage connection");
    } else {
        DatabaseMetaData md;
        try {
            md = errorStorageConnection.getMetaData();
            dsUrl = md.getURL();
        // dsUserName = md.getUserName();
        // dsPassword = md.getPassword();
        } catch (SQLException e) {
            log.warn("error occured during getting errorStorage metadata: " + e.getMessage());
        }
        if (dsUrl == null) {
            log.warn("could not get errorStorage url");
        } else {
            // onderstaande is nodig omdat het niet mogelijk is het
            // password op te vragen uit het DatabaseMetaData object of
            // uit het Connection object
            String confResString = null;
            try {
                confResString = Misc.getConfigurationResources();
                if (confResString != null) {
                    confResString = XmlUtils.removeNamespaces(confResString);
                }
            } catch (IOException e) {
                log.warn("error getting configuration resources: " + e.getMessage());
            }
            String authDataAlias = null;
            if (confResString != null) {
                String dsName = null;
                try {
                    dsName = errorStorage.getDataSourceNameToUse();
                } catch (JdbcException e) {
                    log.warn("error getting datasource name to use: " + e.getMessage());
                }
                if (dsName != null) {
                    String xpathExpression = "XMI/JDBCProvider/factories[@jndiName='" + dsName + "']/@authDataAlias";
                    try {
                        Transformer t = XmlUtils.createXPathEvaluator(xpathExpression);
                        authDataAlias = XmlUtils.transformXml(t, confResString);
                    } catch (Exception e) {
                        log.warn("error getting datasource authDataAlias: " + e.getMessage());
                    }
                }
            }
            if (StringUtils.isEmpty(authDataAlias)) {
                log.warn("could not get errorStorage authDataAlias");
            } else {
                CredentialFactory cf = new CredentialFactory(authDataAlias, null, null);
                dsUserName = cf.getUsername();
                dsPassword = cf.getPassword();
                return setupJdbcDataSource(dsUrl, dsUserName, dsPassword);
            }
        }
    }
    return null;
}
Also used : Transformer(javax.xml.transform.Transformer) SQLException(java.sql.SQLException) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) IOException(java.io.IOException) DatabaseMetaData(java.sql.DatabaseMetaData) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) ListenerException(nl.nn.adapterframework.core.ListenerException) JmsException(nl.nn.adapterframework.jms.JmsException) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 17 with JdbcException

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

the class ResultSet2FileSender method sendMessage.

protected String sendMessage(Connection connection, String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    int counter = 0;
    ResultSet resultset = null;
    String fileName = (String) prc.getSession().get(getFileNameSessionKey());
    int maxRecords = -1;
    if (StringUtils.isNotEmpty(getMaxRecordsSessionKey())) {
        maxRecords = Integer.parseInt((String) prc.getSession().get(getMaxRecordsSessionKey()));
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(fileName, isAppend());
        PreparedStatement statement = getStatement(connection, correlationID, null, true);
        resultset = statement.executeQuery();
        boolean eor = false;
        if (maxRecords == 0) {
            eor = true;
        }
        while (resultset.next() && !eor) {
            counter++;
            processResultSet(resultset, fos, counter);
            if (maxRecords >= 0 && counter >= maxRecords) {
                ResultSetMetaData rsmd = resultset.getMetaData();
                if (rsmd.getColumnCount() >= 3) {
                    String group = resultset.getString(3);
                    while (resultset.next() && !eor) {
                        String groupNext = resultset.getString(3);
                        if (groupNext.equals(group)) {
                            counter++;
                            processResultSet(resultset, fos, counter);
                        } else {
                            eor = true;
                        }
                    }
                } else {
                    eor = true;
                }
            }
        }
    } catch (FileNotFoundException e) {
        throw new SenderException(getLogPrefix() + "could not find file [" + fileName + "]", e);
    } catch (IOException e) {
        throw new SenderException(getLogPrefix() + "got IOException", e);
    } catch (SQLException sqle) {
        throw new SenderException(getLogPrefix() + "got exception executing a SQL command", sqle);
    } catch (JdbcException e) {
        throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e);
    } finally {
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            log.warn(new SenderException(getLogPrefix() + "got exception closing fileoutputstream", e));
        }
        try {
            if (resultset != null) {
                resultset.close();
            }
        } catch (SQLException e) {
            log.warn(new SenderException(getLogPrefix() + "got exception closing resultset", e));
        }
    }
    return "<result><rowsprocessed>" + counter + "</rowsprocessed></result>";
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) FileNotFoundException(java.io.FileNotFoundException) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException)

Example 18 with JdbcException

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

the class ConfigurationUtils method addConfigToDatabase.

public static boolean addConfigToDatabase(IbisContext ibisContext, String jmsRealm, boolean activate_config, boolean automatic_reload, String name, String version, String fileName, InputStream file, String ruser) throws ConfigurationException {
    if (StringUtils.isEmpty(jmsRealm)) {
        jmsRealm = JmsRealmFactory.getInstance().getFirstDatasourceJmsRealm();
        if (StringUtils.isEmpty(jmsRealm)) {
            return false;
        }
    }
    Connection conn = null;
    ResultSet rs = null;
    FixedQuerySender qs = (FixedQuerySender) ibisContext.createBeanAutowireByName(FixedQuerySender.class);
    qs.setJmsRealm(jmsRealm);
    qs.setQuery("SELECT COUNT(*) FROM IBISCONFIG");
    qs.configure();
    try {
        qs.open();
        conn = qs.getConnection();
        int updated = 0;
        if (activate_config) {
            String query = ("UPDATE IBISCONFIG SET ACTIVECONFIG = '" + (qs.getDbmsSupport().getBooleanValue(false)) + "' WHERE NAME=?");
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setString(1, name);
            updated = stmt.executeUpdate();
        }
        if (updated > 0) {
            String query = ("DELETE FROM IBISCONFIG WHERE NAME=? AND VERSION = ?");
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setString(1, name);
            stmt.setString(2, version);
            stmt.execute();
        }
        String query = ("INSERT INTO IBISCONFIG (NAME, VERSION, FILENAME, CONFIG, CRE_TYDST, RUSER, ACTIVECONFIG, AUTORELOAD) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?, ?)");
        PreparedStatement stmt = conn.prepareStatement(query);
        stmt.setString(1, name);
        stmt.setString(2, version);
        stmt.setString(3, fileName);
        stmt.setBinaryStream(4, file);
        stmt.setString(5, ruser);
        stmt.setObject(6, qs.getDbmsSupport().getBooleanValue(activate_config));
        stmt.setObject(7, qs.getDbmsSupport().getBooleanValue(automatic_reload));
        return stmt.execute();
    } catch (SenderException e) {
        throw new ConfigurationException(e);
    } catch (JdbcException e) {
        throw new ConfigurationException(e);
    } catch (SQLException e) {
        throw new ConfigurationException(e);
    } finally {
        qs.close();
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.warn("Could not close resultset", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.warn("Could not close connection", e);
            }
        }
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SenderException(nl.nn.adapterframework.core.SenderException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 19 with JdbcException

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

the class ConfigurationUtils method makeConfigActive.

public static boolean makeConfigActive(IbisContext ibisContext, String name, String version, String jmsRealm) throws ConfigurationException {
    if (StringUtils.isEmpty(jmsRealm)) {
        jmsRealm = JmsRealmFactory.getInstance().getFirstDatasourceJmsRealm();
        if (StringUtils.isEmpty(jmsRealm)) {
            return false;
        }
    }
    Connection conn = null;
    ResultSet rs = null;
    FixedQuerySender qs = (FixedQuerySender) ibisContext.createBeanAutowireByName(FixedQuerySender.class);
    qs.setJmsRealm(jmsRealm);
    qs.setQuery("SELECT COUNT(*) FROM IBISCONFIG");
    qs.configure();
    try {
        qs.open();
        conn = qs.getConnection();
        int updated = 0;
        String selectQuery = "SELECT NAME FROM IBISCONFIG WHERE NAME=? AND VERSION=?";
        PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
        selectStmt.setString(1, name);
        selectStmt.setString(2, version);
        rs = selectStmt.executeQuery();
        if (rs.next()) {
            String query = ("UPDATE IBISCONFIG SET ACTIVECONFIG = '" + (qs.getDbmsSupport().getBooleanValue(false)) + "' WHERE NAME=?");
            PreparedStatement stmt = conn.prepareStatement(query);
            stmt.setString(1, name);
            updated = stmt.executeUpdate();
            if (updated > 0) {
                String query2 = ("UPDATE IBISCONFIG SET ACTIVECONFIG = '" + (qs.getDbmsSupport().getBooleanValue(true)) + "' WHERE NAME=? AND VERSION=?");
                PreparedStatement stmt2 = conn.prepareStatement(query2);
                stmt2.setString(1, name);
                stmt2.setString(2, version);
                return (stmt2.executeUpdate() > 0) ? true : false;
            }
        }
    } catch (SenderException e) {
        throw new ConfigurationException(e);
    } catch (JdbcException e) {
        throw new ConfigurationException(e);
    } catch (SQLException e) {
        throw new ConfigurationException(e);
    } finally {
        qs.close();
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                log.warn("Could not close resultset", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.warn("Could not close connection", e);
            }
        }
    }
    return false;
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SenderException(nl.nn.adapterframework.core.SenderException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 20 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, 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);
        stmt.execute();
    } catch (Exception e) {
        throw new JdbcException("could not execute query [" + query + "]" + displayParameters(param1, param2), e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw new JdbcException("could not close statement for query [" + query + "]" + displayParameters(param1, param2), 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)

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