Skip to content

Commit

Permalink
Removed transactionless save methods where unnecessary. Replaced tran…
Browse files Browse the repository at this point in the history
…sactionless save methods with transactional methods that require new transaction.

Changes don't affect behavior when running only one monitor, they are corrections only for running multiple monitor instances.
Removed empty Unit test class.
  • Loading branch information
Claude Muller committed Feb 1, 2019
1 parent be35cc4 commit c8e49cc
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ public Investor getInvestorByEthereumAddress(String address) throws InvestorNotF

/**
* Inserts the given {@link Investor} into the database or updates it if it already exists.
* Flushes changes to the database. The save is executed transactionless, which means that the
* changes are commited to the database immediatly.
* Flushes changes to the database. The save requires a new transaction. If the caller already has an open
* transaciton, it gets suspended and a new transaction is opened. The new transaction is commited after leaving
* this method.
* @param investor The investor to insert/update.
* @return the inserted/updated investor entry.
* @see CrudRepository#save(java.lang.Object)
*/
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public Investor saveTransactionless(Investor investor) {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Investor saveRequireNewTransaction(Investor investor) {
return investorRepository.saveAndFlush(investor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,15 @@ public PaymentLog getPaymentLog(String transactionId) throws PaymentLogNotFoundE

/**
* Inserts the given {@link PaymentLog} into the database or updates it if it already exists.
* The save is executed in a new database transaction and commited after returning from this
* method even if the calling code already runs in an active transaction.
* Flushes changes to the database. The save requires a new transaction. If the caller already has an open
* transaciton, it gets suspended and a new transaction is opened. The new transaction is commited after leaving
* this method.
* @param log The payment log to insert/update.
* @return the inserted/updated payment log.
* @see CrudRepository#save(java.lang.Object)
*/
@Transactional(propagation = Propagation.REQUIRED)
public PaymentLog saveAndCommit(PaymentLog log) {
return paymentLogRepository.saveAndFlush(log);
}

/**
* Inserts the given {@link PaymentLog} into the database or updates it if it already exists.
* Flushes changes to the database. The save is executed transactionless, which means that the
* changes are commited to the database immediatly.
* @param log The payment log to insert/update.
* @return the inserted/updated payment log.
* @see CrudRepository#save(java.lang.Object)
*/
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public PaymentLog saveTransactionless(PaymentLog log) {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public PaymentLog saveRequireNewTransaction(PaymentLog log) {
return paymentLogRepository.saveAndFlush(log);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public Optional<SaleTier> getTierAtDate(Date date) {

/**
* Inserts the given {@link SaleTier} into the database or updates it if it already exists.
* Flushes changes to the database. The save is executed transactionless, which means that the
* changes are commited to the database immediatly.
* Flushes changes to the database. The save requires a transaction. So if the caller does not have an
* open transaction, a new transaction is created and commited after this method. Otherwise the caller's transaction
* is used and no commit happens directly after this method.
* @param tier The sale tier to insert/update.
* @return the inserted/updated sale tier.
* @see CrudRepository#save(java.lang.Object)
*/
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public SaleTier saveTransactionless(SaleTier tier) {
@Transactional(propagation = Propagation.REQUIRED)
public SaleTier saveRequireTransaction(SaleTier tier) {
return saleTierRepository.saveAndFlush(tier);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ResponseEntity<List<SaleTierResponse>> createTiers(
@RequestBody List<SaleTierRequest> tiersRequest) {

List<SaleTierResponse> tiersResponse = tiersRequest.stream().map((request) -> {
return saleTierService.saveTransactionless(fromRequestToEntity(request));
return saleTierService.saveRequireTransaction(fromRequestToEntity(request));
}).map((savedEntity) -> fromEntityToResponse(savedEntity)).collect(Collectors.toList());

return ResponseEntity.ok(tiersResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void createTiers_With_Right_Credential() throws Exception {
List<SaleTierRequest> listRequest = Arrays.asList(req1);
SaleTier saleTierEntityRequest = tierController.fromRequestToEntity(req1);

when(saleTierService.saveTransactionless(any())).thenReturn(saleTierEntityRequest);
when(saleTierService.saveRequireTransaction(any())).thenReturn(saleTierEntityRequest);

MvcResult result = this.mockMvc.perform(post(TIERS_CREATE_ENDPOINT)
.with(httpBasic("user", "password"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ private boolean isAmountInsufficient(BigDecimal usdAmount) {
return usdAmount.compareTo(configHolder.getFiatBasePaymentMinimum()) < 0;
}

/**
* Updates the given payment log (in status {@link PaymentLog.TransactionStatus#BUILDING}) according to the given
* transaction. If all neccessary information can be retrieved, including the exchange rate, the payment log is
* updated and the changes are immediatly commited. If some information cannot be retrieved a refund entry is
* created (also immediatly commited) and the payment log is not updated.
* @param tx The transaction corresponding to the payment log.
* @param paymentLog The payment log to update.
* @return the update payment log or null if some transaction information could not be retrieved and a refund entry
* had to be created.
* @throws RefundEntryAlreadyExistsException if a refund entry already exists for this payment log. A refund entry
* is create if some transaction information cannot be retrieved.
*/
private PaymentLog updateBuildingPaymentLog(TransactionAdapter tx, PaymentLog paymentLog) throws RefundEntryAlreadyExistsException {
if (paymentLog == null) return null;
RefundReason reason = null;
Expand All @@ -163,7 +175,7 @@ private PaymentLog updateBuildingPaymentLog(TransactionAdapter tx, PaymentLog pa
monitorService.createRefundEntryForPaymentLogAndCommit(paymentLog, reason);
return null;
}
return paymentLogService.saveAndCommit(paymentLog);
return paymentLogService.saveRequireNewTransaction(paymentLog);
}

private BigDecimal getUSDExchangeRate(Instant blockTimestamp, CurrencyType currencyType)
Expand Down Expand Up @@ -216,7 +228,7 @@ protected void confirmTransaction(TransactionAdapter tx) {
LOG.info("Setting status of transaction {} to confirmed.", tx.getTransactionId());
PaymentLog paymentLog = paymentLogService.getPaymentLog(tx.getTransactionId());
paymentLog.setTransactionStatus(TransactionStatus.CONFIRMED);
paymentLogService.saveAndCommit(paymentLog);
paymentLogService.saveRequireNewTransaction(paymentLog);
} catch (MissingTransactionInformationException e) {
LOG.error("Couldn't set payment log status to confirmed because the transaction id " +
"could not be retrieved from the {} transaction.", tx.getCurrencyType(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void createDevelopmentTiers() throws Exception {
true,
true
);
saleTierService.saveTransactionless(s1);
saleTierService.saveTransactionless(s2);
saleTierService.saveRequireTransaction(s1);
saleTierService.saveRequireTransaction(s2);
LOG.info("Development tiers successfully set-up.");
LOG.info("Tier {}: startDate={} endDate={}", s1.getTierNo(), startDateS1, endDateS1);
LOG.info("Tier {}: startDate={} endDate={}", s2.getTierNo(), startDateS2, endDateS2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private boolean matchReceivedMessage(List<TokensAllocatedEmailMessage> messages,
}

private Investor createAndSaveInvestor(ECKey key) {
return investorService.saveTransactionless(buildInvestor(key));
return investorService.saveRequireNewTransaction(buildInvestor(key));
}

private Investor buildInvestor(ECKey key) {
Expand All @@ -189,7 +189,7 @@ private void createAndSaveTier() {
BigInteger tomics = monitorService.convertTokensToTomics(new BigDecimal(1000L))
.toBigInteger();

saleTierService.saveTransactionless(
saleTierService.saveRequireTransaction(
new SaleTier(4, "4", from, to, new BigDecimal("0.0"),
BigInteger.ZERO, tomics, true, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ private Investor createInvestor() {
try {
return investorService.getInvestorByEmail(INVESTOR_EMAIL);
} catch (InvestorNotFoundException e) {
return investorService.saveTransactionless(
return investorService.saveRequireNewTransaction(
new Investor(new java.util.Date(), INVESTOR_EMAIL, "token",
"walletAddress", "payInEtherPublicKey", "payInBitcoinPublicKey",
"refundEtherAddress", "refundBitcoinAddress", "ipAddress"));
Expand All @@ -653,7 +653,7 @@ private PaymentLog createPaymentLog(BigDecimal usdAmount, Date blockTime, Invest
int i = 1;
while (!succeeded) {
try {
paymentLog = paymentLogService.saveTransactionless(
paymentLog = paymentLogService.saveRequireNewTransaction(
new PaymentLog(txId, creationDate, currency, blockTime,
weiAmount, USD_FX_RATE, usdAmount, investor,
tomicsAmount, TransactionStatus.BUILDING));
Expand Down Expand Up @@ -746,7 +746,7 @@ private SaleTier createAndSaveSaleTier(int tierNo, Date startDate, Date endDate,
tomicsMax,
hasDynamicDuration,
hasDynamicMax);
return saleTierService.saveTransactionless(t);
return saleTierService.saveRequireTransaction(t);
}

public void newStartDateMustBe(Date date) {
Expand Down

0 comments on commit c8e49cc

Please sign in to comment.