Java Code Examples for java.sql.Clob#free()

The following examples show how to use java.sql.Clob#free() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Util.java    From rxjava-jdbc with Apache License 2.0 6 votes vote down vote up
/**
 * Automatically frees the clob (<code>Clob.free()</code>) once the clob
 * Reader is closed.
 * 
 * @param clob
 * @param reader
 * @return
 */
private static Reader createFreeOnCloseReader(final Clob clob, final Reader reader) {
    return new Reader() {

        @Override
        public void close() throws IOException {
            try {
                reader.close();
            } finally {
                try {
                    clob.free();
                } catch (SQLException e) {
                    log.debug(e.getMessage());
                }
            }
        }

        @Override
        public int read(char[] cbuf, int off, int len) throws IOException {
            return reader.read(cbuf, off, len);
        }
    };
}
 
Example 2
Source File: JdbcUtils.java    From datax-web with MIT License 5 votes vote down vote up
public static void close(Clob x) {
    if (x == null) {
        return;
    }

    try {
        x.free();
    } catch (Exception e) {
        LOG.debug("close error", e);
    }
}
 
Example 3
Source File: TemporaryLobCreator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void close() {
	try {
		for (Blob blob : this.temporaryBlobs) {
			blob.free();
		}
		for (Clob clob : this.temporaryClobs) {
			clob.free();
		}
	}
	catch (SQLException ex) {
		logger.error("Could not free LOB", ex);
	}
}
 
Example 4
Source File: TemporaryLobCreator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
	try {
		for (Blob blob : this.temporaryBlobs) {
			blob.free();
		}
		for (Clob clob : this.temporaryClobs) {
			clob.free();
		}
	}
	catch (SQLException ex) {
		logger.error("Could not free LOB", ex);
	}
}
 
Example 5
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ClobChunk getClobChunk(int connId, int lobId, long offset,
    int chunkSize, boolean freeLobAtEnd, ByteBuffer token)
    throws GFXDException {
  try {
    EmbedConnection conn = getValidConnection(connId, token).getConnection();
    Object lob = conn.getLOBMapping(lobId);
    if (lob instanceof Clob) {
      Clob clob = (Clob)lob;
      long length = clob.length() - offset;
      if (length > Integer.MAX_VALUE) {
        throw Util.generateCsSQLException(SQLState.BLOB_TOO_LARGE_FOR_CLIENT,
            Long.toString(length), Long.toString(Integer.MAX_VALUE));
      }
      ClobChunk chunk = new ClobChunk().setLobId(lobId).setOffset(offset);
      if (chunkSize > 0 && chunkSize < length) {
        chunk.setChunk(clob.getSubString(offset + 1, chunkSize)).setLast(
            false);
      }
      else {
        chunk.setChunk(clob.getSubString(offset + 1, (int)length)).setLast(
            true);
        if (freeLobAtEnd) {
          conn.removeLOBMapping(lobId);
          clob.free();
        }
      }
      return chunk;
    }
    else {
      throw Util.generateCsSQLException(SQLState.LOB_LOCATOR_INVALID);
    }
  } catch (Throwable t) {
    throw gfxdException(t);
  }
}
 
Example 6
Source File: GFXDServiceImpl.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public ClobChunk getClobChunk(int connId, int lobId, long offset,
    int chunkSize, boolean freeLobAtEnd, ByteBuffer token)
    throws GFXDException {
  try {
    EmbedConnection conn = getValidConnection(connId, token).getConnection();
    Object lob = conn.getLOBMapping(lobId);
    if (lob instanceof Clob) {
      Clob clob = (Clob)lob;
      long length = clob.length() - offset;
      if (length > Integer.MAX_VALUE) {
        throw Util.generateCsSQLException(SQLState.BLOB_TOO_LARGE_FOR_CLIENT,
            Long.toString(length), Long.toString(Integer.MAX_VALUE));
      }
      ClobChunk chunk = new ClobChunk().setLobId(lobId).setOffset(offset);
      if (chunkSize > 0 && chunkSize < length) {
        chunk.setChunk(clob.getSubString(offset + 1, chunkSize)).setLast(
            false);
      }
      else {
        chunk.setChunk(clob.getSubString(offset + 1, (int)length)).setLast(
            true);
        if (freeLobAtEnd) {
          conn.removeLOBMapping(lobId);
          clob.free();
        }
      }
      return chunk;
    }
    else {
      throw Util.generateCsSQLException(SQLState.LOB_LOCATOR_INVALID);
    }
  } catch (Throwable t) {
    throw gfxdException(t);
  }
}
 
Example 7
Source File: TemporaryLobCreator.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
	try {
		for (Blob blob : this.temporaryBlobs) {
			blob.free();
		}
		for (Clob clob : this.temporaryClobs) {
			clob.free();
		}
	}
	catch (SQLException ex) {
		logger.error("Could not free LOB", ex);
	}
}
 
Example 8
Source File: UtilTest.java    From rxjava-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoMapClobToByteArray() throws SQLException {
    Clob clob = EasyMock.createMock(Clob.class);
    String s = "hello there";
    expect(clob.getCharacterStream()).andReturn(new StringReader(s)).once();
    clob.free();
    EasyMock.expectLastCall().once();
    replay(clob);
    Object string = autoMap(clob, String.class);
    assertEquals(s, string);
    verify(clob);
}
 
Example 9
Source File: TableWebSocket.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
/**
 * Filter out and count messages according to payloadFilter
 *
 * @param criteria
 * @param opcodes Null when all opcodes should be retrieved.
 * @param inScopeChannelIds
 * @param payloadFilter Null when all payloads should be retrieved.
 * @param payloadLength
 * @return number of message that fulfill given template
 * @throws DatabaseException
 */
private int countMessageWithPayloadFilter(
        WebSocketMessageDTO criteria,
        List<Integer> opcodes,
        List<Integer> inScopeChannelIds,
        WebSocketMessagesPayloadFilter payloadFilter,
        int payloadLength)
        throws DatabaseException {
    String query =
            "SELECT m.opcode, m.payload_utf8 FROM websocket_message AS m "
                    + "LEFT OUTER JOIN websocket_message_fuzz f "
                    + "ON m.message_id = f.message_id AND m.channel_id = f.channel_id "
                    + "<where> ";
    int count = 0;
    try {
        PreparedStatement stmt =
                buildMessageCriteriaStatement(query, criteria, opcodes, inScopeChannelIds);
        stmt.execute();
        ResultSet resultSet = stmt.getResultSet();
        try {
            while (resultSet.next()) {
                String payload;
                // read payload
                if (resultSet.getInt("opcode") != WebSocketMessage.OPCODE_BINARY) {

                    if (payloadLength == -1) {
                        // load all characters
                        payload = resultSet.getString("payload_utf8");
                    } else {
                        Clob clob = resultSet.getClob("payload_utf8");
                        int length = Math.min(payloadLength, (int) clob.length());
                        payload = clob.getSubString(1, length);
                        clob.free();
                    }
                    if (payloadFilter.isStringValidWithPattern(payload)) {
                        count++;
                    }
                }
            }
        } finally {
            resultSet.close();
            stmt.close();
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }

    return count;
}