Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better transaction handling #1268

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package no.nav.klage.dokument.service

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import jakarta.transaction.Transactional
import no.nav.klage.dokument.api.mapper.DokumentMapper
import no.nav.klage.dokument.api.view.*
import no.nav.klage.dokument.clients.kabalsmarteditorapi.model.request.CommentInput
Expand Down Expand Up @@ -31,6 +30,7 @@ import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.ourJacksonObjectMapper
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
import java.util.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.klage.oppgave.clients.gosysoppgave
import no.nav.klage.kodeverk.Tema
import no.nav.klage.oppgave.clients.gosysoppgave.OppgaveMapperResponse.OppgaveMappe
import no.nav.klage.oppgave.config.CacheWithJCacheConfiguration
import no.nav.klage.oppgave.exceptions.GosysOppgaveClientException
import no.nav.klage.oppgave.util.TokenUtil
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
Expand Down Expand Up @@ -40,7 +41,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<GosysOppgaveRecord>()
.block() ?: throw OppgaveClientException("Oppgave could not be fetched")
.block() ?: throw RuntimeException("Oppgave could not be fetched")
}
}

Expand All @@ -62,7 +63,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<GosysOppgaveRecord>()
.block() ?: throw OppgaveClientException("Oppgave could not be updated")
.block() ?: throw RuntimeException("Oppgave could not be updated")
}
}

Expand All @@ -87,7 +88,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<OppgaveMapperResponse>()
.block() ?: throw OppgaveClientException("Could not get mapper for enhet")
.block() ?: throw RuntimeException("Could not get mapper for enhet")
}
}

Expand All @@ -110,7 +111,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<OppgaveMappe>()
.block() ?: throw OppgaveClientException("Could not get mapper for enhet")
.block() ?: throw RuntimeException("Could not get mapper for enhet")
}
}

Expand All @@ -136,7 +137,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<OppgaveListResponse>()
.block() ?: throw OppgaveClientException("Oppgaver could not be fetched")
.block() ?: throw RuntimeException("Oppgaver could not be fetched")
}

return oppgaveResponse.oppgaver
Expand All @@ -155,10 +156,10 @@ class GosysOppgaveClient(
ex.request?.uri ?: "-",
ex.responseBodyAsString
)
throw OppgaveClientException("Caught WebClientResponseException", ex)
throw GosysOppgaveClientException("Caught WebClientResponseException", ex)
} catch (rtex: RuntimeException) {
logger.warn("Caught RuntimeException", rtex)
throw OppgaveClientException("Caught runtimeexception", rtex)
throw GosysOppgaveClientException("Caught runtimeexception", rtex)
} finally {
val end: Long = System.currentTimeMillis()
logger.info("Method {} took {} millis", methodName, (end - start))
Expand All @@ -181,7 +182,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<List<Gjelder>>()
.block() ?: throw OppgaveClientException("Could not fetch gjelder kodeverk for tema ${tema.navn}")
.block() ?: throw RuntimeException("Could not fetch gjelder kodeverk for tema ${tema.navn}")
}

return gjelderResponse
Expand All @@ -203,7 +204,7 @@ class GosysOppgaveClient(
.header("Nav-Consumer-Id", applicationName)
.retrieve()
.bodyToMono<List<OppgavetypeResponse>>()
.block() ?: throw OppgaveClientException("Could not fetch oppgavetype kodeverk for tema ${tema.navn}")
.block() ?: throw RuntimeException("Could not fetch oppgavetype kodeverk for tema ${tema.navn}")
}

return oppgavetypeResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ enum class Status(val statusId: Long) {
}
}

class OppgaveClientException : Exception {
constructor(message: String?) : super(message)

constructor(message: String?, cause: Throwable?) : super(message, cause)
}

