Skip to content

Commit

Permalink
HikariDataSource を使ってコネクションプーリングを実行する
Browse files Browse the repository at this point in the history
  • Loading branch information
bvlion committed May 4, 2021
1 parent f25ff38 commit 5513ce6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 9 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ jobs:

- name: Setup secrets
run: |
echo "${{ secrets.VPC_ACCESS_CONNECTOR }}" | base64 -d >> app.yaml
echo "${{ secrets.SECRET_YAML }}" | base64 -d > secret.yaml
VERSION=$(echo ${{ github.ref }} | sed -e "s#refs/tags/##g" | sed -e 's/\./-/g')
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ dependencies {
implementation "mysql:mysql-connector-java:$mysql_connector_version"
implementation "org.jsoup:jsoup:1.13.1"
implementation "com.google.cloud.sql:mysql-socket-factory:1.2.0"
implementation "com.zaxxer:HikariCP:4.0.3"

testImplementation "io.ktor:ktor-server-tests:$ktor_version"
testImplementation "io.mockk:mockk:1.10.5"
Expand Down
2 changes: 1 addition & 1 deletion cron/cron.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cron:
- description: "インスタンス定期監視"
target: default
url: /
url: /test
schedule: every 5 mins
timezone: Asia/Tokyo
4 changes: 3 additions & 1 deletion resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ ktor {

app {
database {
url = "jdbc:mysql://127.0.0.1:3336/bookmarks?useSSL=false"
url = "jdbc:mysql://127.0.0.1:3336/bookmarks"
url = ${?DATABASE_URL}
user = "user"
user = ${?DATABASE_USER}
password = "password"
password = ${?DATABASE_PASSWORD}
cloud_sql_instance = ""
cloud_sql_instance = ${?CLOUD_SQL_INSTANCE}
}
auth {
audience = "sharebookmarks"
Expand Down
1 change: 1 addition & 0 deletions resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<appender-ref ref="STDOUT"/>
</root>
<logger name="io.netty" level="INFO"/>
<logger name="com.zaxxer.hikari" level="INFO"/>
</configuration>
30 changes: 24 additions & 6 deletions src/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package net.ambitious.sharebookmarks

import com.auth0.jwt.JWT
import com.fasterxml.jackson.databind.SerializationFeature
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.auth.jwt.*
Expand Down Expand Up @@ -73,12 +75,28 @@ fun Application.module() {
etc()
}

Database.connect(
url = environment.config.property("app.database.url").getString(),
user = environment.config.property("app.database.user").getString(),
password = environment.config.property("app.database.password").getString(),
driver = "com.mysql.jdbc.Driver"
)
Database.connect(HikariDataSource(
HikariConfig().apply {
driverClassName = "com.mysql.jdbc.Driver"
jdbcUrl = environment.config.property("app.database.url").getString()
username = environment.config.property("app.database.user").getString()
password = environment.config.property("app.database.password").getString()
connectionTestQuery = "SELECT 1"
minimumIdle = 5
maximumPoolSize = 10
poolName = "ConnectionPool"
leakDetectionThreshold = 5000
isAutoCommit = false

val cloudSqlInstance = environment.config.property("app.database.cloud_sql_instance").getString()
if (cloudSqlInstance.isNotEmpty()) {
addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory")
addDataSourceProperty("cloudSqlInstance", cloudSqlInstance)
}
addDataSourceProperty("useSSL", "false")
validate()
}
))
}

fun main(args: Array<String>) {
Expand Down

0 comments on commit 5513ce6

Please sign in to comment.