Search in sources :

Example 86 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.

the class AggregationTests method initSampleDataIfNecessary.

/**
 * Imports the sample dataset (zips.json) if necessary (e.g. if it doesn't exist yet). The dataset can originally be
 * found on the mongodb aggregation framework example website:
 *
 * @see <a href="https://docs.mongodb.org/manual/tutorial/aggregation-examples/">MongoDB Aggregation Examples</a>
 */
private void initSampleDataIfNecessary() {
    if (!initialized) {
        LOGGER.debug("Server uses MongoDB Version: {}", mongoVersion);
        mongoTemplate.dropCollection(ZipInfo.class);
        mongoTemplate.execute(ZipInfo.class, new CollectionCallback<Void>() {

            @Override
            public Void doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
                Scanner scanner = null;
                try {
                    scanner = new Scanner(new BufferedInputStream(new ClassPathResource("zips.json").getInputStream()));
                    while (scanner.hasNextLine()) {
                        String zipInfoRecord = scanner.nextLine();
                        collection.insertOne(Document.parse(zipInfoRecord));
                    }
                } catch (Exception e) {
                    if (scanner != null) {
                        scanner.close();
                    }
                    throw new RuntimeException("Could not load mongodb sample dataset!", e);
                }
                return null;
            }
        });
        long count = mongoTemplate.count(new Query(), ZipInfo.class);
        assertThat(count, is(29467L));
        initialized = true;
    }
}
Also used : Scanner(java.util.Scanner) MongoException(com.mongodb.MongoException) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) Document(org.bson.Document) ClassPathResource(org.springframework.core.io.ClassPathResource) ParseException(java.text.ParseException) MongoException(com.mongodb.MongoException) DataAccessException(org.springframework.dao.DataAccessException) MappingException(org.springframework.data.mapping.MappingException) ExpectedException(org.junit.rules.ExpectedException) BufferedInputStream(java.io.BufferedInputStream) DataAccessException(org.springframework.dao.DataAccessException)

Example 87 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.

the class GeoJsonTests method shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger.

// DATAMONGO-1453
@Test
public void shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() {
    this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class), new CollectionCallback<Object>() {

        @Override
        public Object doInCollection(MongoCollection<org.bson.Document> collection) throws MongoException, DataAccessException {
            org.bson.Document lineStringRepresentation = new org.bson.Document();
            lineStringRepresentation.put("type", "LineString");
            lineStringRepresentation.put("coordinates", new BasicDbListBuilder().add(new BasicDbListBuilder().add(0).add(0).get()).add(new BasicDbListBuilder().add(1).add(1).get()).get());
            org.bson.Document document = new org.bson.Document();
            document.append("_id", "datamongo-1453");
            document.append("geoJsonLineString", lineStringRepresentation);
            collection.insertOne(document);
            return document;
        }
    });
    assertThat(template.findOne(query(where("id").is("datamongo-1453")), DocumentWithPropertyUsingGeoJsonType.class).geoJsonLineString, is(equalTo(new GeoJsonLineString(new Point(0D, 0D), new Point(1, 1)))));
}
Also used : MongoException(com.mongodb.MongoException) BasicDbListBuilder(org.springframework.data.mongodb.test.util.BasicDbListBuilder) Point(org.springframework.data.geo.Point) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 88 with DataAccessException

use of org.springframework.dao.DataAccessException in project metacat by Netflix.

the class DirectSqlTable method delete.

/**
 * Deletes all the table related information from the store.
 * @param tableName table name
 */
