2828import org .hibernate .dialect .Dialect ;
2929import org .hibernate .dialect .SimpleDatabaseVersion ;
3030import org .hibernate .engine .jdbc .connections .spi .ConnectionProvider ;
31+ import org .hibernate .engine .jdbc .connections .spi .ConnectionProviderConfigurationException ;
3132import org .hibernate .engine .jdbc .connections .spi .DatabaseConnectionInfo ;
3233import org .hibernate .internal .util .config .ConfigurationHelper ;
3334import org .hibernate .service .UnknownUnwrapTypeException ;
@@ -90,20 +91,18 @@ private PooledConnections buildPool(Map<String,Object> configurationValues, Serv
9091 final int maxSize = ConfigurationHelper .getInt ( AvailableSettings .POOL_SIZE , configurationValues , 20 );
9192 final int initialSize = ConfigurationHelper .getInt ( INITIAL_SIZE , configurationValues , minSize );
9293
93- ConnectionCreator connectionCreator = buildCreator ( configurationValues , serviceRegistry );
94- PooledConnections .Builder pooledConnectionBuilder = new PooledConnections .Builder (
95- connectionCreator ,
96- autoCommit
97- );
94+ final ConnectionCreator creator = buildCreator ( configurationValues , serviceRegistry );
95+ final PooledConnections .Builder pooledConnectionBuilder = new PooledConnections .Builder ( creator , autoCommit );
9896 pooledConnectionBuilder .initialSize ( initialSize );
9997 pooledConnectionBuilder .minSize ( minSize );
10098 pooledConnectionBuilder .maxSize ( maxSize );
10199 pooledConnectionBuilder .validator ( this );
102100 return pooledConnectionBuilder .build ();
103101 }
104102
105- private static ConnectionCreator buildCreator (Map <String ,Object > configurationValues , ServiceRegistryImplementor serviceRegistry ) {
106- final String url = (String ) configurationValues .get ( AvailableSettings .URL );
103+ private static ConnectionCreator buildCreator (
104+ Map <String ,Object > configurationValues , ServiceRegistryImplementor serviceRegistry ) {
105+ final String url = jdbcUrl ( configurationValues );
107106
108107 String driverClassName = (String ) configurationValues .get ( AvailableSettings .DRIVER );
109108 boolean success = false ;
@@ -112,14 +111,14 @@ private static ConnectionCreator buildCreator(Map<String,Object> configurationVa
112111 driver = loadDriverIfPossible ( driverClassName , serviceRegistry );
113112 success = true ;
114113 }
115- else if ( url != null ) {
114+ else {
116115 //try to guess the driver class from the JDBC URL
117116 for ( Database database : Database .values () ) {
118117 if ( database .matchesUrl ( url ) ) {
119118 driverClassName = database .getDriverClassName ( url );
120119 if ( driverClassName != null ) {
121120 try {
122- loadDriverIfPossible (driverClassName , serviceRegistry );
121+ loadDriverIfPossible ( driverClassName , serviceRegistry );
123122 success = true ;
124123 }
125124 catch (Exception e ) {
@@ -132,49 +131,24 @@ else if ( url != null ) {
132131 }
133132 }
134133
135- StringBuilder list = new StringBuilder ();
136- if ( !success ) {
137- //we're hoping that the driver is already loaded
138- ConnectionInfoLogger .INSTANCE .jdbcDriverNotSpecified ();
139- Enumeration <Driver > drivers = DriverManager .getDrivers ();
140- while ( drivers .hasMoreElements () ) {
141- if ( list .length () != 0 ) {
142- list .append (", " );
143- }
144- list .append ( drivers .nextElement ().getClass ().getName () );
145- }
146- }
147-
148- if ( url == null ) {
149- throw new HibernateException ( "No JDBC URL specified by property " + JAKARTA_JDBC_URL );
150- }
134+ final String driverList = success ? driverClassName : driverList ();
151135
152136 final Properties connectionProps = ConnectionProviderInitiator .getConnectionProperties ( configurationValues );
153137
154138 final boolean autoCommit = ConfigurationHelper .getBoolean ( AvailableSettings .AUTOCOMMIT , configurationValues );
155139 final Integer isolation = ConnectionProviderInitiator .extractIsolation ( configurationValues );
156140 final String initSql = (String ) configurationValues .get ( INIT_SQL );
157141
158- final Object connectionCreatorFactory = configurationValues .get ( CONNECTION_CREATOR_FACTORY );
159- ConnectionCreatorFactory factory = null ;
160- if ( connectionCreatorFactory instanceof ConnectionCreatorFactory ) {
161- factory = (ConnectionCreatorFactory ) connectionCreatorFactory ;
162- }
163- else if ( connectionCreatorFactory != null ) {
164- factory = loadConnectionCreatorFactory ( connectionCreatorFactory .toString (), serviceRegistry );
165- }
166- if ( factory == null ) {
167- factory = ConnectionCreatorFactoryImpl .INSTANCE ;
168- }
142+ final ConnectionCreatorFactory factory = getConnectionCreatorFactory ( configurationValues , serviceRegistry );
169143
170144 dbInfo = new DatabaseConnectionInfoImpl (
171145 url ,
172- success ? driverClassName : list . toString () ,
146+ driverList ,
173147 SimpleDatabaseVersion .ZERO_VERSION ,
174148 Boolean .toString ( autoCommit ),
175- isolation != null ? ConnectionProviderInitiator .toIsolationNiceName (isolation ) : null ,
176- ConfigurationHelper .getInt (MIN_SIZE , configurationValues , 1 ),
177- ConfigurationHelper .getInt (AvailableSettings .POOL_SIZE , configurationValues , 20 )
149+ isolation != null ? ConnectionProviderInitiator .toIsolationNiceName ( isolation ) : null ,
150+ ConfigurationHelper .getInt ( MIN_SIZE , configurationValues , 1 ),
151+ ConfigurationHelper .getInt ( AvailableSettings .POOL_SIZE , configurationValues , 20 )
178152 );
179153
180154 return factory .create (
@@ -189,38 +163,77 @@ else if ( connectionCreatorFactory != null ) {
189163 );
190164 }
191165
166+ private static String driverList () {
167+ //we're hoping that the driver is already loaded
168+ ConnectionInfoLogger .INSTANCE .jdbcDriverNotSpecified ();
169+ final StringBuilder list = new StringBuilder ();
170+ final Enumeration <Driver > drivers = DriverManager .getDrivers ();
171+ while ( drivers .hasMoreElements () ) {
172+ if ( !list .isEmpty () ) {
173+ list .append (", " );
174+ }
175+ list .append ( drivers .nextElement ().getClass ().getName () );
176+ }
177+ return list .toString ();
178+ }
179+
180+ private static String jdbcUrl (Map <String , Object > configurationValues ) {
181+ final String url = (String ) configurationValues .get ( AvailableSettings .URL );
182+ if ( url == null ) {
183+ throw new ConnectionProviderConfigurationException ( "No JDBC URL specified by property '" + JAKARTA_JDBC_URL + "'" );
184+ }
185+ return url ;
186+ }
187+
188+ private static ConnectionCreatorFactory getConnectionCreatorFactory (
189+ Map <String , Object > configurationValues , ServiceRegistryImplementor serviceRegistry ) {
190+ final Object connectionCreatorFactory = configurationValues .get ( CONNECTION_CREATOR_FACTORY );
191+ final ConnectionCreatorFactory factory ;
192+ if ( connectionCreatorFactory instanceof ConnectionCreatorFactory instance ) {
193+ factory = instance ;
194+ }
195+ else if ( connectionCreatorFactory != null ) {
196+ factory = loadConnectionCreatorFactory ( connectionCreatorFactory .toString (), serviceRegistry );
197+ }
198+ else {
199+ factory = null ;
200+ }
201+ return factory == null ? ConnectionCreatorFactoryImpl .INSTANCE : factory ;
202+ }
203+
192204 private static Driver loadDriverIfPossible (String driverClassName , ServiceRegistryImplementor serviceRegistry ) {
193205 if ( driverClassName == null ) {
194206 ConnectionInfoLogger .INSTANCE .debug ( "No driver class specified" );
195207 return null ;
196208 }
197-
198- if ( serviceRegistry != null ) {
199- final ClassLoaderService classLoaderService = serviceRegistry .requireService ( ClassLoaderService .class );
200- final Class < Driver > driverClass = classLoaderService .classForName ( driverClassName );
209+ else if ( serviceRegistry != null ) {
210+ final Class < Driver > driverClass =
211+ serviceRegistry .requireService ( ClassLoaderService .class )
212+ .classForName ( driverClassName );
201213 try {
202214 return driverClass .newInstance ();
203215 }
204216 catch ( Exception e ) {
205217 throw new ServiceException ( "Specified JDBC Driver " + driverClassName + " could not be loaded" , e );
206218 }
207219 }
208-
209- try {
210- return (Driver ) Class .forName ( driverClassName ).newInstance ();
211- }
212- catch ( Exception e1 ) {
213- throw new ServiceException ( "Specified JDBC Driver " + driverClassName + " could not be loaded" , e1 );
220+ else {
221+ try {
222+ return (Driver ) Class .forName ( driverClassName ).newInstance ();
223+ }
224+ catch (Exception e1 ) {
225+ throw new ServiceException ( "Specified JDBC Driver " + driverClassName + " could not be loaded" , e1 );
226+ }
214227 }
215228 }
216229
217- private static ConnectionCreatorFactory loadConnectionCreatorFactory (String connectionCreatorFactoryClassName , ServiceRegistryImplementor serviceRegistry ) {
230+ private static ConnectionCreatorFactory loadConnectionCreatorFactory (
231+ String connectionCreatorFactoryClassName , ServiceRegistryImplementor serviceRegistry ) {
218232 if ( connectionCreatorFactoryClassName == null ) {
219233 ConnectionInfoLogger .INSTANCE .debug ( "No connection creator factory class specified" );
220234 return null ;
221235 }
222-
223- if ( serviceRegistry != null ) {
236+ else if ( serviceRegistry != null ) {
224237 final ClassLoaderService classLoaderService = serviceRegistry .requireService ( ClassLoaderService .class );
225238 final Class <ConnectionCreatorFactory > factoryClass =
226239 classLoaderService .classForName ( connectionCreatorFactoryClassName );
@@ -231,12 +244,15 @@ private static ConnectionCreatorFactory loadConnectionCreatorFactory(String conn
231244 throw new ServiceException ( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded" , e );
232245 }
233246 }
234-
235- try {
236- return (ConnectionCreatorFactory ) Class .forName ( connectionCreatorFactoryClassName ).newInstance ();
237- }
238- catch ( Exception e1 ) {
239- throw new ServiceException ( "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded" , e1 );
247+ else {
248+ try {
249+ return (ConnectionCreatorFactory ) Class .forName ( connectionCreatorFactoryClassName ).newInstance ();
250+ }
251+ catch (Exception e1 ) {
252+ throw new ServiceException (
253+ "Specified ConnectionCreatorFactory " + connectionCreatorFactoryClassName + " could not be loaded" ,
254+ e1 );
255+ }
240256 }
241257 }
242258
0 commit comments