Search in sources :

Example 1 with AbstractLobCreatingPreparedStatementCallback

use of org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback in project camel by apache.

the class JdbcAggregationRepository method insertAndUpdateHelper.

protected void insertAndUpdateHelper(final CamelContext camelContext, final String key, final Exchange exchange, String sql, final boolean idComesFirst) throws Exception {
    final byte[] data = codec.marshallExchange(camelContext, exchange, allowSerializedHeaders);
    jdbcTemplate.execute(sql, new AbstractLobCreatingPreparedStatementCallback(getLobHandler()) {

        @Override
        protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
            int totalParameterIndex = 0;
            lobCreator.setBlobAsBytes(ps, ++totalParameterIndex, data);
            if (idComesFirst) {
                ps.setString(++totalParameterIndex, key);
            }
            if (storeBodyAsText) {
                ps.setString(++totalParameterIndex, exchange.getIn().getBody(String.class));
            }
            if (hasHeadersToStoreAsText()) {
                for (String headerName : headersToStoreAsText) {
                    String headerValue = exchange.getIn().getHeader(headerName, String.class);
                    ps.setString(++totalParameterIndex, headerValue);
                }
            }
            if (!idComesFirst) {
                ps.setString(++totalParameterIndex, key);
            }
        }
    });
}
Also used : AbstractLobCreatingPreparedStatementCallback(org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Example 2 with AbstractLobCreatingPreparedStatementCallback

use of org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback in project perun by CESNET.

the class Auditer method storeMessageToDb.

/**
	 * Store the message to the DB.
	 *
	 * @param sess
	 * @param message
	 */
public void storeMessageToDb(final PerunSession sess, final String message) {
    synchronized (LOCK_DB_TABLE_AUDITER_LOG) {
        try {
            final int msgId = Utils.getNewId(jdbc, "auditer_log_id_seq");
            jdbc.execute("insert into auditer_log (id, msg, actor, created_at, created_by_uid) values (?,?,?," + Compatibility.getSysdate() + ",?)", new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

                public void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException {
                    ps.setInt(1, msgId);
                    lobCreator.setClobAsString(ps, 2, message);
                    ps.setString(3, sess.getPerunPrincipal().getActor());
                    ps.setInt(4, sess.getPerunPrincipal().getUserId());
                }
            });
        } catch (RuntimeException e) {
            log.error("Cannot store auditer log message ['{}'], exception: {}", message, e);
        } catch (InternalErrorException e) {
            log.error("Cannot get unique id for new auditer log message ['{}'], exception: {}", message, e);
        }
    }
}
Also used : AbstractLobCreatingPreparedStatementCallback(org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) LobCreator(org.springframework.jdbc.support.lob.LobCreator)

Aggregations

PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 AbstractLobCreatingPreparedStatementCallback (org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback)2 LobCreator (org.springframework.jdbc.support.lob.LobCreator)2 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)1