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

feat: EXPOSED-224 Add query timeout at Transaction #1890

Merged
Merged
Show file tree
Hide file tree
Changes from 12 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
4 changes: 4 additions & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,7 @@ public class org/jetbrains/exposed/sql/Transaction : org/jetbrains/exposed/sql/U
public final fun getStatementCount ()I
public final fun getStatementStats ()Ljava/util/HashMap;
public final fun getStatements ()Ljava/lang/StringBuilder;
public final fun getTimeout ()Ljava/lang/Integer;
public fun getTransactionIsolation ()I
public final fun getWarnLongQueriesDuration ()Ljava/lang/Long;
public final fun identity (Lorg/jetbrains/exposed/sql/Column;)Ljava/lang/String;
Expand All @@ -2281,6 +2282,7 @@ public class org/jetbrains/exposed/sql/Transaction : org/jetbrains/exposed/sql/U
public final fun setMinRepetitionDelay (J)V
public final fun setRepetitionAttempts (I)V
public final fun setStatementCount (I)V
public final fun setTimeout (Ljava/lang/Integer;)V
public final fun setWarnLongQueriesDuration (Ljava/lang/Long;)V
public final fun unregisterInterceptor (Lorg/jetbrains/exposed/sql/statements/StatementInterceptor;)Z
}
Expand Down Expand Up @@ -2934,10 +2936,12 @@ public abstract interface class org/jetbrains/exposed/sql/statements/api/Prepare
public abstract fun fillParameters (Ljava/lang/Iterable;)I
public abstract fun getFetchSize ()Ljava/lang/Integer;
public abstract fun getResultSet ()Ljava/sql/ResultSet;
public abstract fun getTimeout ()Ljava/lang/Integer;
public abstract fun set (ILjava/lang/Object;)V
public abstract fun setFetchSize (Ljava/lang/Integer;)V
public abstract fun setInputStream (ILjava/io/InputStream;)V
public abstract fun setNull (ILorg/jetbrains/exposed/sql/IColumnType;)V
public abstract fun setTimeout (Ljava/lang/Integer;)V
}

public final class org/jetbrains/exposed/sql/statements/api/PreparedStatementApi$DefaultImpls {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ open class Transaction(
/** The maximum number of milliseconds to wait before retrying this `transaction` if SQLException happens */
var maxRepetitionDelay: Long = db.transactionManager.defaultMaxRepetitionDelay

/** The number of seconds to wait before timing out a statement */
var timeout: Int? = null
FullOfOrange marked this conversation as resolved.
Show resolved Hide resolved

val id by lazy { UUID.randomUUID().toString() }

// currently executing statement. Used to log error properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ abstract class Statement<out T>(val type: StatementType, val targets: List<Table
}

val statement = try {
prepared(transaction, prepareSQL(transaction))
prepared(transaction, prepareSQL(transaction)).apply {
timeout = transaction.timeout
}
FullOfOrange marked this conversation as resolved.
Show resolved Hide resolved
} catch (e: SQLException) {
throw ExposedSQLException(e, contexts, transaction)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface PreparedStatementApi {

var fetchSize: Int?

var timeout: Int?

fun fillParameters(args: Iterable<Pair<IColumnType, Any?>>): Int {
args.forEachIndexed { index, (c, v) ->
c.setParameter(this, index + 1, c.valueToDB(v))
Expand Down
2 changes: 2 additions & 0 deletions exposed-jdbc/api/exposed-jdbc.api
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcPreparedStateme
public fun getFetchSize ()Ljava/lang/Integer;
public fun getResultSet ()Ljava/sql/ResultSet;
public final fun getStatement ()Ljava/sql/PreparedStatement;
public fun getTimeout ()Ljava/lang/Integer;
public final fun getWasGeneratedKeysRequested ()Z
public fun set (ILjava/lang/Object;)V
public fun setFetchSize (Ljava/lang/Integer;)V
public fun setInputStream (ILjava/io/InputStream;)V
public fun setNull (ILorg/jetbrains/exposed/sql/IColumnType;)V
public fun setTimeout (Ljava/lang/Integer;)V
}

public final class org/jetbrains/exposed/sql/statements/jdbc/JdbcSavepoint : org/jetbrains/exposed/sql/statements/api/ExposedSavepoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class JdbcPreparedStatementImpl(
value?.let { statement.fetchSize = value }
}

override var timeout: Int?
get() = statement.queryTimeout
set(value) {
value?.let { statement.queryTimeout = value }
}

override fun addBatch() {
statement.addBatch()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.jetbrains.exposed.sql.tests.shared

import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.junit.Test
import kotlin.test.assertFailsWith

/**
* @author [email protected]
*/
class QueryTimeoutTest : DatabaseTestsBase() {

private fun generateTimeoutStatements(db: TestDB, timeout: Int): String {
return when (db) {
TestDB.MYSQL -> "SELECT SLEEP($timeout) = 0;"
TestDB.POSTGRESQL -> "SELECT pg_sleep($timeout);"
else -> throw NotImplementedError()
}
}
FullOfOrange marked this conversation as resolved.
Show resolved Hide resolved

@Test
fun timeoutStatements() {
withDb(listOf(TestDB.MYSQL, TestDB.POSTGRESQL)) {
this.timeout = 3
assertFailsWith<ExposedSQLException> {
TransactionManager.current().exec(
generateTimeoutStatements(it, 5)
)
}
FullOfOrange marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Test
fun noTimeoutWithTimeoutStatement() {
withDb(listOf(TestDB.MYSQL, TestDB.POSTGRESQL)) {
this.timeout = 3
TransactionManager.current().exec(
generateTimeoutStatements(it, 1)
)
}
}
}
Loading