Examples of JdbcChannelException


Examples of org.apache.flume.channel.jdbc.JdbcChannelException

          channelName, tx.getConnection());

      tx.commit();
    } catch (Exception ex) {
      tx.rollback();
      throw new JdbcChannelException("Failed to persist event", ex);
    } finally {
      if (tx != null) {
        tx.close();
      }
    }
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

      String canonicalDbDirPath = null;

      try {
        canonicalDbDirPath = dbDir.getCanonicalPath();
      } catch (IOException ex) {
        throw new JdbcChannelException("Unable to find canonical path of dir: "
            + defaultDbDir, ex);
      }

      if (!dbDir.exists()) {
        if (!dbDir.mkdirs()) {
          throw new JdbcChannelException("unable to create directory: "
              + canonicalDbDirPath);
        }
      }

      connectUrl = "jdbc:derby:" + canonicalDbDirPath + "/db;create=true";

      // No jdbc properties file will be used
      jdbcPropertiesFile = null;

      LOGGER.warn("Overriding values for - driver: " + driverClassName
          + ", user: " + userName + "connectUrl: " + connectUrl
          + ", jdbc properties file: " + jdbcPropertiesFile
          + ", dbtype: " + dbTypeName);
    }

    // Right now only Derby and MySQL supported
    databaseType = DatabaseType.getByName(dbTypeName);

    switch (databaseType) {
    case DERBY:
    case MYSQL:
      break;
    default:
      throw new JdbcChannelException("Database " + databaseType
          + " not supported at this time");
    }

    // Register driver
    if (driverClassName == null || driverClassName.trim().length() == 0) {
      throw new JdbcChannelException("No jdbc driver specified");
    }

    try {
      Class.forName(driverClassName);
    } catch (ClassNotFoundException ex) {
      throw new JdbcChannelException("Unable to load driver: "
                  + driverClassName, ex);
    }

    // JDBC Properties
    Properties jdbcProps = new Properties();

    if (jdbcPropertiesFile != null && jdbcPropertiesFile.trim().length() > 0) {
      File jdbcPropsFile = new File(jdbcPropertiesFile.trim());
      if (!jdbcPropsFile.exists()) {
        throw new JdbcChannelException("Jdbc properties file does not exist: "
            + jdbcPropertiesFile);
      }

      InputStream inStream = null;
      try {
        inStream = new FileInputStream(jdbcPropsFile);
        jdbcProps.load(inStream);
      } catch (IOException ex) {
        throw new JdbcChannelException("Unable to load jdbc properties "
            + "from file: " + jdbcPropertiesFile, ex);
      } finally {
        if (inStream != null) {
          try {
            inStream.close();
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

      try {
        connection.rollback();
      } catch (SQLException ex2) {
        LOGGER.error("Unable to rollback transaction", ex2);
      }
      throw new JdbcChannelException("Unable to query schema", ex);
    } finally {
      if (stmt != null) {
        try {
          stmt.close();
        } catch (SQLException ex) {
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

      try {
        connection.rollback();
      } catch (SQLException ex2) {
        LOGGER.error("Unable to rollback transaction", ex2);
      }
      throw new JdbcChannelException("Unable to run query: "
          + COLUMN_LOOKUP_QUERY + ": 1=" + tableName + ", 2=" + schemaName, ex);
    } finally {
      if (pStmt != null) {
        try {
          pStmt.close();
        } catch (SQLException ex) {
          LOGGER.error("Unable to close statement", ex);
        }
        if (connection != null) {
          try {
            connection.close();
          } catch (SQLException ex) {
            LOGGER.error("Unable to close connection", ex);
          }
        }
      }
    }

    Set<String> columnDiff = new HashSet<String>();
    columnDiff.addAll(columnNames);

    // Expected Column string form
    StringBuilder sb = new StringBuilder("{");
    boolean first = true;
    for (String column : columns) {
      columnDiff.remove(column);
      if (first) {
        first = false;
      } else {
        sb.append(", ");
      }
      sb.append(column);
    }
    sb.append("}");

    String expectedColumns = sb.toString();

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Table " + schemaName + "." + tableName
          + " expected columns: " + expectedColumns + ", actual columns: "
          + columnNames);
    }

    if (columnNames.size() != columns.length || columnDiff.size() != 0) {
      throw new JdbcChannelException("Expected table " + schemaName + "."
          + tableName + " to have columns: " + expectedColumns + ". Instead "
          + "found columns: " + columnNames);
    }
  }
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

      try {
        connection.rollback();
      } catch (SQLException ex2) {
        LOGGER.error("Unable to rollback transaction", ex2);
      }
      throw new JdbcChannelException("Unable to run query: "
          + query, ex);
    } finally {
      if (stmt != null) {
        try {
          stmt.close();
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

      baseEventStmt.setString(2, channelName);
      baseEventStmt.setBoolean(3, hasSpillPayload);

      int baseEventCount = baseEventStmt.executeUpdate();
      if (baseEventCount != 1) {
        throw new JdbcChannelException("Invalid update count on base "
            + "event insert: " + baseEventCount);
      }
      // Extract event ID and set it
      ResultSet eventIdResult = baseEventStmt.getGeneratedKeys();

      if (!eventIdResult.next()) {
        throw new JdbcChannelException("Unable to retrieive inserted event-id");
      }

      long eventId = eventIdResult.getLong(1);
      pe.setEventId(eventId);

      // Persist the payload spill
      if (hasSpillPayload) {
         spillEventStmt = connection.prepareStatement(STMT_INSERT_EVENT_SPILL);
         spillEventStmt.setLong(1, eventId);
         spillEventStmt.setBinaryStream(2,
             new ByteArrayInputStream(spillPayload), spillPayload.length);
         int spillEventCount = spillEventStmt.executeUpdate();
         if (spillEventCount != 1) {
           throw new JdbcChannelException("Invalid update count on spill "
               + "event insert: " + spillEventCount);
         }
      }

      // Persist the headers
      List<HeaderEntry> headers = pe.getHeaderEntries();
      if (headers != null && headers.size() > 0) {
        List<HeaderEntry> headerWithNameSpill = new ArrayList<HeaderEntry>();
        List<HeaderEntry> headerWithValueSpill = new ArrayList<HeaderEntry>();

        baseHeaderStmt = connection.prepareStatement(STMT_INSERT_HEADER_BASE,
                                Statement.RETURN_GENERATED_KEYS);
        Iterator<HeaderEntry> it = headers.iterator();
        while (it.hasNext()) {
          HeaderEntry entry = it.next();
          SpillableString name = entry.getName();
          SpillableString value = entry.getValue();
          baseHeaderStmt.setLong(1, eventId);
          baseHeaderStmt.setString(2, name.getBase());
          baseHeaderStmt.setString(3, value.getBase());
          baseHeaderStmt.setBoolean(4, name.hasSpill());
          baseHeaderStmt.setBoolean(5, value.hasSpill());

          int updateCount = baseHeaderStmt.executeUpdate();
          if (updateCount != 1) {
             throw new JdbcChannelException("Unexpected update header count: "
                 + updateCount);
          }
          ResultSet headerIdResultSet = baseHeaderStmt.getGeneratedKeys();
          if (!headerIdResultSet.next()) {
            throw new JdbcChannelException(
                "Unable to retrieve inserted header id");
          }
          long headerId = headerIdResultSet.getLong(1);
          entry.setId(headerId);

          if (name.hasSpill()) {
            headerWithNameSpill.add(entry);
          }

          if (value.hasSpill()) {
            headerWithValueSpill.add(entry);
          }
        }

        // Persist header name spills
        if (headerWithNameSpill.size() > 0) {
          LOGGER.debug("Number of headers with name spill: "
                  + headerWithNameSpill.size());

          headerNameSpillStmt =
              connection.prepareStatement(STMT_INSERT_HEADER_NAME_SPILL);

          for (HeaderEntry entry : headerWithNameSpill) {
            String nameSpill = entry.getName().getSpill();

            headerNameSpillStmt.setLong(1, entry.getId());
            headerNameSpillStmt.setString(2, nameSpill);
            headerNameSpillStmt.addBatch();
          }

          int[] nameSpillUpdateCount = headerNameSpillStmt.executeBatch();
          if (nameSpillUpdateCount.length != headerWithNameSpill.size()) {
            throw new JdbcChannelException("Unexpected update count for header "
                + "name spills: expected " + headerWithNameSpill.size() + ", "
                + "found " + nameSpillUpdateCount.length);
          }

          for (int i = 0; i < nameSpillUpdateCount.length; i++) {
            if (nameSpillUpdateCount[i] != 1) {
              throw new JdbcChannelException("Unexpected update count for "
                  + "header name spill at position " + i + ", value: "
                  + nameSpillUpdateCount[i]);
            }
          }
        }

        // Persist header value spills
        if (headerWithValueSpill.size() > 0) {
          LOGGER.debug("Number of headers with value spill: "
              + headerWithValueSpill.size());

          headerValueSpillStmt =
              connection.prepareStatement(STMT_INSERT_HEADER_VALUE_SPILL);

          for(HeaderEntry entry : headerWithValueSpill) {
            String valueSpill = entry.getValue().getSpill();

            headerValueSpillStmt.setLong(1, entry.getId());
            headerValueSpillStmt.setString(2, valueSpill);
            headerValueSpillStmt.addBatch();
          }

          int[] valueSpillUpdateCount = headerValueSpillStmt.executeBatch();
          if (valueSpillUpdateCount.length != headerWithValueSpill.size()) {
            throw new JdbcChannelException("Unexpected update count for header "
                + "value spills: expected " + headerWithValueSpill.size() + ", "
                + "found " + valueSpillUpdateCount.length);
          }

          for (int i = 0; i < valueSpillUpdateCount.length; i++) {
            if (valueSpillUpdateCount[i] != 1) {
              throw new JdbcChannelException("Unexpected update count for "
                  + "header value spill at position " + i + ", value: "
                  + valueSpillUpdateCount[i]);
            }
          }
        }
      }
    } catch (SQLException ex) {
      throw new JdbcChannelException("Unable to persist event: " + pe, ex);
    } finally {
      if (baseEventStmt != null) {
        try {
          baseEventStmt.close();
        } catch (SQLException ex) {
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

            connection.prepareStatement(STMT_FETCH_PAYLOAD_SPILL);

        spillEventFetchStmt.setLong(1, eventId);
        ResultSet rsetSpillEvent = spillEventFetchStmt.executeQuery();
        if (!rsetSpillEvent.next()) {
          throw new JdbcChannelException("Payload spill expected but not "
              + "found for event: " + eventId);
        }
        Blob payloadSpillBlob = rsetSpillEvent.getBlob(1);
        payloadInputStream = payloadSpillBlob.getBinaryStream();
        ByteArrayOutputStream spillStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = payloadInputStream.read(buffer)) != -1) {
          spillStream.write(buffer, 0, length);
        }
        peBuilder.setSpillPayload(spillStream.toByteArray());

        // Delete this spill
        deleteSpillEventStmt =
            connection.prepareStatement(STMT_DELETE_EVENT_SPILL);
        deleteSpillEventStmt.setLong(1, eventId);

        int updateCount = deleteSpillEventStmt.executeUpdate();
        if (updateCount != 1) {
          throw new JdbcChannelException("Unexpected row count for spill "
              + "delete: " + updateCount);
        }
      }

      if (rsetBaseEvent.next()) {
        throw new JdbcChannelException("More than expected events retrieved");
      }

      // Populate headers
      List<Long> nameSpillHeaders = null;
      List<Long> valueSpillHeaders = null;
      baseHeaderFetchStmt = connection.prepareStatement(STMT_FETCH_HEADER_BASE);
      baseHeaderFetchStmt.setLong(1, eventId);
      int headerCount = 0; // for later delete validation

      ResultSet rsetBaseHeader = baseHeaderFetchStmt.executeQuery();
      while (rsetBaseHeader.next()) {
        headerCount++;
        long headerId = rsetBaseHeader.getLong(1);
        String baseName = rsetBaseHeader.getString(2);
        String baseValue = rsetBaseHeader.getString(3);
        boolean hasNameSpill = rsetBaseHeader.getBoolean(4);
        boolean hasValueSpill = rsetBaseHeader.getBoolean(5);

        peBuilder.setHeader(headerId, baseName, baseValue);
        if (hasNameSpill) {
          if (nameSpillHeaders == null) {
            nameSpillHeaders = new ArrayList<Long>();
          }
          nameSpillHeaders.add(headerId);
        }

        if (hasValueSpill) {
          if (valueSpillHeaders == null) {
            valueSpillHeaders = new ArrayList<Long>();
          }
          valueSpillHeaders.add(headerId);
        }
      }

      if (nameSpillHeaders != null) {

        nameSpillHeaderStmt =
            connection.prepareStatement(STMT_FETCH_HEADER_NAME_SPILL);

        deleteNameSpillHeaderStmt =
            connection.prepareStatement(STMT_DELETE_HEADER_NAME_SPILL);
        for (long headerId : nameSpillHeaders) {
          nameSpillHeaderStmt.setLong(1, headerId);
          ResultSet rsetHeaderNameSpill = nameSpillHeaderStmt.executeQuery();
          if (!rsetHeaderNameSpill.next()) {
            throw new JdbcChannelException("Name spill was set for header "
                + headerId + " but was not found");
          }
          String nameSpill = rsetHeaderNameSpill.getString(1);

          peBuilder.setHeaderNameSpill(headerId, nameSpill);
          deleteNameSpillHeaderStmt.setLong(1, headerId);
          deleteNameSpillHeaderStmt.addBatch();
        }

        // Delete header name spills
        int[] headerNameSpillDelete = deleteNameSpillHeaderStmt.executeBatch();
        if (headerNameSpillDelete.length != nameSpillHeaders.size()) {
          throw new JdbcChannelException("Unexpected number of header name "
              + "spill deletes: expected " + nameSpillHeaders.size()
              + ", found: " + headerNameSpillDelete.length);
        }

        for (int numRowsAffected : headerNameSpillDelete) {
          if (numRowsAffected != 1) {
            throw new JdbcChannelException("Unexpected number of deleted rows "
                + "for header name spill deletes: " + numRowsAffected);
          }
        }
      }

      if (valueSpillHeaders != null) {
        valueSpillHeaderStmt =
            connection.prepareStatement(STMT_FETCH_HEADER_VALUE_SPILL);

        deleteValueSpillHeaderStmt =
            connection.prepareStatement(STMT_DELETE_HEADER_VALUE_SPILL);
        for (long headerId: valueSpillHeaders) {
          valueSpillHeaderStmt.setLong(1, headerId);
          ResultSet rsetHeaderValueSpill = valueSpillHeaderStmt.executeQuery();
          if (!rsetHeaderValueSpill.next()) {
            throw new JdbcChannelException("Value spill was set for header "
                + headerId + " but was not found");
          }
          String valueSpill = rsetHeaderValueSpill.getString(1);

          peBuilder.setHeaderValueSpill(headerId, valueSpill);
          deleteValueSpillHeaderStmt.setLong(1, headerId);
          deleteValueSpillHeaderStmt.addBatch();
        }
        // Delete header value spills
        int[] headerValueSpillDelete = deleteValueSpillHeaderStmt.executeBatch();
        if (headerValueSpillDelete.length != valueSpillHeaders.size()) {
          throw new JdbcChannelException("Unexpected number of header value "
              + "spill deletes: expected " + valueSpillHeaders.size()
              + ", found: " + headerValueSpillDelete.length);
        }

        for (int numRowsAffected : headerValueSpillDelete) {
          if (numRowsAffected != 1) {
            throw new JdbcChannelException("Unexpected number of deleted rows "
                + "for header value spill deletes: " + numRowsAffected);
          }
        }
      }

      // Now delete Headers
      if (headerCount > 0) {
        deleteBaseHeaderStmt =
            connection.prepareStatement(STMT_DELETE_HEADER_BASE);
        deleteBaseHeaderStmt.setLong(1, eventId);

        int rowCount = deleteBaseHeaderStmt.executeUpdate();
        if (rowCount != headerCount) {
          throw new JdbcChannelException("Unexpected base header delete count: "
              + "expected: " + headerCount + ", found: " + rowCount);
        }
      }

      // Now delete the Event
      deleteBaseEventStmt = connection.prepareStatement(STMT_DELETE_EVENT_BASE);
      deleteBaseEventStmt.setLong(1, eventId);
      int rowCount = deleteBaseEventStmt.executeUpdate();

      if (rowCount != 1) {
        throw new JdbcChannelException("Unexpected row count for delete of "
            + "event-id: " + eventId + ", count: " + rowCount);
      }

    } catch (SQLException ex) {
      throw new JdbcChannelException("Unable to retrieve event", ex);
    } catch (IOException ex) {
      throw new JdbcChannelException("Unable to read data", ex);
    } finally {
      if (payloadInputStream != null) {
        try {
          payloadInputStream.close();
        } catch (IOException ex) {
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

    try {
      stmt = connection.createStatement();
      stmt.execute(QUERY_CHANNEL_SIZE);
      ResultSet rset = stmt.getResultSet();
      if (!rset.next()) {
        throw new JdbcChannelException("Failed to determine channel size: "
              + "Query (" + QUERY_CHANNEL_SIZE
              + ") did not produce any results");
      }

      size = rset.getLong(1);
      connection.commit();
    } catch (SQLException ex) {
      try {
        connection.rollback();
      } catch (SQLException ex2) {
        LOGGER.error("Unable to rollback transaction", ex2);
      }
      throw new JdbcChannelException("Unable to run query: "
          + QUERY_CHANNEL_SIZE, ex);
    } finally {
      if (stmt != null) {
        try {
          stmt.close();
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

      if (bHeaderParts == null) {
        bHeaderParts = new HashMap<Long, HeaderPart>();
      }
      HeaderPart hp = new HeaderPart(baseName, baseValue);
      if (bHeaderParts.put(headerId, hp) != null) {
        throw new JdbcChannelException("Duplicate header found: "
            + "headerId: " + headerId + ", baseName: " + baseName + ", "
            + "baseValue: " + baseValue);
      }

      return this;
View Full Code Here

Examples of org.apache.flume.channel.jdbc.JdbcChannelException

    }

    public Builder setHeaderNameSpill(long headerId, String nameSpill) {
      HeaderPart hp = bHeaderParts.get(headerId);
      if (hp == null) {
        throw new JdbcChannelException("Header not found for spill: "
            + headerId);
      }

      hp.setSpillName(nameSpill);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.