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 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
4 changes: 4 additions & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,7 @@ public class org/jetbrains/exposed/sql/Transaction : org/jetbrains/exposed/sql/U
public final fun getMaxRepetitionDelay ()J
public final fun getMinRepetitionDelay ()J
public fun getOuterTransaction ()Lorg/jetbrains/exposed/sql/Transaction;
public final fun getQueryTimeout ()Ljava/lang/Integer;
public fun getReadOnly ()Z
public final fun getRepetitionAttempts ()I
public final fun getStatementCount ()I
Expand All @@ -2279,6 +2280,7 @@ public class org/jetbrains/exposed/sql/Transaction : org/jetbrains/exposed/sql/U
public final fun setDuration (J)V
public final fun setMaxRepetitionDelay (J)V
public final fun setMinRepetitionDelay (J)V
public final fun setQueryTimeout (Ljava/lang/Integer;)V
public final fun setRepetitionAttempts (I)V
public final fun setStatementCount (I)V
public final fun setWarnLongQueriesDuration (Ljava/lang/Long;)V
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,12 @@ 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 the JDBC driver should wait for a statement to execute in [Transaction] transaction before timing out.
* Note Not all JDBC drivers implement this limit. Please check the driver documentation.
*/
var queryTimeout: Int? = null

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.queryTimeout
}
} 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 = it }
}

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

import com.impossibl.postgres.jdbc.PGSQLSimpleException
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.Assert.fail
import org.junit.Test
import org.postgresql.util.PSQLException
import java.sql.SQLTimeoutException

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

private fun generateTimeoutStatements(db: TestDB, timeout: Int): String {
return when (db) {
TestDB.MYSQL, TestDB.MARIADB -> "SELECT SLEEP($timeout) = 0;"
TestDB.POSTGRESQL, TestDB.POSTGRESQLNG -> "SELECT pg_sleep($timeout);"
TestDB.SQLSERVER -> "WAITFOR DELAY '00:00:$timeout';"
else -> throw NotImplementedError()
}
}

private val timeoutTestDBList = listOf(TestDB.MYSQL, TestDB.MARIADB, TestDB.POSTGRESQL, TestDB.POSTGRESQLNG, TestDB.SQLSERVER)

@Test
fun timeoutStatements() {
withDb(timeoutTestDBList) { testDB ->
this.queryTimeout = 3
try {
TransactionManager.current().exec(
generateTimeoutStatements(testDB, 5)
)
fail("Should have thrown a timeout or cancelled statement exception")
} catch (cause: ExposedSQLException) {
when (testDB) {
// PostgreSQL throws a regular PgSQLException with a cancelled statement message
TestDB.POSTGRESQL -> assertTrue(cause.cause is PSQLException)
// PostgreSQLNG throws a regular PGSQLSimpleException with a cancelled statement message
TestDB.POSTGRESQLNG -> assertTrue(cause.cause is PGSQLSimpleException)
else -> assertTrue(cause.cause is SQLTimeoutException)
}
}
}
}

@Test
fun noTimeoutWithTimeoutStatement() {
withDb(timeoutTestDBList) {
this.queryTimeout = 3
TransactionManager.current().exec(
generateTimeoutStatements(it, 1)
)
}
}
}
Loading