77import java .io .Serial ;
88import java .sql .Connection ;
99import java .sql .SQLException ;
10- import java .sql .DatabaseMetaData ;
1110import javax .sql .DataSource ;
1211import java .util .Map ;
1312import java .util .function .Consumer ;
1413import java .util .function .Function ;
1514
15+ import io .agroal .api .configuration .AgroalConnectionFactoryConfiguration .TransactionIsolation ;
1616import org .hibernate .HibernateException ;
1717import org .hibernate .cfg .AgroalSettings ;
1818import org .hibernate .cfg .AvailableSettings ;
19+ import org .hibernate .cfg .JdbcSettings ;
1920import org .hibernate .dialect .Dialect ;
2021import org .hibernate .engine .jdbc .connections .internal .ConnectionProviderInitiator ;
2122import org .hibernate .engine .jdbc .connections .internal .DatabaseConnectionInfoImpl ;
3536import io .agroal .api .security .NamePrincipal ;
3637import io .agroal .api .security .SimplePassword ;
3738
39+ import static java .util .function .Function .identity ;
3840import static java .util .stream .Collectors .toMap ;
3941import static org .hibernate .cfg .AgroalSettings .AGROAL_CONFIG_PREFIX ;
4042import static org .hibernate .engine .jdbc .env .internal .JdbcEnvironmentInitiator .allowJdbcMetadataAccess ;
@@ -77,24 +79,23 @@ public class AgroalConnectionProvider implements ConnectionProvider, Configurabl
7779
7880 private static String extractIsolationAsString (Map <String , Object > properties ) {
7981 final Integer isolation = ConnectionProviderInitiator .extractIsolation ( properties );
80- if ( isolation != null ) {
81- // Agroal resolves transaction isolation from the 'nice' name
82- return ConnectionProviderInitiator .toIsolationNiceName ( isolation );
83- }
84- return null ;
82+ return isolation != null ?
83+ // Agroal resolves transaction isolation from the 'nice' name
84+ ConnectionProviderInitiator .toIsolationNiceName ( isolation )
85+ : null ;
8586 }
8687
8788 private static void resolveIsolationSetting (Map <String , Object > properties , AgroalConnectionFactoryConfigurationSupplier cf ) {
8889 final String isolationString = extractIsolationAsString ( properties );
8990 if ( isolationString != null ) {
90- cf .jdbcTransactionIsolation ( AgroalConnectionFactoryConfiguration . TransactionIsolation .valueOf ( isolationString ) );
91+ cf .jdbcTransactionIsolation ( TransactionIsolation .valueOf ( isolationString ) );
9192 }
9293 }
9394
9495 private static <T > void copyProperty (Map <String , Object > properties , String key , Consumer <T > consumer , Function <String , T > converter ) {
9596 final Object value = properties .get ( key );
96- if ( value instanceof String string ) {
97- consumer .accept ( converter .apply ( string ) );
97+ if ( value != null ) {
98+ consumer .accept ( converter .apply ( value . toString () ) );
9899 }
99100 }
100101
@@ -104,14 +105,24 @@ public void configure(Map<String, Object> properties) throws HibernateException
104105
105106 ConnectionInfoLogger .INSTANCE .configureConnectionPool ( "Agroal" );
106107 try {
107- final AgroalPropertiesReader agroalProperties = new AgroalPropertiesReader ( CONFIG_PREFIX )
108- .readProperties ( toStringValuedProperties ( properties ) );
109- agroalProperties .modify ().connectionPoolConfiguration ( cp -> cp .connectionFactoryConfiguration ( cf -> {
110- copyProperty ( properties , AvailableSettings .DRIVER , cf ::connectionProviderClassName , Function .identity () );
111- copyProperty ( properties , AvailableSettings .URL , cf ::jdbcUrl , Function .identity () );
112- copyProperty ( properties , AvailableSettings .USER , cf ::principal , NamePrincipal ::new );
113- copyProperty ( properties , AvailableSettings .PASS , cf ::credential , SimplePassword ::new );
114- copyProperty ( properties , AvailableSettings .AUTOCOMMIT , cf ::autoCommit , Boolean ::valueOf );
108+ final Map <String , String > config = toStringValuedProperties ( properties );
109+ if ( !properties .containsKey ( AgroalSettings .AGROAL_MAX_SIZE ) ) {
110+ final String maxSize =
111+ properties .containsKey ( JdbcSettings .POOL_SIZE )
112+ ? properties .get ( JdbcSettings .POOL_SIZE ).toString ()
113+ : String .valueOf ( 10 );
114+ config .put ( AgroalSettings .AGROAL_MAX_SIZE , maxSize );
115+ }
116+ final AgroalPropertiesReader agroalProperties =
117+ new AgroalPropertiesReader ( CONFIG_PREFIX )
118+ .readProperties ( config );
119+ agroalProperties .modify ()
120+ .connectionPoolConfiguration ( cp -> cp .connectionFactoryConfiguration ( cf -> {
121+ copyProperty ( properties , JdbcSettings .DRIVER , cf ::connectionProviderClassName , identity () );
122+ copyProperty ( properties , JdbcSettings .URL , cf ::jdbcUrl , identity () );
123+ copyProperty ( properties , JdbcSettings .USER , cf ::principal , NamePrincipal ::new );
124+ copyProperty ( properties , JdbcSettings .PASS , cf ::credential , SimplePassword ::new );
125+ copyProperty ( properties , JdbcSettings .AUTOCOMMIT , cf ::autoCommit , Boolean ::valueOf );
115126 resolveIsolationSetting ( properties , cf );
116127 return cf ;
117128 } ) );
@@ -152,30 +163,31 @@ public boolean supportsAggressiveRelease() {
152163
153164 @ Override
154165 public DatabaseConnectionInfo getDatabaseConnectionInfo (Dialect dialect ) {
155- final AgroalConnectionPoolConfiguration acpc = agroalDataSource .getConfiguration ().connectionPoolConfiguration ();
166+ final AgroalConnectionPoolConfiguration acpc =
167+ agroalDataSource .getConfiguration ().connectionPoolConfiguration ();
156168 final AgroalConnectionFactoryConfiguration acfc = acpc .connectionFactoryConfiguration ();
157-
158-
159169 return new DatabaseConnectionInfoImpl (
160170 acfc .jdbcUrl (),
161- // Attempt to resolve the driver name from the dialect, in case it wasn't explicitly set and access to
162- // the database metadata is allowed
163- acfc .connectionProviderClass () != null ? acfc .connectionProviderClass ().toString () : extractDriverNameFromMetadata (),
171+ // Attempt to resolve the driver name from the dialect,
172+ // in case it wasn't explicitly set and access to the
173+ // database metadata is allowed
174+ acfc .connectionProviderClass () != null
175+ ? acfc .connectionProviderClass ().toString ()
176+ : extractDriverNameFromMetadata (),
164177 dialect .getVersion (),
165178 Boolean .toString ( acfc .autoCommit () ),
166179 acfc .jdbcTransactionIsolation () != null
167180 ? ConnectionProviderInitiator .toIsolationNiceName ( acfc .jdbcTransactionIsolation ().level () )
168181 : null ,
169182 acpc .minSize (),
170- acpc .minSize ()
183+ acpc .maxSize ()
171184 );
172185 }
173186
174187 private String extractDriverNameFromMetadata () {
175188 if (isMetadataAccessAllowed ) {
176189 try ( Connection conn = getConnection () ) {
177- DatabaseMetaData dbmd = conn .getMetaData ();
178- return dbmd .getDriverName ();
190+ return conn .getMetaData ().getDriverName ();
179191 }
180192 catch (SQLException e ) {
181193 // Do nothing
@@ -209,8 +221,11 @@ public <T> T unwrap(Class<T> unwrapType) {
209221 @ Override
210222 public void stop () {
211223 if ( agroalDataSource != null ) {
212- ConnectionInfoLogger .INSTANCE .cleaningUpConnectionPool ( agroalDataSource .getConfiguration ().connectionPoolConfiguration ().
213- connectionFactoryConfiguration ().jdbcUrl () );
224+ ConnectionInfoLogger .INSTANCE .cleaningUpConnectionPool (
225+ agroalDataSource .getConfiguration ()
226+ .connectionPoolConfiguration ()
227+ .connectionFactoryConfiguration ()
228+ .jdbcUrl () );
214229 agroalDataSource .close ();
215230 }
216231 }
0 commit comments