Skip to content
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 @@ -28,13 +28,14 @@ import com.github.jasync.sql.db.mysql.util.CapabilityFlag
import com.github.jasync.sql.db.mysql.util.CharsetMapper
import com.github.jasync.sql.db.pool.TimeoutScheduler
import com.github.jasync.sql.db.pool.TimeoutSchedulerImpl
import com.github.jasync.sql.db.util.FP
import com.github.jasync.sql.db.util.Failure
import com.github.jasync.sql.db.util.NettyUtils
import com.github.jasync.sql.db.util.Success
import com.github.jasync.sql.db.util.Version
import com.github.jasync.sql.db.util.complete
import com.github.jasync.sql.db.util.failed
import com.github.jasync.sql.db.util.flatMap
import com.github.jasync.sql.db.util.flatMapTry
import com.github.jasync.sql.db.util.isCompleted
import com.github.jasync.sql.db.util.length
import com.github.jasync.sql.db.util.mapTry
Expand All @@ -50,6 +51,7 @@ import java.time.Duration
import java.util.Optional
import java.util.concurrent.CancellationException
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionException
import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.atomic.AtomicReference

Expand Down Expand Up @@ -393,17 +395,17 @@ class MySQLConnection @JvmOverloads constructor(
}

fun sendQueryAfterCurrent(query: String): CompletableFuture<QueryResult> {
return if (isQuerying()) {
logger.info { "attaching after current query $query" }
queryPromise().get().flatMap { sendQuery(query) }
} else {
sendQuery(query)
}.mapTry { queryResult, throwable ->
// Cancellation exception will be ignored so that the following transaction clean up can be executed
if (throwable is CancellationException) {
QueryResult(rowsAffected = 0L, statusMessage = null)
val currentQuery = queryPromise()
if (!currentQuery.isPresent) {
return sendQuery(query)
}
logger.info { "attaching after current query $query" }
return currentQuery.get().flatMapTry { _, throwable ->
val cause = if (throwable is CompletionException) throwable.cause ?: throwable else throwable
if (cause is CancellationException) {
FP.successful(QueryResult(rowsAffected = 0L, statusMessage = null))
} else {
queryResult
sendQuery(query)
}
}
}
Expand Down
206 changes: 106 additions & 100 deletions r2dbc-mysql/src/main/java/JasyncStatement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import reactor.kotlin.core.publisher.onErrorMap
import reactor.kotlin.core.publisher.toFlux
import reactor.kotlin.core.publisher.toMono
import java.io.IOException
import java.util.concurrent.CompletionException
import java.util.concurrent.TimeoutException
import java.util.function.Supplier
import com.github.jasync.sql.db.Connection as JasyncConnection

Expand Down Expand Up @@ -131,133 +133,137 @@ internal class JasyncStatement(private val clientSupplier: Supplier<JasyncConnec
}
}

private fun mapException(throwable: Throwable) = when (throwable) {
is ConnectionTimeoutedException -> R2dbcTimeoutException(throwable)
is IllegalArgumentException -> throwable
is IllegalStateException -> throwable
is UnsupportedOperationException -> throwable
is IOException -> throwable
is MySQLException -> {
val errorMessage = throwable.errorMessage
when (errorMessage.errorCode) {
MysqlErrors.ER_DBACCESS_DENIED_ERROR,
MysqlErrors.ER_ACCESS_DENIED_ERROR,
MysqlErrors.ER_KILL_DENIED_ERROR,
MysqlErrors.ER_TABLEACCESS_DENIED_ERROR,
MysqlErrors.ER_COLUMNACCESS_DENIED_ERROR,
MysqlErrors.ER_SPECIFIC_ACCESS_DENIED_ERROR,
MysqlErrors.ER_PROCACCESS_DENIED_ERROR,
MysqlErrors.ER_ACCESS_DENIED_NO_PASSWORD_ERROR,
MysqlErrors.ER_ACCESS_DENIED_CHANGE_USER_ERROR -> R2dbcPermissionDeniedException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
)
MysqlErrors.ER_DUP_KEY,
MysqlErrors.ER_BAD_NULL_ERROR,
MysqlErrors.ER_DUP_ENTRY,
MysqlErrors.ER_DUP_UNIQUE,
MysqlErrors.ER_CANNOT_ADD_FOREIGN,
MysqlErrors.ER_NO_REFERENCED_ROW,
MysqlErrors.ER_ROW_IS_REFERENCED,
MysqlErrors.ER_NO_DEFAULT_FOR_FIELD,
MysqlErrors.ER_ROW_IS_REFERENCED_2,
MysqlErrors.ER_NO_REFERENCED_ROW_2,
MysqlErrors.ER_FOREIGN_DUPLICATE_KEY,
MysqlErrors.ER_DUP_UNKNOWN_IN_INDEX
-> R2dbcDataIntegrityViolationException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
)
MysqlErrors.ER_TABLE_EXISTS_ERROR,
MysqlErrors.ER_BAD_TABLE_ERROR,
MysqlErrors.ER_BAD_FIELD_ERROR,
MysqlErrors.ER_PARSE_ERROR,
MysqlErrors.ER_ILLEGAL_REFERENCE,
MysqlErrors.ER_NO_SUCH_TABLE,
MysqlErrors.ER_SP_ALREADY_EXISTS,
MysqlErrors.ER_SP_DOES_NOT_EXIST,
MysqlErrors.ER_FUNC_INEXISTENT_NAME_COLLISION
-> R2dbcBadGrammarException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
sql,
throwable
)
MysqlErrors.ER_LOCK_WAIT_TIMEOUT,
MysqlErrors.ER_QUERY_TIMEOUT,
3024 -> R2dbcTimeoutException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
)
MysqlErrors.ER_XA_RBROLLBACK,
MysqlErrors.ER_XA_RBTIMEOUT -> R2dbcRollbackException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
)
MysqlErrors.ER_NET_READ_INTERRUPTED,
MysqlErrors.ER_NET_WRITE_INTERRUPTED,
MysqlErrors.ER_LOCK_DEADLOCK,
MysqlErrors.ER_QUERY_INTERRUPTED -> R2dbcTransientResourceException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
)
else -> when (errorMessage.sqlState.take(2)) {
"0A", "22", "26", "2F", "20", "42", "XA" -> R2dbcBadGrammarException(
internal fun mapException(throwable: Throwable): Throwable {
val unwrapped = if (throwable is CompletionException) throwable.cause ?: throwable else throwable
return when (unwrapped) {
is ConnectionTimeoutedException,
is TimeoutException -> R2dbcTimeoutException(unwrapped.message, unwrapped)
is IllegalArgumentException -> unwrapped
is IllegalStateException -> unwrapped
is UnsupportedOperationException -> unwrapped
is IOException -> unwrapped
is MySQLException -> {
val errorMessage = unwrapped.errorMessage
when (errorMessage.errorCode) {
MysqlErrors.ER_DBACCESS_DENIED_ERROR,
MysqlErrors.ER_ACCESS_DENIED_ERROR,
MysqlErrors.ER_KILL_DENIED_ERROR,
MysqlErrors.ER_TABLEACCESS_DENIED_ERROR,
MysqlErrors.ER_COLUMNACCESS_DENIED_ERROR,
MysqlErrors.ER_SPECIFIC_ACCESS_DENIED_ERROR,
MysqlErrors.ER_PROCACCESS_DENIED_ERROR,
MysqlErrors.ER_ACCESS_DENIED_NO_PASSWORD_ERROR,
MysqlErrors.ER_ACCESS_DENIED_CHANGE_USER_ERROR -> R2dbcPermissionDeniedException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
unwrapped
)
"25", "28" -> R2dbcPermissionDeniedException(
MysqlErrors.ER_DUP_KEY,
MysqlErrors.ER_BAD_NULL_ERROR,
MysqlErrors.ER_DUP_ENTRY,
MysqlErrors.ER_DUP_UNIQUE,
MysqlErrors.ER_CANNOT_ADD_FOREIGN,
MysqlErrors.ER_NO_REFERENCED_ROW,
MysqlErrors.ER_ROW_IS_REFERENCED,
MysqlErrors.ER_NO_DEFAULT_FOR_FIELD,
MysqlErrors.ER_ROW_IS_REFERENCED_2,
MysqlErrors.ER_NO_REFERENCED_ROW_2,
MysqlErrors.ER_FOREIGN_DUPLICATE_KEY,
MysqlErrors.ER_DUP_UNKNOWN_IN_INDEX
-> R2dbcDataIntegrityViolationException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
unwrapped
)
"21", "23" -> R2dbcDataIntegrityViolationException(
MysqlErrors.ER_TABLE_EXISTS_ERROR,
MysqlErrors.ER_BAD_TABLE_ERROR,
MysqlErrors.ER_BAD_FIELD_ERROR,
MysqlErrors.ER_PARSE_ERROR,
MysqlErrors.ER_ILLEGAL_REFERENCE,
MysqlErrors.ER_NO_SUCH_TABLE,
MysqlErrors.ER_SP_ALREADY_EXISTS,
MysqlErrors.ER_SP_DOES_NOT_EXIST,
MysqlErrors.ER_FUNC_INEXISTENT_NAME_COLLISION
-> R2dbcBadGrammarException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
sql,
unwrapped
)
"H1", "08" -> R2dbcNonTransientResourceException(
MysqlErrors.ER_LOCK_WAIT_TIMEOUT,
MysqlErrors.ER_QUERY_TIMEOUT,
3024 -> R2dbcTimeoutException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
unwrapped
)
"40" -> R2dbcRollbackException(
MysqlErrors.ER_XA_RBROLLBACK,
MysqlErrors.ER_XA_RBTIMEOUT -> R2dbcRollbackException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
unwrapped
)
else -> JasyncDatabaseException(
MysqlErrors.ER_NET_READ_INTERRUPTED,
MysqlErrors.ER_NET_WRITE_INTERRUPTED,
MysqlErrors.ER_LOCK_DEADLOCK,
MysqlErrors.ER_QUERY_INTERRUPTED -> R2dbcTransientResourceException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
throwable
unwrapped
)
else -> when (errorMessage.sqlState.take(2)) {
"0A", "22", "26", "2F", "20", "42", "XA" -> R2dbcBadGrammarException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
unwrapped
)
"25", "28" -> R2dbcPermissionDeniedException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
unwrapped
)
"21", "23" -> R2dbcDataIntegrityViolationException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
unwrapped
)
"H1", "08" -> R2dbcNonTransientResourceException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
unwrapped
)
"40" -> R2dbcRollbackException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
unwrapped
)
else -> JasyncDatabaseException(
errorMessage.errorMessage,
errorMessage.sqlState,
errorMessage.errorCode,
unwrapped
)
}
}
}
}

