Skip to content

Commit 2279b52

Browse files
committed
more work on DatabaseConnectionInfo
sensible reuse of the JDBC Connection
1 parent a14f6c0 commit 2279b52

File tree

9 files changed

+537
-538
lines changed

9 files changed

+537
-538
lines changed

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
2424
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
2525
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
26+
import org.hibernate.exception.JDBCConnectionException;
2627
import org.hibernate.internal.log.ConnectionInfoLogger;
2728
import org.hibernate.service.UnknownUnwrapTypeException;
2829
import org.hibernate.service.spi.Configurable;
@@ -110,7 +111,7 @@ public void configure(Map<String, Object> properties) throws HibernateException
110111

111112
ConnectionInfoLogger.INSTANCE.configureConnectionPool( "Agroal" );
112113
try {
113-
final Map<String, String> config = toStringValuedProperties( properties );
114+
final var config = toStringValuedProperties( properties );
114115
if ( !properties.containsKey( AgroalSettings.AGROAL_MAX_SIZE ) ) {
115116
final String maxSize =
116117
properties.containsKey( JdbcSettings.POOL_SIZE )
@@ -170,30 +171,35 @@ public boolean supportsAggressiveRelease() {
170171
public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
171172
final var poolConfig = agroalDataSource.getConfiguration().connectionPoolConfiguration();
172173
final var connectionConfig = poolConfig.connectionFactoryConfiguration();
173-
return new DatabaseConnectionInfoImpl(
174-
AgroalConnectionProvider.class,
175-
connectionConfig.jdbcUrl(),
176-
// Attempt to resolve the driver name from the dialect,
177-
// in case it wasn't explicitly set and access to the
178-
// database metadata is allowed
179-
connectionConfig.connectionProviderClass() != null
180-
? connectionConfig.connectionProviderClass().toString()
181-
: extractDriverNameFromMetadata(),
182-
dialect.getClass(),
183-
dialect.getVersion(),
184-
hasSchema( agroalDataSource),
185-
hasCatalog( agroalDataSource),
186-
getSchema( agroalDataSource ),
187-
getCatalog( agroalDataSource ),
188-
Boolean.toString( connectionConfig.autoCommit() ),
189-
connectionConfig.jdbcTransactionIsolation() != null
190-
&& connectionConfig.jdbcTransactionIsolation().isDefined()
191-
? toIsolationNiceName( connectionConfig.jdbcTransactionIsolation().level() )
192-
: toIsolationNiceName( getIsolation( agroalDataSource ) ),
193-
poolConfig.minSize(),
194-
poolConfig.maxSize(),
195-
getFetchSize( agroalDataSource )
196-
);
174+
try ( var connection = agroalDataSource.getConnection() ) {
175+
return new DatabaseConnectionInfoImpl(
176+
AgroalConnectionProvider.class,
177+
connectionConfig.jdbcUrl(),
178+
// Attempt to resolve the driver name from the dialect,
179+
// in case it wasn't explicitly set and access to the
180+
// database metadata is allowed
181+
connectionConfig.connectionProviderClass() != null
182+
? connectionConfig.connectionProviderClass().toString()
183+
: extractDriverNameFromMetadata(),
184+
dialect.getClass(),
185+
dialect.getVersion(),
186+
hasSchema( connection ),
187+
hasCatalog( connection ),
188+
getSchema( connection ),
189+
getCatalog( connection ),
190+
Boolean.toString( connectionConfig.autoCommit() ),
191+
connectionConfig.jdbcTransactionIsolation() != null
192+
&& connectionConfig.jdbcTransactionIsolation().isDefined()
193+
? toIsolationNiceName( connectionConfig.jdbcTransactionIsolation().level() )
194+
: toIsolationNiceName( getIsolation( connection ) ),
195+
poolConfig.minSize(),
196+
poolConfig.maxSize(),
197+
getFetchSize( connection )
198+
);
199+
}
200+
catch (SQLException e) {
201+
throw new JDBCConnectionException( "Could not create connection", e );
202+
}
197203
}
198204

199205
private String extractDriverNameFromMetadata() {

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

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
1414
import org.hibernate.engine.jdbc.connections.spi.ConnectionProviderConfigurationException;
1515
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
16+
import org.hibernate.exception.JDBCConnectionException;
1617
import org.hibernate.internal.log.ConnectionInfoLogger;
1718
import org.hibernate.service.UnknownUnwrapTypeException;
1819
import org.hibernate.service.spi.Configurable;
@@ -163,32 +164,37 @@ public void configure(Map<String, Object> properties) {
163164
final var poolSettings = poolSettings( properties );
164165
dataSource = createDataSource( jdbcUrl, connectionProps, poolSettings );
165166

166-
final Integer fetchSize = getFetchSize( dataSource );
167-
final boolean hasSchema = hasSchema( dataSource );
168-
final boolean hasCatalog = hasCatalog( dataSource );
169-
final String schema = getSchema( dataSource );
170-
final String catalog = getCatalog( dataSource );
171-
if ( isolation == null ) {
172-
isolation = getIsolation( dataSource );
167+
try ( var connection = dataSource.getConnection() ) {
168+
final Integer fetchSize = getFetchSize( connection );
169+
final boolean hasSchema = hasSchema( connection );
170+
final boolean hasCatalog = hasCatalog( connection );
171+
final String schema = getSchema( connection );
172+
final String catalog = getCatalog( connection );
173+
if ( isolation == null ) {
174+
isolation = getIsolation( connection );
175+
}
176+
dbInfoProducer = dialect -> new DatabaseConnectionInfoImpl(
177+
C3P0ConnectionProvider.class,
178+
jdbcUrl,
179+
jdbcDriverClass,
180+
dialect.getClass(),
181+
dialect.getVersion(),
182+
hasSchema,
183+
hasCatalog,
184+
schema,
185+
catalog,
186+
Boolean.toString( autocommit ),
187+
isolation == null ? null : toIsolationNiceName( isolation ),
188+
requireNonNullElse( getInteger( C3P0_STYLE_MIN_POOL_SIZE.substring( 5 ), poolSettings ),
189+
DEFAULT_MIN_POOL_SIZE ),
190+
requireNonNullElse( getInteger( C3P0_STYLE_MAX_POOL_SIZE.substring( 5 ), poolSettings ),
191+
DEFAULT_MAX_POOL_SIZE ),
192+
fetchSize
193+
);
194+
}
195+
catch (SQLException e) {
196+
throw new JDBCConnectionException( "Could not create connection", e );
173197
}
174-
dbInfoProducer = dialect -> new DatabaseConnectionInfoImpl(
175-
C3P0ConnectionProvider.class,
176-
jdbcUrl,
177-
jdbcDriverClass,
178-
dialect.getClass(),
179-
dialect.getVersion(),
180-
hasSchema,
181-
hasCatalog,
182-
schema,
183-
catalog,
184-
Boolean.toString( autocommit ),
185-
isolation == null ? null : toIsolationNiceName( isolation ),
186-
requireNonNullElse( getInteger( C3P0_STYLE_MIN_POOL_SIZE.substring( 5 ), poolSettings ),
187-
DEFAULT_MIN_POOL_SIZE ),
188-
requireNonNullElse( getInteger( C3P0_STYLE_MAX_POOL_SIZE.substring( 5 ), poolSettings ),
189-
DEFAULT_MAX_POOL_SIZE ),
190-
fetchSize
191-
);
192198
}
193199

194200
private DataSource createDataSource(String jdbcUrl, Properties connectionProps, Map<String, Object> poolProperties) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @author Steve Ebersole
1313
*/
14-
interface ConnectionCreator {
14+
public interface ConnectionCreator {
1515
/**
1616
* Obtain the URL to which this creator connects. Intended just for informational (logging) purposes.
1717
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @author Christian Beikov
1717
*/
18-
interface ConnectionCreatorFactory {
18+
public interface ConnectionCreatorFactory {
1919

2020
ConnectionCreator create(
2121
Driver driver,

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

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.engine.jdbc.connections.internal;
66

7+
import java.sql.Connection;
78
import java.sql.SQLException;
89
import java.util.Map;
910

@@ -16,7 +17,6 @@
1617
import org.hibernate.internal.util.NullnessHelper;
1718
import org.hibernate.internal.util.config.ConfigurationHelper;
1819

19-
import javax.sql.DataSource;
2020

2121
import static org.hibernate.dialect.SimpleDatabaseVersion.ZERO_VERSION;
2222
import static org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.interpretIsolation;
@@ -117,114 +117,55 @@ public DatabaseConnectionInfoImpl(Dialect dialect) {
117117
);
118118
}
119119

120-
public static boolean hasSchema(DataSource dataSource) {
121-
try ( var conn = dataSource.getConnection() ) {
122-
return conn.getMetaData().supportsSchemasInDataManipulation();
120+
public static boolean hasSchema(Connection connection) {
121+
try {
122+
return connection.getMetaData().supportsSchemasInTableDefinitions();
123123
}
124124
catch ( SQLException ignored ) {
125125
return true;
126126
}
127127
}
128128

129-
public static boolean hasCatalog(DataSource dataSource) {
130-
try ( var conn = dataSource.getConnection() ) {
131-
return conn.getMetaData().supportsCatalogsInDataManipulation();
129+
public static boolean hasCatalog(Connection connection) {
130+
try {
131+
return connection.getMetaData().supportsCatalogsInDataManipulation();
132132
}
133133
catch ( SQLException ignored ) {
134134
return true;
135135
}
136136
}
137137

138-
public static String getSchema(DataSource dataSource) {
139-
try ( var conn = dataSource.getConnection() ) {
138+
public static String getSchema(Connection connection) {
139+
try {
140140
// method introduced in 1.7
141-
return conn.getSchema();
141+
return connection.getSchema();
142142
}
143143
catch ( SQLException|AbstractMethodError ignored ) {
144144
return null;
145145
}
146146
}
147147

148-
public static String getCatalog(DataSource dataSource) {
149-
try ( var conn = dataSource.getConnection() ) {
150-
return conn.getCatalog();
148+
public static String getCatalog(Connection connection) {
149+
try {
150+
return connection.getCatalog();
151151
}
152152
catch ( SQLException ignored ) {
153153
return null;
154154
}
155155
}
156156

157-
static boolean hasSchema(ConnectionCreator creator) {
158-
try ( var conn = creator.createConnection() ) {
159-
return conn.getMetaData().supportsSchemasInTableDefinitions();
160-
}
161-
catch ( SQLException ignored ) {
162-
return true;
163-
}
164-
}
165-
166-
static boolean hasCatalog(ConnectionCreator creator) {
167-
try ( var conn = creator.createConnection() ) {
168-
return conn.getMetaData().supportsCatalogsInDataManipulation();
169-
}
170-
catch ( SQLException ignored ) {
171-
return true;
172-
}
173-
}
174-
175-
static String getSchema(ConnectionCreator creator) {
176-
try ( var conn = creator.createConnection() ) {
177-
// method introduced in 1.7
178-
return conn.getSchema();
179-
}
180-
catch ( SQLException|AbstractMethodError ignored ) {
181-
return null;
182-
}
183-
}
184-
185-
static String getCatalog(ConnectionCreator creator) {
186-
try ( var conn = creator.createConnection() ) {
187-
return conn.getCatalog();
188-
}
189-
catch ( SQLException ignored ) {
190-
return null;
191-
}
192-
}
193-
194-
public static Integer getFetchSize(DataSource dataSource) {
195-
try ( var conn = dataSource.getConnection() ) {
196-
try ( var statement = conn.createStatement() ) {
197-
return statement.getFetchSize();
198-
}
199-
}
200-
catch ( SQLException ignored ) {
201-
return null;
202-
}
203-
}
204-
205-
public static Integer getIsolation(DataSource dataSource) {
206-
try ( var conn = dataSource.getConnection() ) {
207-
return conn.getTransactionIsolation();
208-
}
209-
catch ( SQLException ignored ) {
210-
return null;
211-
}
212-
}
213-
214-
static Integer getFetchSize(ConnectionCreator creator) {
215-
try ( var conn = creator.createConnection() ) {
216-
try ( var statement = conn.createStatement() ) {
217-
return statement.getFetchSize();
218-
}
157+
public static Integer getFetchSize(Connection connection) {
158+
try ( var statement = connection.createStatement() ) {
159+
return statement.getFetchSize();
219160
}
220161
catch ( SQLException ignored ) {
221162
return null;
222163
}
223164
}
224165

225-
static Integer getIsolation(ConnectionCreator creator) {
226-
try ( var conn = creator.createConnection() ) {
227-
return conn.getTransactionIsolation();
166+
public static Integer getIsolation(Connection connection) {
167+
try {
168+
return connection.getTransactionIsolation();
228169
}
229170
catch ( SQLException ignored ) {
230171
return null;

0 commit comments

Comments
 (0)