public void delete(final QualifiedName tableName) {
    try {
        final TableSequenceIds ids = getSequenceIds(tableName);
        directSqlSavePartition.delete(tableName);
        jdbcTemplate.update(SQL.UPDATE_SDS_CD, new SqlParameterValue(Types.BIGINT, null), new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.UPDATE_SDS_SERDE, new SqlParameterValue(Types.BIGINT, null), new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        // 
        try {
            jdbcTemplate.update(SQL.DELETE_COLUMNS_OLD, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        } catch (DataAccessException ignored) {
            log.debug("Ignore. Probably table COLUMNS_OLD does not exist.");
        }
        try {
            jdbcTemplate.update(SQL.DELETE_TBL_PRIVS, new SqlParameterValue(Types.BIGINT, ids.getTableId()));
        } catch (DataAccessException ignored) {
            log.debug("Ignore. Probably table TBL_PRIVS does not exist.");
        }
        try {
            jdbcTemplate.update(SQL.DELETE_TBL_COL_PRIVS, new SqlParameterValue(Types.BIGINT, ids.getTableId()));
        } catch (DataAccessException ignored) {
            log.debug("Ignore. Probably table TBL_COL_PRIVS does not exist.");
        }
        jdbcTemplate.update(SQL.DELETE_COLUMNS_V2, new SqlParameterValue(Types.BIGINT, ids.getCdId()));
        jdbcTemplate.update(SQL.DELETE_CDS, new SqlParameterValue(Types.BIGINT, ids.getCdId()));
        jdbcTemplate.update(SQL.DELETE_PARTITION_KEYS, new SqlParameterValue(Types.BIGINT, ids.getTableId()));
        jdbcTemplate.update(SQL.DELETE_TABLE_PARAMS, new SqlParameterValue(Types.BIGINT, ids.getTableId()));
        jdbcTemplate.update(SQL.DELETE_TAB_COL_STATS, new SqlParameterValue(Types.BIGINT, ids.getTableId()));
        jdbcTemplate.update(SQL.UPDATE_TABLE_SD, new SqlParameterValue(Types.BIGINT, null), new SqlParameterValue(Types.BIGINT, ids.getTableId()));
        jdbcTemplate.update(SQL.DELETE_SKEWED_COL_NAMES, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_BUCKETING_COLS, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_SORT_COLS, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_SD_PARAMS, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_SKEWED_COL_VALUE_LOC_MAP, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_SKEWED_VALUES, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_SERDE_PARAMS, new SqlParameterValue(Types.BIGINT, ids.getSerdeId()));
        jdbcTemplate.update(SQL.DELETE_SERDES, new SqlParameterValue(Types.BIGINT, ids.getSerdeId()));
        jdbcTemplate.update(SQL.DELETE_SDS, new SqlParameterValue(Types.BIGINT, ids.getSdsId()));
        jdbcTemplate.update(SQL.DELETE_TBLS, new SqlParameterValue(Types.BIGINT, ids.getTableId()));
    } catch (DataAccessException e) {
        throw new ConnectorException(String.format("Failed delete hive table %s", tableName), e);
    }
}
Also used : SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataAccessException(org.springframework.dao.DataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 89 with DataAccessException

use of org.springframework.dao.DataAccessException in project metacat by Netflix.

the class SequenceGeneration method newPartitionSequenceIdByName.

/**
 * Returns the current sequence ids and increments the sequence ids by the given <code>size</code>.
 *
 * @param size              number of records getting inserted
 * @param sequenceParamName the sequence Parameter Name
 * @return current sequence ids
 */
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Long newPartitionSequenceIdByName(final int size, final String sequenceParamName) {
    Long result = null;
    try {
        // Get current sequence number
        result = jdbcTemplate.queryForObject(SQL.SEQUENCE_NEXT_VAL_BYNAME, new Object[] { sequenceParamName }, Long.class);
    } catch (DataAccessException e) {
        log.warn("Failed getting the sequence ids for partition", e);
    } catch (Exception e) {
        throw new IllegalStateException("Failed retrieving the sequence numbers.");
    }
    try {
        if (result == null) {
            // init to 1L in case there's no records
            result = 1L;
            jdbcTemplate.update(SQL.SEQUENCE_INSERT_VAL, result + size, sequenceParamName);
        } else {
            jdbcTemplate.update(SQL.SEQUENCE_UPDATE_VAL, result + size, sequenceParamName);
        }
        return result;
    } catch (Exception e) {
        throw new ConnectorException("Failed updating the sequence ids for partition", e);
    }
}
Also used : ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) DataAccessException(org.springframework.dao.DataAccessException) DataAccessException(org.springframework.dao.DataAccessException) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)89 SQLException (java.sql.SQLException)40 Test (org.junit.Test)26 Connection (java.sql.Connection)17 ResultSet (java.sql.ResultSet)16 PreparedStatement (java.sql.PreparedStatement)14 MongoException (com.mongodb.MongoException)13 Document (org.bson.Document)8 TransactionStatus (org.springframework.transaction.TransactionStatus)7 HashMap (java.util.HashMap)6 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)5 IOException (java.io.IOException)4 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)4 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3