data class OppgaveMapperResponse(
val antallTreffTotalt: Int,
val mapper: List<OppgaveMappe>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import no.nav.klage.oppgave.util.getSecureLogger
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.event.EventListener
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.event.TransactionPhase
import org.springframework.transaction.event.TransactionalEventListener
import java.time.LocalDateTime
import java.util.*

Expand Down Expand Up @@ -50,6 +53,8 @@ class CleanupAfterBehandlingEventListener(
}

@EventListener
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun cleanupAfterBehandling(behandlingEndretEvent: BehandlingEndretEvent) {
val behandling = behandlingEndretEvent.behandling

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class SendBehandlingEndretToKafkaEventListener(
val objectMapper: ObjectMapper = ourJacksonObjectMapper()
}

/* This code needs a transaction b/c of lazy loading */
@EventListener
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.nav.klage.oppgave.eventlisteners

import no.nav.klage.oppgave.domain.events.BehandlingEndretEvent
import no.nav.klage.oppgave.service.StatistikkTilDVHService
import no.nav.klage.oppgave.util.getLogger
import org.springframework.context.event.EventListener
import org.springframework.stereotype.Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.klage.oppgave.eventlisteners
import com.fasterxml.jackson.databind.ObjectMapper
import no.nav.klage.oppgave.domain.events.BehandlingEndretEvent
import no.nav.klage.oppgave.domain.klage.Felt
import no.nav.klage.oppgave.repositories.BehandlingRepository
import no.nav.klage.oppgave.service.GosysOppgaveService
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.ourJacksonObjectMapper
Expand All @@ -17,7 +16,6 @@ import org.springframework.transaction.event.TransactionalEventListener

@Service
class UpdateGosysOppgaveEventListener(
private val behandlingRepository: BehandlingRepository,
private val gosysOppgaveService: GosysOppgaveService,
@Value("\${SYSTEMBRUKER_IDENT}") private val systembrukerIdent: String,
) {
Expand All @@ -28,7 +26,6 @@ class UpdateGosysOppgaveEventListener(
val objectMapper: ObjectMapper = ourJacksonObjectMapper()
}

/* This code needs a transaction b/c of lazy loading */
@EventListener
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ class EREGOrganizationNotFoundException(msg: String) : RuntimeException(msg)

class KodeverkNotFoundException(msg: String) : RuntimeException(msg)

class AttachmentCouldNotBeConvertedException(override val message: String = "FILE_COULD_NOT_BE_CONVERTED") : RuntimeException()
class AttachmentCouldNotBeConvertedException(override val message: String = "FILE_COULD_NOT_BE_CONVERTED") : RuntimeException()

class GosysOppgaveClientException : RuntimeException {
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import no.nav.klage.oppgave.domain.kafka.EventType
import no.nav.klage.oppgave.domain.kafka.StatistikkTilDVH
import no.nav.klage.oppgave.domain.kafka.UtsendingStatus
import no.nav.klage.oppgave.domain.klage.*
import no.nav.klage.oppgave.eventlisteners.StatistikkTilDVHService.Companion.TR_ENHET
import no.nav.klage.oppgave.repositories.*
import no.nav.klage.oppgave.service.StatistikkTilDVHService.Companion.TR_ENHET
import no.nav.klage.oppgave.util.*
import org.slf4j.Logger
import org.springframework.beans.factory.annotation.Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.klage.oppgave.service
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import jakarta.transaction.Transactional
import no.nav.klage.dokument.api.view.JournalfoertDokumentReference
import no.nav.klage.kodeverk.hjemmel.Hjemmel
import no.nav.klage.kodeverk.hjemmel.ytelseTilRegistreringshjemlerV2
Expand All @@ -21,6 +20,7 @@ import no.nav.klage.oppgave.util.getLogger
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package no.nav.klage.oppgave.service

import jakarta.transaction.Transactional
import no.nav.klage.dokument.api.view.JournalfoertDokumentReference
import no.nav.klage.kodeverk.Type
import no.nav.klage.oppgave.clients.kaka.KakaApiGateway
import no.nav.klage.oppgave.domain.events.BehandlingEndretEvent
import no.nav.klage.oppgave.domain.klage.*
import no.nav.klage.oppgave.repositories.AnkebehandlingRepository
import no.nav.klage.oppgave.repositories.BehandlingEtterTrygderettenOpphevetRepository
import no.nav.klage.oppgave.repositories.BehandlingRepository
import no.nav.klage.oppgave.util.getLogger
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
import java.time.Period
import java.util.*
Expand All @@ -21,14 +20,11 @@ import java.util.*
@Transactional
class AnkebehandlingService(
private val ankebehandlingRepository: AnkebehandlingRepository,
private val behandlingEtterTrygderettenOpphevetRepository: BehandlingEtterTrygderettenOpphevetRepository,
private val behandlingRepository: BehandlingRepository,
private val kakaApiGateway: KakaApiGateway,
private val dokumentService: DokumentService,
private val behandlingService: BehandlingService,
private val applicationEventPublisher: ApplicationEventPublisher,
@Value("#{T(java.time.LocalDate).parse('\${KAKA_VERSION_2_DATE}')}")
private val kakaVersion2Date: LocalDate,
@Value("\${SYSTEMBRUKER_IDENT}") private val systembrukerIdent: String,
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package no.nav.klage.oppgave.service
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import jakarta.transaction.Transactional
import no.nav.klage.dokument.api.view.JournalfoertDokumentReference
import no.nav.klage.kodeverk.Type
import no.nav.klage.oppgave.clients.kaka.KakaApiGateway
Expand All @@ -17,6 +16,7 @@ import no.nav.klage.oppgave.util.getLogger
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.ApplicationEventPublisher
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
import java.time.LocalDateTime

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2340,7 +2340,7 @@ class BehandlingService(
return if (behandlingList.isEmpty()) {
null
} else if (behandlingList.size != 1) {
throw Exception("Found more than one behandling for gosysOppgaveId $gosysOppgaveId, investigate")
throw RuntimeException("Found more than one behandling for gosysOppgaveId $gosysOppgaveId, investigate")
} else {
behandlingList.first().id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import no.nav.klage.oppgave.domain.kafka.GosysoppgaveEvent
import no.nav.klage.oppgave.domain.kafka.InternalBehandlingEvent
import no.nav.klage.oppgave.domain.kafka.InternalEventType
import no.nav.klage.oppgave.domain.klage.Behandling
import no.nav.klage.oppgave.exceptions.GosysOppgaveClientException
import no.nav.klage.oppgave.exceptions.GosysOppgaveNotEditableException
import no.nav.klage.oppgave.exceptions.IllegalOperation
import no.nav.klage.oppgave.util.getLogger
Expand Down Expand Up @@ -282,7 +283,7 @@ class GosysOppgaveService(
val mappeResponse = gosysOppgaveClient.getMappe(id = id, systemContext = systemContext)

if (mappeResponse.id == null) {
throw OppgaveClientException("Mappe did not contain id")
throw GosysOppgaveClientException("Mappe did not contain id")
}

return GosysOppgaveMappeView(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.klage.oppgave.eventlisteners
package no.nav.klage.oppgave.service

import no.nav.klage.kodeverk.Fagsystem
import no.nav.klage.kodeverk.PartIdType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import no.nav.klage.kodeverk.Ytelse
import no.nav.klage.oppgave.domain.events.BehandlingEndretEvent
import no.nav.klage.oppgave.domain.klage.*
import no.nav.klage.oppgave.repositories.KafkaEventRepository
import no.nav.klage.oppgave.service.StatistikkTilDVHService
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
Expand Down