Search in sources :

Example 1 with IDbmsSupport

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

the class JdbcTransactionalStorage method storeMessageInDatabase.

protected String storeMessageInDatabase(Connection conn, String messageId, String correlationId, Timestamp receivedDateTime, String comments, String label, Serializable message) throws IOException, SQLException, JdbcException, SenderException {
    PreparedStatement stmt = null;
    try {
        IDbmsSupport dbmsSupport = getDbmsSupport();
        if (log.isDebugEnabled()) {
            log.debug("preparing insert statement [" + insertQuery + "]");
        }
        if (!dbmsSupport.mustInsertEmptyBlobBeforeData()) {
            stmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
        } else {
            stmt = conn.prepareStatement(insertQuery);
        }
        stmt.clearParameters();
        int parPos = 0;
        if (StringUtils.isNotEmpty(getTypeField())) {
            stmt.setString(++parPos, type);
        }
        if (StringUtils.isNotEmpty(getSlotId())) {
            stmt.setString(++parPos, getSlotId());
        }
        if (StringUtils.isNotEmpty(getHostField())) {
            stmt.setString(++parPos, host);
        }
        if (StringUtils.isNotEmpty(getLabelField())) {
            stmt.setString(++parPos, label);
        }
        stmt.setString(++parPos, messageId);
        stmt.setString(++parPos, correlationId);
        stmt.setTimestamp(++parPos, receivedDateTime);
        stmt.setString(++parPos, comments);
        if (type.equalsIgnoreCase(TYPE_MESSAGELOG_PIPE) || type.equalsIgnoreCase(TYPE_MESSAGELOG_RECEIVER)) {
            if (getRetention() < 0) {
                stmt.setTimestamp(++parPos, null);
            } else {
                Date date = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.DAY_OF_MONTH, getRetention());
                stmt.setTimestamp(++parPos, new Timestamp(cal.getTime().getTime()));
            }
        } else {
            stmt.setTimestamp(++parPos, null);
        }
        if (!isStoreFullMessage()) {
            if (isOnlyStoreWhenMessageIdUnique()) {
                stmt.setString(++parPos, messageId);
                stmt.setString(++parPos, slotId);
            }
            stmt.execute();
            return null;
        }
        if (!dbmsSupport.mustInsertEmptyBlobBeforeData()) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            if (isBlobsCompressed()) {
                DeflaterOutputStream dos = new DeflaterOutputStream(out);
                ObjectOutputStream oos = new ObjectOutputStream(dos);
                oos.writeObject(message);
                dos.close();
            } else {
                ObjectOutputStream oos = new ObjectOutputStream(out);
                oos.writeObject(message);
            }
            stmt.setBytes(++parPos, out.toByteArray());
            if (isOnlyStoreWhenMessageIdUnique()) {
                stmt.setString(++parPos, messageId);
                stmt.setString(++parPos, slotId);
            }
            stmt.execute();
            ResultSet rs = stmt.getGeneratedKeys();
            boolean messageIdExists = false;
            if (rs.next() && rs.getString(1) != null) {
                return "<results><result>" + rs.getString(1) + "</result></results>";
            } else {
                messageIdExists = true;
            }
            if (messageIdExists) {
                boolean isMessageDifferent = isMessageDifferent(conn, messageId, message);
                String resultString = createResultString(isMessageDifferent);
                log.warn("MessageID [" + messageId + "] already exists");
                if (isMessageDifferent) {
                    log.warn("Message with MessageID [" + messageId + "] is not equal");
                }
                return resultString;
            }
        }
        if (isOnlyStoreWhenMessageIdUnique()) {
            stmt.setString(++parPos, messageId);
            stmt.setString(++parPos, slotId);
        }
        stmt.execute();
        int updateCount = stmt.getUpdateCount();
        if (log.isDebugEnabled()) {
            log.debug("update count for insert statement: " + updateCount);
        }
        if (updateCount > 0) {
            if (log.isDebugEnabled()) {
                log.debug("preparing select statement [" + selectKeyQuery + "]");
            }
            stmt = conn.prepareStatement(selectKeyQuery);
            ResultSet rs = null;
            try {
                // retrieve the key
                rs = stmt.executeQuery();
                if (!rs.next()) {
                    throw new SenderException("could not retrieve key of stored message");
                }
                String newKey = rs.getString(1);
                rs.close();
                // and update the blob
                if (log.isDebugEnabled()) {
                    log.debug("preparing update statement [" + updateBlobQuery + "]");
                }
                stmt = conn.prepareStatement(updateBlobQuery);
                stmt.clearParameters();
                stmt.setString(1, newKey);
                rs = stmt.executeQuery();
                if (!rs.next()) {
                    throw new SenderException("could not retrieve row for stored message [" + messageId + "]");
                }
                Object blobHandle = dbmsSupport.getBlobUpdateHandle(rs, 1);
                OutputStream out = dbmsSupport.getBlobOutputStream(rs, 1, blobHandle);
                if (isBlobsCompressed()) {
                    DeflaterOutputStream dos = new DeflaterOutputStream(out);
                    ObjectOutputStream oos = new ObjectOutputStream(dos);
                    oos.writeObject(message);
                    oos.close();
                    dos.close();
                } else {
                    ObjectOutputStream oos = new ObjectOutputStream(out);
                    oos.writeObject(message);
                    oos.close();
                }
                out.close();
                dbmsSupport.updateBlob(rs, 1, blobHandle);
                return newKey;
            } finally {
                if (rs != null) {
                    rs.close();
                }
            }
        } else {
            if (isOnlyStoreWhenMessageIdUnique()) {
                return "already there";
            } else {
                throw new SenderException("update count for update statement not greater than 0 [" + updateCount + "]");
            }
        }
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}
Also used : Calendar(java.util.Calendar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Timestamp(java.sql.Timestamp) Date(java.util.Date) IDbmsSupport(nl.nn.adapterframework.jdbc.dbms.IDbmsSupport) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ResultSet(java.sql.ResultSet) SenderException(nl.nn.adapterframework.core.SenderException)

Example 2 with IDbmsSupport

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

the class Result2LobWriterBase method createWriter.

protected Writer createWriter(IPipeLineSession session, String streamId, ParameterResolutionContext prc) throws Exception {
    querySender.sendMessage(streamId, streamId);
    Connection conn = querySender.getConnection();
    openConnections.put(streamId, conn);
    PreparedStatement stmt = querySender.getStatement(conn, session.getMessageId(), streamId, true);
    ResultSet rs = stmt.executeQuery();
    openResultSets.put(streamId, rs);
    IDbmsSupport dbmsSupport = querySender.getDbmsSupport();
    Object lobHandle = getLobHandle(dbmsSupport, rs);
    openLobHandles.put(streamId, lobHandle);
    return getWriter(dbmsSupport, lobHandle, rs);
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IDbmsSupport(nl.nn.adapterframework.jdbc.dbms.IDbmsSupport) PreparedStatement(java.sql.PreparedStatement)

Example 3 with IDbmsSupport

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

the class JdbcTransactionalStorage method checkTable.

private void checkTable(Connection connection) throws JdbcException {
    IDbmsSupport dbms = getDbmsSupport();
    String schemaOwner = getSchemaOwner4Check();
    log.debug("checking for presence of table [" + getTableName() + "] in schema/catalog [" + schemaOwner + "]");
    if (dbms.isTablePresent(connection, getSchemaOwner4Check(), getTableName())) {
        checkTableColumnPresent(connection, dbms, getKeyField());
        checkTableColumnPresent(connection, dbms, getTypeField());
        checkTableColumnPresent(connection, dbms, getSlotIdField());
        checkTableColumnPresent(connection, dbms, getHostField());
        checkTableColumnPresent(connection, dbms, getIdField());
        checkTableColumnPresent(connection, dbms, getCorrelationIdField());
        checkTableColumnPresent(connection, dbms, getDateField());
        checkTableColumnPresent(connection, dbms, getCommentField());
        if (isStoreFullMessage()) {
            checkTableColumnPresent(connection, dbms, getMessageField());
        }
        checkTableColumnPresent(connection, dbms, getExpiryDateField());
        checkTableColumnPresent(connection, dbms, getLabelField());
    } else {
        String msg = "Table [" + getTableName() + "] not present";
        ConfigurationWarnings.getInstance().add(getLogPrefix() + msg);
    }
}
Also used : IDbmsSupport(nl.nn.adapterframework.jdbc.dbms.IDbmsSupport)

Aggregations

IDbmsSupport (nl.nn.adapterframework.jdbc.dbms.IDbmsSupport)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 Connection (java.sql.Connection)1 Timestamp (java.sql.Timestamp)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 DeflaterOutputStream (java.util.zip.DeflaterOutputStream)1 SenderException (nl.nn.adapterframework.core.SenderException)1