Search in sources :

Example 1 with Transaction

use of com.revolsys.transaction.Transaction in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method writeAll.

protected int writeAll(final Iterable<? extends Record> records, final RecordState state) {
    int count = 0;
    try (Transaction transaction = newTransaction(Propagation.REQUIRED)) {
        // rolled back.
        try (final RecordWriter writer = newRecordWriter(true)) {
            for (final Record record : records) {
                write(writer, record, state);
                count++;
            }
        } catch (final RuntimeException e) {
            transaction.setRollbackOnly();
            throw e;
        } catch (final Error e) {
            transaction.setRollbackOnly();
            throw e;
        }
    }
    return count;
}
Also used : RecordWriter(com.revolsys.record.io.RecordWriter) Transaction(com.revolsys.transaction.Transaction) ArrayRecord(com.revolsys.record.ArrayRecord) Record(com.revolsys.record.Record)

Example 2 with Transaction

use of com.revolsys.transaction.Transaction in project com.revolsys.open by revolsys.

the class AbstractJdbcRecordStore method deleteRecords.

@Override
public int deleteRecords(final Query query) {
    final String typeName = query.getTypeName();
    RecordDefinition recordDefinition = query.getRecordDefinition();
    if (recordDefinition == null) {
        if (typeName != null) {
            recordDefinition = getRecordDefinition(typeName);
            query.setRecordDefinition(recordDefinition);
        }
    }
    final String sql = JdbcUtils.getDeleteSql(query);
    try (Transaction transaction = newTransaction(com.revolsys.transaction.Propagation.REQUIRED)) {
        // rolled back.
        try (JdbcConnection connection = getJdbcConnection(isAutoCommit());
            final PreparedStatement statement = connection.prepareStatement(sql)) {
            JdbcUtils.setPreparedStatementParameters(statement, query);
            return statement.executeUpdate();
        } catch (final SQLException e) {
            transaction.setRollbackOnly();
            throw new RuntimeException("Unable to delete : " + sql, e);
        } catch (final RuntimeException e) {
            transaction.setRollbackOnly();
            throw e;
        } catch (final Error e) {
            transaction.setRollbackOnly();
            throw e;
        }
    }
}
Also used : Transaction(com.revolsys.transaction.Transaction) SQLException(java.sql.SQLException) JdbcConnection(com.revolsys.jdbc.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 3 with Transaction

use of com.revolsys.transaction.Transaction in project com.revolsys.open by revolsys.

the class GeometryTest method doWriteTest.

private void doWriteTest(final DataType geometryType, final int axisCount, final int partCount, final int ringCount) {
    final GeometryFactory sourceGeometryFactory = GeometryFactory.floating(3857, axisCount);
    String typeName = geometryType.getName().toUpperCase();
    typeName = typeName.replace("MULTI", "MULTI_");
    final PathName typePath = PathName.newPathName("/ORACLE_TEST/" + typeName + axisCount);
    Geometry geometry = GeometryTestUtil.geometry(sourceGeometryFactory, geometryType, axisCount, partCount, ringCount);
    if (geometry instanceof Polygonal) {
        geometry = geometry.toCounterClockwise();
    }
    try (Transaction transaction = this.recordStore.newTransaction(Propagation.REQUIRES_NEW)) {
        final RecordDefinition recordDefinition = this.recordStore.getRecordDefinition(typePath);
        final Record record = this.recordStore.newRecord(typePath, Collections.singletonMap("GEOMETRY", geometry));
        try (Writer<Record> writer = this.recordStore.newRecordWriter()) {
            writer.write(record);
        }
        final Identifier identifier = record.getIdentifier();
        final Record savedRecord = this.recordStore.getRecord(typePath, identifier);
        Assert.assertNotNull("Saved record", savedRecord);
        final Geometry savedGeometry = savedRecord.getGeometry();
        final GeometryFactory tableGeometryFactory = recordDefinition.getGeometryFactory();
        final Geometry expectedGeometry = geometry.convertGeometry(tableGeometryFactory);
        com.revolsys.geometry.util.Assert.equals("Saved geometry", savedGeometry.equalsExact(expectedGeometry), expectedGeometry, savedGeometry);
        transaction.setRollbackOnly();
    }
}
Also used : Geometry(com.revolsys.geometry.model.Geometry) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) Identifier(com.revolsys.identifier.Identifier) Transaction(com.revolsys.transaction.Transaction) Polygonal(com.revolsys.geometry.model.Polygonal) Record(com.revolsys.record.Record) PathName(com.revolsys.io.PathName) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 4 with Transaction

use of com.revolsys.transaction.Transaction in project com.revolsys.open by revolsys.

the class RecordStoreLayer method forEachRecordInternal.

