@@ -19,6 +19,7 @@ import org.jetbrains.ktor.benchmarks.Constants.UPDATE_QUERY
19
19
import org.jetbrains.ktor.benchmarks.Constants.WORLD_QUERY
20
20
import java.sql.Connection
21
21
import java.util.concurrent.ThreadLocalRandom
22
+ import kotlin.random.Random
22
23
23
24
@Serializable
24
25
data class Message (val message : String )
@@ -49,7 +50,7 @@ fun Application.main() {
49
50
}
50
51
51
52
get(" /db" ) {
52
- val random = ThreadLocalRandom .current()
53
+ val random = Random . Default
53
54
54
55
val world = withContext(databaseDispatcher) {
55
56
pool.connection.use { connection ->
@@ -67,7 +68,7 @@ fun Application.main() {
67
68
call.respondText(Json .encodeToString(world), ContentType .Application .Json )
68
69
}
69
70
70
- fun Connection.selectWorlds (queries : Int , random : ThreadLocalRandom ): List <World > {
71
+ fun Connection.selectWorlds (queries : Int , random : Random ): List <World > {
71
72
val result = ArrayList <World >(queries)
72
73
prepareStatement(WORLD_QUERY ).use { statement ->
73
74
repeat(queries) {
@@ -85,7 +86,7 @@ fun Application.main() {
85
86
86
87
get(" /queries" ) {
87
88
val queries = call.queries()
88
- val random = ThreadLocalRandom .current()
89
+ val random = Random . Default
89
90
90
91
val result = withContext(databaseDispatcher) {
91
92
pool.connection.use { it.selectWorlds(queries, random) }
@@ -130,7 +131,7 @@ fun Application.main() {
130
131
131
132
get(" /updates" ) {
132
133
val queries = call.queries()
133
- val random = ThreadLocalRandom .current()
134
+ val random = Random . Default
134
135
val result: List <World >
135
136
136
137
withContext(databaseDispatcher) {
@@ -139,14 +140,14 @@ fun Application.main() {
139
140
140
141
result.forEach { it.randomNumber = random.nextInt(dbRows) + 1 }
141
142
142
- connection.prepareStatement(UPDATE_QUERY )
143
- .use { updateStatement ->
143
+ connection.prepareStatement(UPDATE_QUERY ).use { updateStatement ->
144
144
for ((id, randomNumber) in result) {
145
145
updateStatement.setInt(1 , randomNumber)
146
146
updateStatement.setInt(2 , id)
147
-
148
- updateStatement.executeUpdate()
147
+ updateStatement.addBatch()
149
148
}
149
+
150
+ updateStatement.executeBatch()
150
151
}
151
152
}
152
153
}
0 commit comments