Skip to content

Commit a5ca07a

Browse files
committed
split "insertTransaction" into multiple db transactions
1 parent 69eb2fc commit a5ca07a

File tree

3 files changed

+37
-44
lines changed

3 files changed

+37
-44
lines changed

src/main/java/de/rwth/idsg/steve/repository/ReservationRepository.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import de.rwth.idsg.steve.repository.dto.InsertReservationParams;
44
import de.rwth.idsg.steve.repository.dto.Reservation;
55
import de.rwth.idsg.steve.web.dto.ReservationQueryForm;
6-
import org.jooq.DSLContext;
76
import org.jooq.Record1;
87
import org.jooq.Select;
98

@@ -32,6 +31,5 @@ public interface ReservationRepository {
3231

3332
void accepted(int reservationId);
3433
void cancelled(int reservationId);
35-
void used(DSLContext ctx, Select<Record1<Integer>> connectorPkSelect, String ocppIdTag,
36-
int reservationId, int transactionId);
34+
void used(Select<Record1<Integer>> connectorPkSelect, String ocppIdTag, int reservationId, int transactionId);
3735
}

src/main/java/de/rwth/idsg/steve/repository/impl/OcppServerRepositoryImpl.java

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -155,54 +155,50 @@ public void insertMeterValues(String chargeBoxIdentity, List<MeterValue> list, i
155155

156156
@Override
157157
public Integer insertTransaction(InsertTransactionParams p) {
158-
return ctx.transactionResult(configuration -> {
159-
DSLContext ctx = DSL.using(configuration);
160-
161-
insertIgnoreConnector(ctx, p.getChargeBoxId(), p.getConnectorId());
158+
insertIgnoreConnector(ctx, p.getChargeBoxId(), p.getConnectorId());
162159

163-
// it is important to insert idTag before transaction, since the transaction table references it
164-
boolean unknownTagInserted = insertIgnoreIdTag(ctx, p);
160+
// it is important to insert idTag before transaction, since the transaction table references it
161+
boolean unknownTagInserted = insertIgnoreIdTag(ctx, p);
165162

166-
SelectConditionStep<Record1<Integer>> connectorPkQuery =
167-
DSL.select(CONNECTOR.CONNECTOR_PK)
168-
.from(CONNECTOR)
169-
.where(CONNECTOR.CHARGE_BOX_ID.equal(p.getChargeBoxId()))
170-
.and(CONNECTOR.CONNECTOR_ID.equal(p.getConnectorId()));
171-
172-
// -------------------------------------------------------------------------
173-
// Step 1: Insert transaction
174-
// -------------------------------------------------------------------------
163+
SelectConditionStep<Record1<Integer>> connectorPkQuery =
164+
DSL.select(CONNECTOR.CONNECTOR_PK)
165+
.from(CONNECTOR)
166+
.where(CONNECTOR.CHARGE_BOX_ID.equal(p.getChargeBoxId()))
167+
.and(CONNECTOR.CONNECTOR_ID.equal(p.getConnectorId()));
175168

176-
int transactionId = ctx.insertInto(TRANSACTION)
177-
.set(CONNECTOR_STATUS.CONNECTOR_PK, connectorPkQuery)
178-
.set(TRANSACTION.ID_TAG, p.getIdTag())
179-
.set(TRANSACTION.START_TIMESTAMP, p.getStartTimestamp())
180-
.set(TRANSACTION.START_VALUE, p.getStartMeterValue())
181-
.returning(TRANSACTION.TRANSACTION_PK)
182-
.fetchOne()
183-
.getTransactionPk();
169+
// -------------------------------------------------------------------------
170+
// Step 1: Insert transaction
171+
// -------------------------------------------------------------------------
184172

185-
if (unknownTagInserted) {
186-
log.warn("The transaction '{}' contains an unknown idTag '{}' which was inserted into DB "
187-
+ "to prevent information loss and has been blocked", transactionId, p.getIdTag());
188-
}
173+
int transactionId = ctx.insertInto(TRANSACTION)
174+
.set(CONNECTOR_STATUS.CONNECTOR_PK, connectorPkQuery)
175+
.set(TRANSACTION.ID_TAG, p.getIdTag())
176+
.set(TRANSACTION.START_TIMESTAMP, p.getStartTimestamp())
177+
.set(TRANSACTION.START_VALUE, p.getStartMeterValue())
178+
.returning(TRANSACTION.TRANSACTION_PK)
179+
.fetchOne()
180+
.getTransactionPk();
181+
182+
if (unknownTagInserted) {
183+
log.warn("The transaction '{}' contains an unknown idTag '{}' which was inserted into DB "
184+
+ "to prevent information loss and has been blocked", transactionId, p.getIdTag());
185+
}
189186

190-
// -------------------------------------------------------------------------
191-
// Step 2 for OCPP 1.5: A startTransaction may be related to a reservation
192-
// -------------------------------------------------------------------------
187+
// -------------------------------------------------------------------------
188+
// Step 2 for OCPP 1.5: A startTransaction may be related to a reservation
189+
// -------------------------------------------------------------------------
193190

194-
if (p.isSetReservationId()) {
195-
reservationRepository.used(ctx, connectorPkQuery, p.getIdTag(), p.getReservationId(), transactionId);
196-
}
191+
if (p.isSetReservationId()) {
192+
reservationRepository.used(connectorPkQuery, p.getIdTag(), p.getReservationId(), transactionId);
193+
}
197194

198-
// -------------------------------------------------------------------------
199-
// Step 3: Set connector status to "Occupied"
200-
// -------------------------------------------------------------------------
195+
// -------------------------------------------------------------------------
196+
// Step 3: Set connector status to "Occupied"
197+
// -------------------------------------------------------------------------
201198

202-
insertConnectorStatus(ctx, connectorPkQuery, p.getStartTimestamp(), p.getStatusUpdate());
199+
insertConnectorStatus(ctx, connectorPkQuery, p.getStartTimestamp(), p.getStatusUpdate());
203200

204-
return transactionId;
205-
});
201+
return transactionId;
206202
}
207203

208204
@Override

src/main/java/de/rwth/idsg/steve/repository/impl/ReservationRepositoryImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ public void cancelled(int reservationId) {
143143
}
144144

145145
@Override
146-
public void used(DSLContext ctx, Select<Record1<Integer>> connectorPkSelect, String ocppIdTag,
147-
int reservationId, int transactionId) {
146+
public void used(Select<Record1<Integer>> connectorPkSelect, String ocppIdTag, int reservationId, int transactionId) {
148147
int count = ctx.update(RESERVATION)
149148
.set(RESERVATION.STATUS, ReservationStatus.USED.name())
150149
.set(RESERVATION.TRANSACTION_PK, transactionId)

0 commit comments

Comments
 (0)