19 Aralık 2019 Perşembe

SpringBoot DataSource application.properties PostgreSQL Ayarları

Giriş
Hibernate ile Postgresql kullanıyorsak hibernate.dialect alanında bunu görmemiz gerekir. Çıktı olarak şunu alırız.
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed
LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactor for persistence unit 'default'
org.hibernate.jpa.internal.LogHelper : Processing PersistenceUnitInfo [ name : default ...]
org.hibernate.Version : Hibernate Core [5.2.17.Final]
org.hibernate.cfg.Environment : hibernate.properties not found
org.hibernate.annotations.common.Version : Hibernate Commons Annotations {5.0.1.Final}
org.hibernate.dialect.Dialect : Using dialect : org.hibernate.dialect.PostgreSQL95Dialect
LobCreatorBuilderImpl : Disabling contextual LOB creation as connection was null
org.hibernate.type.BasicTypeRegistry : Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIBBinaryType
PgBouncer
Eğer connection pool olarak PgBouncer kullanıyorsak şöyle yaparız
spring:
  datasource:
    type: org.springframework.jdbc.datasource.DriverManagerDataSource
    driver-class-name: org.postgresql.Driver
Zorunlu Alanlar
Şöyle
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=myuser
spring.datasource.password=mypassword
- Eğer Hibernate kullanıyorsak şu alan zorunlu değil
spring.jpa.properties.hibernate.dialect
Açıklaması şöyle
The dialect to use is detected by the JPA provider. If you prefer to set the dialect yourself, set the spring.jpa.database-platform property.
- spring.database.driver-class-name alanı zorunlu değil. Şöyle yapmaya gerek yok. jdbc url'den otomatik bulunuyor
spring.datasource.driver-class-name=org.postgresql.Driver
- Eğer schema vermek istersek şöyle yaparız
spring.jpa.properties.hibernate.default_schema=myschema
Örnek
Kendi projemizde şöyle yaptık.
spring.datasource.url=jdbc:postgresql://myserver:5432/mydb
spring.jpa.properties.hibernate.default_schema=myschema
spring.datasource.username=myuser
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.Driver


spring.datasoure.hikari.minimum-idle=2
spring.datasoure.hikari.maximum-pool-size=5


spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
Önce spring.jpa.properties.hibernate.dialect alanına değer atıyorduk şöyle yapıyorduk
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
Ancak daha sonra gördük ki bu alana değer atamasak ta Hibernate yine PostgreSQL95Dialect kullanıyor. Bu alanı sildik.

spring.datasource.platform Alanı
Açıklaması şöyle
Platform to use in DDL or DML scripts - such as schema-${platform}.sql or data-${platform}.sql
Örnek
Şöyle yaparız.
# DATA SOURCE
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=root
Örnek
Şöyle yaparız.
# Configure postgres
spring.datasource.platform=postgres
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/qweqwe
spring.datasource.username=postgres
spring.datasource.password=dontcopythis
spring.jpa.database Alanı
Bu alanı doldurmak aslında gerekmiyor. Otomatik olarak bulunuyor
Örnek
Şöyle yaparız.
# DATA SOURCE
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.generate-ddl=true

spring.jpa.show-sql = true

spring.database.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto = update
Örnek
Şöyle yaparız.
# Configure postgres

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/qweqwe
spring.datasource.username=postgres
spring.datasource.password=dontcopythis

Hiç yorum yok:

Yorum Gönder