CORDA-1865 Fix race on inserting attachments (#3734)

* CORDA-1865 Fix race on inserting attachments

* CORDA-1865 Fix race on inserting attachments
This commit is contained in:
Tudor Malene 2018-08-01 18:11:42 +01:00 committed by GitHub
parent 2e03c7f8c0
commit 6499a13951
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,7 @@ import net.corda.core.transactions.SignedTransaction
import net.corda.core.utilities.NonEmptySet
import net.corda.core.utilities.UntrustworthyData
import net.corda.core.utilities.unwrap
import java.nio.file.FileAlreadyExistsException
import java.util.*
/**
@ -149,9 +150,15 @@ class FetchAttachmentsFlow(requests: Set<SecureHash>,
for (attachment in downloaded) {
with(serviceHub.attachments) {
if (!hasAttachment(attachment.id)) {
importAttachment(attachment.open(), "$P2P_UPLOADER:${otherSideSession.counterparty.name}", null)
try {
importAttachment(attachment.open(), "$P2P_UPLOADER:${otherSideSession.counterparty.name}", null)
} catch (e: FileAlreadyExistsException) {
// This can happen when another transaction will insert the same attachment during this transaction.
// The outcome is the same (the attachment is imported), so we can ignore this exception.
logger.debug("Attachment ${attachment.id} already inserted.")
}
} else {
logger.info("Attachment ${attachment.id} already exists, skipping.")
logger.debug("Attachment ${attachment.id} already exists, skipping.")
}
}
}