is InsufficientParametersException -> R2dbcDataIntegrityViolationException(
throwable.message,
throwable
)
is InsufficientParametersException -> R2dbcDataIntegrityViolationException(
unwrapped.message,
unwrapped
)

else -> JasyncDatabaseException("Unknown exception", "UNKOWN", -1, throwable)
else -> JasyncDatabaseException(unwrapped.message ?: "Unknown exception", "UNKNOWN", -1, unwrapped)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.jasync.r2dbc.mysql

import io.mockk.mockk
import io.r2dbc.spi.R2dbcTimeoutException
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.util.concurrent.CompletionException
import java.util.concurrent.TimeoutException

class JasyncStatementExceptionMappingTest {

@Test
fun `maps TimeoutException and CompletionException(TimeoutException) to R2dbcTimeoutException`() {
val statement = JasyncStatement(mockk(), "SELECT 1")
val timeout = TimeoutException("Operation timeout after it took too long to return (PT3S)")

val direct = statement.mapException(timeout)
assertThat(direct)
.isInstanceOf(R2dbcTimeoutException::class.java)
.hasMessage(timeout.message)
.hasCause(timeout)

val wrapped = statement.mapException(CompletionException(timeout))
assertThat(wrapped)
.isInstanceOf(R2dbcTimeoutException::class.java)
.hasMessage(timeout.message)
.hasCause(timeout)
}

@Test
fun `preserves exception message on fallback JasyncDatabaseException`() {
val statement = JasyncStatement(mockk(), "SELECT 1")
val failure = RuntimeException("unexpected failure details")

val mapped = statement.mapException(CompletionException(failure))
assertThat(mapped)
.isInstanceOf(JasyncDatabaseException::class.java)
.hasMessage("unexpected failure details")
.hasCause(failure)
assertThat((mapped as JasyncDatabaseException).sqlState).isEqualTo("UNKNOWN")
}
}
Loading
Loading