21 Aralık 2022 Çarşamba

SpringBoot spring.jpa Hibernate'e Özel Ayarlar - show_sql

Giriş
Açıklaması şöyle
Hibernate defines the hibernate.show_sql configuration property to enable logging. Unfortunately, the logs go to console, which makes it very difficult to filter them appropriately.
Örnek
Şöyle yaparız.
jpa:
    hibernate:
      ddl-auto: update
      show_sql: true
Bazı iyileştirmeler şöyle
1. Yeni Log Adapter Eklenir.
2. datasource-proxy kütüphanesi kullanılır
3. Eğer Application Server kullanıyorsak P6spy kullanılır. Bu SpringBoot için geçerli ve ilgili değil

1. Yeni Log Adapter Eklenir
Örnek
Şöyle yaparız. Birinci logger SQL cümlelerini loglar ancak parametreleri loglamaz. İkinci logger parametreleri de loglar
<logger name="org.hibernate.SQL" level="debug"/>


<!-- For Hibernate 6 -->
<logger name="org.hibernate.orm.jdbc.bind" level="trace"/>
 
<!-- For Hibernate 5 -->
<logger name="org.hibernate.type.descriptor.sql" level="trace"/>
Çıktı şöyledir
o.h.SQL - insert into post (title, version, id) values (?, ?, ?)
 
o.h.t.d.s.BasicBinder - binding parameter [1] as [VARCHAR] - [High-Performance Java Persistence]
o.h.t.d.s.BasicBinder - binding parameter [2] as [INTEGER] - [0]
o.h.t.d.s.BasicBinder - binding parameter [3] as [BIGINT] - [1]
2. DataSource-proxy
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>net.ttddyy</groupId>
  <artifactId>datasource-proxy</artifactId>
  <version>[LATEST_VERSION]</version>
</dependency>
Şöyle yaparız
@Bean
public DataSource dataSource() {
  SLF4JQueryLoggingListener loggingListener = new SLF4JQueryLoggingListener();
  loggingListener.setQueryLogEntryCreator(new InlineQueryLogEntryCreator());
  return ProxyDataSourceBuilder
    .create(actualDataSource())
    .name(DATA_SOURCE_PROXY_NAME)
    .listener(loggingListener)
    .build();
}
Çıktı şöyledir
Name:DATA_SOURCE_PROXY, Time:6, Success:True,
Type:Prepared, Batch:True, QuerySize:1, BatchSize:3,
Query:["insert into post (title, version, id) values (?, ?, ?)"],
Params:[(Post no. 0, 0, 0), (Post no. 1, 0, 1), (Post no. 2, 0, 2)]
Açıklaması şöyle
Not only the bind parameter values are now present, but, because they are grouped altogether, it is very easy to visualize the batching mechanism too.

With the custom statement listener support, datasource-proxy allows building a query count validator to assert the auto-generated statement count and, therefore, prevent N+1 query problems during the development phase.








Hiç yorum yok:

Yorum Gönder