Skip to content

Commit 715d7c3

Browse files
author
Ilya Nemtsev
committed
fixed batch writes
1 parent 8ee3266 commit 715d7c3

File tree

1 file changed

+8
-6
lines changed
  • frameworks/Kotlin/ktor/ktor/src/main/kotlin/org/jetbrains/ktor/benchmarks

1 file changed

+8
-6
lines changed

frameworks/Kotlin/ktor/ktor/src/main/kotlin/org/jetbrains/ktor/benchmarks/Hello.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.jetbrains.ktor.benchmarks.Constants.UPDATE_QUERY
1919
import org.jetbrains.ktor.benchmarks.Constants.WORLD_QUERY
2020
import java.sql.Connection
2121
import java.util.concurrent.ThreadLocalRandom
22+
import kotlin.random.Random
2223

2324
@Serializable
2425
data class Message(val message: String)
@@ -49,7 +50,7 @@ fun Application.main() {
4950
}
5051

5152
get("/db") {
52-
val random = ThreadLocalRandom.current()
53+
val random = Random.Default
5354

5455
val world = withContext(databaseDispatcher) {
5556
pool.connection.use { connection ->
@@ -67,7 +68,7 @@ fun Application.main() {
6768
call.respondText(Json.encodeToString(world), ContentType.Application.Json)
6869
}
6970

70-
fun Connection.selectWorlds(queries: Int, random: ThreadLocalRandom): List<World> {
71+
fun Connection.selectWorlds(queries: Int, random: Random): List<World> {
7172
val result = ArrayList<World>(queries)
7273
prepareStatement(WORLD_QUERY).use { statement ->
7374
repeat(queries) {
@@ -85,7 +86,7 @@ fun Application.main() {
8586

8687
get("/queries") {
8788
val queries = call.queries()
88-
val random = ThreadLocalRandom.current()
89+
val random = Random.Default
8990

9091
val result = withContext(databaseDispatcher) {
9192
pool.connection.use { it.selectWorlds(queries, random) }
@@ -130,7 +131,7 @@ fun Application.main() {
130131

131132
get("/updates") {
132133
val queries = call.queries()
133-
val random = ThreadLocalRandom.current()
134+
val random = Random.Default
134135
val result: List<World>
135136

136137
withContext(databaseDispatcher) {
@@ -144,9 +145,10 @@ fun Application.main() {
144145
for ((id, randomNumber) in result) {
145146
updateStatement.setInt(1, randomNumber)
146147
updateStatement.setInt(2, id)
147-
148-
updateStatement.executeUpdate()
148+
updateStatement.addBatch()
149149
}
150+
151+
updateStatement.executeBatch()
150152
}
151153
}
152154
}

0 commit comments

Comments
 (0)