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;
}
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;
}
}
}
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();
}
}
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;
}
}
}
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;
}
}
Aggregations