4
4
*/
5
5
package org .hibernate .engine .jdbc .connections .internal ;
6
6
7
+ import java .lang .reflect .InvocationTargetException ;
7
8
import java .sql .Connection ;
8
9
import java .util .Collection ;
9
10
import java .util .HashSet ;
19
20
import org .hibernate .internal .CoreLogging ;
20
21
import org .hibernate .internal .CoreMessageLogger ;
21
22
import org .hibernate .internal .util .StringHelper ;
23
+ import org .hibernate .resource .beans .container .spi .BeanContainer ;
24
+ import org .hibernate .resource .beans .internal .FallbackBeanInstanceProducer ;
25
+ import org .hibernate .resource .beans .internal .Helper ;
26
+ import org .hibernate .resource .beans .spi .BeanInstanceProducer ;
27
+ import org .hibernate .resource .beans .spi .ManagedBeanRegistry ;
22
28
import org .hibernate .service .spi .ServiceRegistryImplementor ;
23
29
24
30
import static java .sql .Connection .TRANSACTION_NONE ;
@@ -102,6 +108,7 @@ public ConnectionProvider initiateService(
102
108
return null ;
103
109
}
104
110
111
+ final BeanContainer beanContainer = Helper .allowExtensionsInCdi ( registry ) ? registry .requireService ( ManagedBeanRegistry .class ).getBeanContainer () : null ;
105
112
final StrategySelector strategySelector = registry .requireService ( StrategySelector .class );
106
113
final Object explicitSetting = configurationValues .get ( CONNECTION_PROVIDER );
107
114
if ( explicitSetting != null ) {
@@ -111,25 +118,25 @@ public ConnectionProvider initiateService(
111
118
}
112
119
else if ( explicitSetting instanceof Class <?> providerClass ) {
113
120
LOG .instantiatingExplicitConnectionProvider ( providerClass .getName () );
114
- return instantiateExplicitConnectionProvider ( providerClass );
121
+ return instantiateExplicitConnectionProvider ( providerClass , beanContainer );
115
122
}
116
123
else {
117
124
final String providerName = nullIfEmpty ( explicitSetting .toString () );
118
125
if ( providerName != null ) {
119
- return instantiateNamedConnectionProvider (providerName , strategySelector );
126
+ return instantiateNamedConnectionProvider (providerName , strategySelector , beanContainer );
120
127
}
121
128
}
122
129
}
123
130
124
- return instantiateConnectionProvider ( configurationValues , strategySelector );
131
+ return instantiateConnectionProvider ( configurationValues , strategySelector , beanContainer );
125
132
}
126
133
127
- private ConnectionProvider instantiateNamedConnectionProvider (String providerName , StrategySelector strategySelector ) {
134
+ private ConnectionProvider instantiateNamedConnectionProvider (String providerName , StrategySelector strategySelector , BeanContainer beanContainer ) {
128
135
LOG .instantiatingExplicitConnectionProvider ( providerName );
129
136
final Class <?> providerClass =
130
137
strategySelector .selectStrategyImplementor ( ConnectionProvider .class , providerName );
131
138
try {
132
- return instantiateExplicitConnectionProvider ( providerClass );
139
+ return instantiateExplicitConnectionProvider ( providerClass , beanContainer );
133
140
}
134
141
catch (Exception e ) {
135
142
throw new HibernateException (
@@ -140,7 +147,7 @@ private ConnectionProvider instantiateNamedConnectionProvider(String providerNam
140
147
}
141
148
142
149
private ConnectionProvider instantiateConnectionProvider (
143
- Map <String , Object > configurationValues , StrategySelector strategySelector ) {
150
+ Map <String , Object > configurationValues , StrategySelector strategySelector , BeanContainer beanContainer ) {
144
151
if ( configurationValues .containsKey ( DATASOURCE ) ) {
145
152
return new DatasourceConnectionProviderImpl ();
146
153
}
@@ -149,9 +156,9 @@ private ConnectionProvider instantiateConnectionProvider(
149
156
getSingleRegisteredProvider ( strategySelector );
150
157
if ( singleRegisteredProvider != null ) {
151
158
try {
152
- return singleRegisteredProvider .newInstance ();
159
+ return singleRegisteredProvider .getConstructor (). newInstance ();
153
160
}
154
- catch (IllegalAccessException | InstantiationException e ) {
161
+ catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException e ) {
155
162
throw new HibernateException ( "Could not instantiate singular-registered ConnectionProvider" , e );
156
163
}
157
164
}
@@ -177,11 +184,47 @@ else if ( configurationValues.containsKey( URL ) ) {
177
184
return new DriverManagerConnectionProviderImpl ();
178
185
}
179
186
else {
180
- LOG .noAppropriateConnectionProvider ();
181
- return new UserSuppliedConnectionProviderImpl ();
187
+ if (beanContainer != null ) {
188
+ return beanContainer .getBean (
189
+ ConnectionProvider .class ,
190
+ new BeanContainer .LifecycleOptions () {
191
+ @ Override
192
+ public boolean canUseCachedReferences () {
193
+ return true ;
194
+ }
195
+
196
+ @ Override
197
+ public boolean useJpaCompliantCreation () {
198
+ return true ;
199
+ }
200
+ },
201
+ new BeanInstanceProducer () {
202
+
203
+ @ Override
204
+ public <B > B produceBeanInstance (Class <B > beanType ) {
205
+ return (B ) noAppropriateConnectionProvider ();
206
+ }
207
+
208
+ @ Override
209
+ public <B > B produceBeanInstance (String name , Class <B > beanType ) {
210
+ return (B ) noAppropriateConnectionProvider ();
211
+ }
212
+
213
+ }
214
+ ).getBeanInstance ();
215
+ }
216
+ else {
217
+ return noAppropriateConnectionProvider ();
218
+ }
219
+
182
220
}
183
221
}
184
222
223
+ private ConnectionProvider noAppropriateConnectionProvider () {
224
+ LOG .noAppropriateConnectionProvider ();
225
+ return new UserSuppliedConnectionProviderImpl ();
226
+ }
227
+
185
228
private Class <? extends ConnectionProvider > getSingleRegisteredProvider (StrategySelector strategySelector ) {
186
229
final Collection <Class <? extends ConnectionProvider >> implementors =
187
230
strategySelector .getRegisteredStrategyImplementors ( ConnectionProvider .class );
@@ -190,9 +233,28 @@ private Class<? extends ConnectionProvider> getSingleRegisteredProvider(Strategy
190
233
: null ;
191
234
}
192
235
193
- private ConnectionProvider instantiateExplicitConnectionProvider (Class <?> providerClass ) {
236
+ private ConnectionProvider instantiateExplicitConnectionProvider (Class <?> providerClass , BeanContainer beanContainer ) {
194
237
try {
195
- return (ConnectionProvider ) providerClass .newInstance ();
238
+ if ( beanContainer != null ) {
239
+ return (ConnectionProvider ) beanContainer .getBean (
240
+ providerClass ,
241
+ new BeanContainer .LifecycleOptions () {
242
+ @ Override
243
+ public boolean canUseCachedReferences () {
244
+ return true ;
245
+ }
246
+
247
+ @ Override
248
+ public boolean useJpaCompliantCreation () {
249
+ return true ;
250
+ }
251
+ },
252
+ FallbackBeanInstanceProducer .INSTANCE
253
+ ).getBeanInstance ();
254
+ }
255
+ else {
256
+ return (ConnectionProvider ) providerClass .getConstructor ().newInstance ();
257
+ }
196
258
}
197
259
catch (Exception e ) {
198
260
throw new HibernateException ( "Could not instantiate connection provider [" + providerClass .getName () + "]" , e );
@@ -201,7 +263,7 @@ private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> provid
201
263
202
264
private static ConnectionProvider instantiateProvider (StrategySelector selector , String strategy ) {
203
265
try {
204
- return selector .selectStrategyImplementor ( ConnectionProvider .class , strategy ).newInstance ();
266
+ return selector .selectStrategyImplementor ( ConnectionProvider .class , strategy ).getConstructor (). newInstance ();
205
267
}
206
268
catch ( Exception e ) {
207
269
LOG .providerClassNotFound (strategy );
0 commit comments