@Override
protected void forEachRecordInternal(Query query, final Consumer<? super LayerRecord> consumer) {
    if (isExists()) {
        try {
            final RecordStore recordStore = getRecordStore();
            if (recordStore != null && query != null) {
                final Predicate<Record> filter = query.getWhereCondition();
                final Map<? extends CharSequence, Boolean> orderBy = query.getOrderBy();
                final List<LayerRecord> changedRecords = new ArrayList<>();
                changedRecords.addAll(getRecordsNew());
                changedRecords.addAll(getRecordsModified());
                Records.filterAndSort(changedRecords, filter, orderBy);
                final Iterator<LayerRecord> changedIterator = changedRecords.iterator();
                LayerRecord currentChangedRecord = Iterators.next(changedIterator);
                final RecordDefinition internalRecordDefinition = getInternalRecordDefinition();
                query = query.newQuery(internalRecordDefinition);
                final Comparator<Record> comparator = Records.newComparatorOrderBy(orderBy);
                try (final BaseCloseable booleanValueCloseable = eventsDisabled();
                    Transaction transaction = recordStore.newTransaction(Propagation.REQUIRES_NEW);
                    final RecordReader reader = newRecordStoreRecordReader(query)) {
                    for (LayerRecord record : reader.<LayerRecord>i()) {
                        boolean write = true;
                        final Identifier identifier = getId(record);
                        if (identifier == null) {
                            record = newProxyLayerRecordNoId(record);
                        } else {
                            final LayerRecord cachedRecord = this.recordsByIdentifier.get(identifier);
                            if (cachedRecord != null) {
                                record = cachedRecord;
                                if (record.isChanged() || isDeleted(record)) {
                                    write = false;
                                }
                            }
                        }
                        if (!isDeleted(record) && write) {
                            while (currentChangedRecord != null && comparator.compare(currentChangedRecord, record) < 0) {
                                consumer.accept(currentChangedRecord);
                                currentChangedRecord = Iterators.next(changedIterator);
                            }
                            consumer.accept(record);
                        }
                    }
                    while (currentChangedRecord != null) {
                        consumer.accept(currentChangedRecord);
                        currentChangedRecord = Iterators.next(changedIterator);
                    }
                }
            }
        } catch (final CancellationException e) {
        } catch (final RuntimeException e) {
            Logs.error(this, "Error executing query: " + query, e);
            throw e;
        }
    }
}
Also used : BaseCloseable(com.revolsys.io.BaseCloseable) RecordReader(com.revolsys.record.io.RecordReader) ArrayList(java.util.ArrayList) RecordDefinition(com.revolsys.record.schema.RecordDefinition) Identifier(com.revolsys.identifier.Identifier) Transaction(com.revolsys.transaction.Transaction) CancellationException(java.util.concurrent.CancellationException) RecordStore(com.revolsys.record.schema.RecordStore) Record(com.revolsys.record.Record)

Example 5 with Transaction

use of com.revolsys.transaction.Transaction in project com.revolsys.open by revolsys.

the class RecordStoreLayer method saveChangesDo.

@Override
protected boolean saveChangesDo(final RecordLayerErrors errors, final LayerRecord record) {
    boolean deleted = super.isDeleted(record);
    if (isExists()) {
        final RecordStore recordStore = getRecordStore();
        if (recordStore == null) {
            return true;
        } else {
            try (Transaction transaction = recordStore.newTransaction(Propagation.REQUIRES_NEW)) {
                try {
                    Identifier identifier = record.getIdentifier();
                    try (final Writer<Record> writer = recordStore.newRecordWriter()) {
                        if (isRecordCached(getCacheIdDeleted(), record) || super.isDeleted(record)) {
                            preDeleteRecord(record);
                            record.setState(RecordState.DELETED);
                            writeDelete(writer, record);
                            deleted = true;
                        } else {
                            final RecordDefinition recordDefinition = getRecordDefinition();
                            if (super.isNew(record)) {
                                final List<String> idFieldNames = recordDefinition.getIdFieldNames();
                                if (identifier == null && !idFieldNames.isEmpty()) {
                                    identifier = recordStore.newPrimaryIdentifier(this.typePath);
                                    if (identifier != null) {
                                        identifier.setIdentifier(record, idFieldNames);
                                    }
                                }
                            }
                            final int fieldCount = recordDefinition.getFieldCount();
                            for (int fieldIndex = 0; fieldIndex < fieldCount; fieldIndex++) {
                                record.validateField(fieldIndex);
                            }
                            if (super.isModified(record)) {
                                writeUpdate(writer, record);
                            } else if (super.isNew(record)) {
                                writer.write(record);
                            }
                        }
                    }
                    if (!deleted) {
                        record.setState(RecordState.PERSISTED);
                    }
                    removeFromRecordIdToRecordMap(identifier);
                    return true;
                } catch (final Throwable e) {
                    throw transaction.setRollbackOnly(e);
                }
            }
        }
    } else {
        return true;
    }
}
Also used : Identifier(com.revolsys.identifier.Identifier) Transaction(com.revolsys.transaction.Transaction) RecordStore(com.revolsys.record.schema.RecordStore) Record(com.revolsys.record.Record) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Aggregations

Transaction (com.revolsys.transaction.Transaction)8 Identifier (com.revolsys.identifier.Identifier)5 Record (com.revolsys.record.Record)5 RecordDefinition (com.revolsys.record.schema.RecordDefinition)5 RecordReader (com.revolsys.record.io.RecordReader)4 RecordStore (com.revolsys.record.schema.RecordStore)4 BaseCloseable (com.revolsys.io.BaseCloseable)2 Geometry (com.revolsys.geometry.model.Geometry)1 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)1 Polygonal (com.revolsys.geometry.model.Polygonal)1 PathName (com.revolsys.io.PathName)1 JdbcConnection (com.revolsys.jdbc.JdbcConnection)1 ArrayRecord (com.revolsys.record.ArrayRecord)1 RecordWriter (com.revolsys.record.io.RecordWriter)1 Condition (com.revolsys.record.query.Condition)1 Query (com.revolsys.record.query.Query)1 LabelCountMap (com.revolsys.util.count.LabelCountMap)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1