Skip to content

Commit 10c12c4

Browse files
committed
include the JDBC fetch size in the database info log
1 parent f06770d commit 10c12c4

File tree

9 files changed

+97
-19
lines changed

9 files changed

+97
-19
lines changed

hibernate-agroal/src/main/java/org/hibernate/agroal/internal/AgroalConnectionProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,22 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
181181
? ConnectionProviderInitiator.toIsolationNiceName( acfc.jdbcTransactionIsolation().level() )
182182
: null,
183183
acpc.minSize(),
184-
acpc.maxSize()
184+
acpc.maxSize(),
185+
getFetchSize( agroalDataSource )
185186
);
186187
}
187188

189+
private static Integer getFetchSize(DataSource dataSource) {
190+
try ( var conn = dataSource.getConnection() ) {
191+
try ( var statement = conn.createStatement() ) {
192+
return statement.getFetchSize();
193+
}
194+
}
195+
catch ( SQLException ignored ) {
196+
return null;
197+
}
198+
}
199+
188200
private String extractDriverNameFromMetadata() {
189201
if (isMetadataAccessAllowed) {
190202
try ( Connection conn = getConnection() ) {

hibernate-c3p0/src/main/java/org/hibernate/c3p0/internal/C3P0ConnectionProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,22 @@ public void configure(Map<String, Object> properties) {
165165
requireNonNullElse( getInteger( C3P0_STYLE_MIN_POOL_SIZE.substring( 5 ), poolSettings ),
166166
DEFAULT_MIN_POOL_SIZE ),
167167
requireNonNullElse( getInteger( C3P0_STYLE_MAX_POOL_SIZE.substring( 5 ), poolSettings ),
168-
DEFAULT_MAX_POOL_SIZE )
168+
DEFAULT_MAX_POOL_SIZE ),
169+
getFetchSize( dataSource )
169170
);
170171
}
171172

173+
private static Integer getFetchSize(DataSource dataSource) {
174+
try ( var conn = dataSource.getConnection() ) {
175+
try ( var statement = conn.createStatement() ) {
176+
return statement.getFetchSize();
177+
}
178+
}
179+
catch ( SQLException ignored ) {
180+
return null;
181+
}
182+
}
183+
172184
private DataSource createDataSource(String jdbcUrl, Properties connectionProps, Map<String, Object> poolProperties) {
173185
try {
174186
return pooledDataSource( unpooledDataSource( jdbcUrl, connectionProps ), poolProperties );

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatabaseConnectionInfoImpl.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.Map;
88

9+
import org.checkerframework.checker.nullness.qual.Nullable;
910
import org.hibernate.cfg.JdbcSettings;
1011
import org.hibernate.dialect.DatabaseVersion;
1112
import org.hibernate.dialect.Dialect;
@@ -37,6 +38,7 @@ public class DatabaseConnectionInfoImpl implements DatabaseConnectionInfo {
3738
protected final String isolationLevel;
3839
protected final Integer poolMinSize;
3940
protected final Integer poolMaxSize;
41+
private final Integer fetchSize;
4042

4143
public DatabaseConnectionInfoImpl(
4244
Class<? extends ConnectionProvider> connectionProviderClass,
@@ -46,7 +48,8 @@ public DatabaseConnectionInfoImpl(
4648
String autoCommitMode,
4749
String isolationLevel,
4850
Integer poolMinSize,
49-
Integer poolMaxSize) {
51+
Integer poolMaxSize,
52+
Integer fetchSize) {
5053
this.connectionProviderClass = connectionProviderClass;
5154
this.jdbcUrl = nullIfEmpty( jdbcUrl );
5255
this.jdbcDriver = nullIfEmpty( jdbcDriver );
@@ -55,6 +58,7 @@ public DatabaseConnectionInfoImpl(
5558
this.isolationLevel = nullIfEmpty( isolationLevel );
5659
this.poolMinSize = poolMinSize;
5760
this.poolMaxSize = poolMaxSize;
61+
this.fetchSize = fetchSize;
5862
}
5963

6064
public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect) {
@@ -67,12 +71,13 @@ public DatabaseConnectionInfoImpl(Map<String, Object> settings, Dialect dialect)
6771
determineIsolationLevel( settings ),
6872
// No setting for min. pool size
6973
null,
70-
determinePoolMaxSize( settings )
74+
determinePoolMaxSize( settings ),
75+
null
7176
);
7277
}
7378

7479
public DatabaseConnectionInfoImpl(Dialect dialect) {
75-
this( null, null, null, dialect.getVersion(), null, null, null, null );
80+
this( null, null, null, dialect.getVersion(), null, null, null, null, null );
7681
}
7782

7883
@Override
@@ -110,16 +115,34 @@ public Integer getPoolMaxSize() {
110115
return poolMaxSize;
111116
}
112117

118+
@Override
119+
public @Nullable Integer getJdbcFetchSize() {
120+
return fetchSize;
121+
}
122+
113123
@Override
114124
public String toInfoString() {
115-
return "\tDatabase JDBC URL [" + handleEmpty( jdbcUrl ) + ']' +
116-
"\n\tDatabase driver: " + handleEmpty( jdbcDriver ) +
117-
"\n\tDatabase version: " + handleEmpty( dialectVersion ) +
118-
"\n\tAutocommit mode: " + handleEmpty( autoCommitMode ) +
119-
"\n\tIsolation level: " + handleEmpty( isolationLevel ) +
120-
"\n\tPool: " + handleEmpty( connectionProviderClass ) +
121-
"\n\tMinimum pool size: " + handleEmpty( poolMinSize ) +
122-
"\n\tMaximum pool size: " + handleEmpty( poolMaxSize );
125+
return """
126+
\tDatabase JDBC URL [%s]
127+
\tDatabase driver: %s
128+
\tDatabase version: %s
129+
\tAutocommit mode: %s
130+
\tIsolation level: %s
131+
\tPool: %s
132+
\tMinimum pool size: %s
133+
\tMaximum pool size: %s
134+
\tJDBC fetch size: %s"""
135+
.formatted(
136+
handleEmpty( jdbcUrl ),
137+
handleEmpty( jdbcDriver ),
138+
handleEmpty( dialectVersion ),
139+
handleEmpty( autoCommitMode ),
140+
handleEmpty( isolationLevel ),
141+
handleEmpty( connectionProviderClass ),
142+
handleEmpty( poolMinSize ),
143+
handleEmpty( poolMaxSize ),
144+
handleEmpty( fetchSize )
145+
);
123146
}
124147

125148
private static String handleEmpty(String value) {
@@ -131,6 +154,10 @@ private static String handleEmpty(DatabaseVersion dialectVersion) {
131154
}
132155

133156
private static String handleEmpty(Integer value) {
157+
return value != null ? ( value == 0 ? "none" : value.toString() ) : DEFAULT;
158+
}
159+
160+
private static String handleFetchSize(Integer value) {
134161
return value != null ? value.toString() : DEFAULT;
135162
}
136163

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DatasourceConnectionProviderImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,21 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, Extract
160160
final String url;
161161
final String driver;
162162
final String isolationLevel;
163+
final Integer fetchSize;
163164
if ( metaData != null ) {
164165
url = metaData.getUrl();
165166
driver = metaData.getDriver();
166-
isolationLevel = toIsolationNiceName( metaData.getTransactionIsolation() )
167+
isolationLevel =
168+
toIsolationNiceName( metaData.getTransactionIsolation() )
167169
+ " [default " + toIsolationNiceName( metaData.getDefaultTransactionIsolation() ) + "]";
170+
final int defaultFetchSize = metaData.getDefaultFetchSize();
171+
fetchSize = defaultFetchSize == -1 ? null : defaultFetchSize;
168172
}
169173
else {
170174
url = null;
171175
driver = null;
172176
isolationLevel = null;
177+
fetchSize = null;
173178
}
174179

175180
return new DatabaseConnectionInfoImpl(
@@ -180,7 +185,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect, Extract
180185
null,
181186
isolationLevel,
182187
null,
183-
null
188+
null,
189+
fetchSize
184190
) {
185191
@Override
186192
public String toInfoString() {

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/DriverManagerConnectionProviderImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ private static ConnectionCreator buildCreator(
150150
Boolean.toString( autoCommit ),
151151
isolation != null ? toIsolationNiceName( isolation ) : null,
152152
getInt( MIN_SIZE, configurationValues, 1 ),
153-
getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 )
153+
getInt( AvailableSettings.POOL_SIZE, configurationValues, 20 ),
154+
null
154155
);
155156

156157
return factory.create(
@@ -292,7 +293,8 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
292293
dbInfo.getAutoCommitMode(),
293294
dbInfo.getIsolationLevel(),
294295
dbInfo.getPoolMinSize(),
295-
dbInfo.getPoolMaxSize()
296+
dbInfo.getPoolMaxSize(),
297+
dbInfo.getJdbcFetchSize()
296298
);
297299
}
298300

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DataSourceBasedMultiTenantConnectionProviderImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
125125
null,
126126
null,
127127
null,
128+
null,
128129
null
129130
) {
130131
@Override

hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/spi/DatabaseConnectionInfo.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ public interface DatabaseConnectionInfo {
6161
@Nullable
6262
Integer getPoolMaxSize();
6363

64+
/**
65+
* The default JDBC fetch size.
66+
*/
67+
@Nullable
68+
Integer getJdbcFetchSize();
69+
6470
/**
6571
* Collects the information available here as a single String with the intent of using it in logging.
6672
*/

hibernate-core/src/main/java/org/hibernate/engine/jdbc/env/spi/ExtractedDatabaseMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public interface ExtractedDatabaseMetaData {
151151
int getDefaultTransactionIsolation();
152152

153153
/**
154-
* Retrieve the default JDBC {@link java.sql.Statement#getFetchSize fetch size}.
154+
* Retrieve the default JDBC {@linkplain java.sql.Statement#getFetchSize fetch size}.
155155
*/
156156
int getDefaultFetchSize();
157157

hibernate-hikaricp/src/main/java/org/hibernate/hikaricp/internal/HikariCPConnectionProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,22 @@ public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
107107
Boolean.toString( hikariConfig.isAutoCommit() ),
108108
hikariConfig.getTransactionIsolation(),
109109
hikariConfig.getMinimumIdle(),
110-
hikariConfig.getMaximumPoolSize()
110+
hikariConfig.getMaximumPoolSize(),
111+
getFetchSize( hikariDataSource )
111112
);
112113
}
113114

115+
private static Integer getFetchSize(DataSource dataSource) {
116+
try ( var conn = dataSource.getConnection() ) {
117+
try ( var statement = conn.createStatement() ) {
118+
return statement.getFetchSize();
119+
}
120+
}
121+
catch ( SQLException ignored ) {
122+
return null;
123+
}
124+
}
125+
114126
private String extractDriverNameFromMetadata() {
115127
if ( isMetadataAccessAllowed ) {
116128
try ( Connection conn = getConnection() ) {

0 commit comments

Comments
 (0)