23
23
*/
24
24
package org .hibernate .engine .jdbc .dialect .internal ;
25
25
26
- import java .sql .Connection ;
27
- import java .sql .DatabaseMetaData ;
28
- import java .sql .SQLException ;
29
26
import java .util .Map ;
30
27
31
28
import org .hibernate .HibernateException ;
34
31
import org .hibernate .cfg .AvailableSettings ;
35
32
import org .hibernate .dialect .Dialect ;
36
33
import org .hibernate .engine .jdbc .dialect .spi .DialectFactory ;
34
+ import org .hibernate .engine .jdbc .dialect .spi .DialectResolutionInfo ;
35
+ import org .hibernate .engine .jdbc .dialect .spi .DialectResolutionInfoSource ;
37
36
import org .hibernate .engine .jdbc .dialect .spi .DialectResolver ;
38
- import org .hibernate .service .spi .InjectService ;
37
+ import org .hibernate .service .spi .ServiceRegistryAwareService ;
38
+ import org .hibernate .service .spi .ServiceRegistryImplementor ;
39
39
40
40
/**
41
41
* Standard implementation of the {@link DialectFactory} service.
42
42
*
43
43
* @author Steve Ebersole
44
44
*/
45
- public class DialectFactoryImpl implements DialectFactory {
45
+ public class DialectFactoryImpl implements DialectFactory , ServiceRegistryAwareService {
46
46
private StrategySelector strategySelector ;
47
+ private DialectResolver dialectResolver ;
47
48
48
- @ InjectService
49
- public void setStrategySelector (StrategySelector strategySelector ) {
50
- this .strategySelector = strategySelector ;
49
+ @ Override
50
+ public void injectServices (ServiceRegistryImplementor serviceRegistry ) {
51
+ this .strategySelector = serviceRegistry .getService ( StrategySelector .class );
52
+ this .dialectResolver = serviceRegistry .getService ( DialectResolver .class );
51
53
}
52
54
53
- private DialectResolver dialectResolver ;
54
-
55
- @ InjectService
55
+ /**
56
+ * Intended only for use from testing.
57
+ *
58
+ * @param dialectResolver The DialectResolver to use
59
+ */
56
60
public void setDialectResolver (DialectResolver dialectResolver ) {
57
61
this .dialectResolver = dialectResolver ;
58
62
}
59
63
60
64
@ Override
61
- public Dialect buildDialect (Map configValues , Connection connection ) throws HibernateException {
65
+ public Dialect buildDialect (Map configValues , DialectResolutionInfoSource resolutionInfoSource ) throws HibernateException {
62
66
final String dialectName = (String ) configValues .get ( AvailableSettings .DIALECT );
63
67
if ( !StringHelper .isEmpty ( dialectName ) ) {
64
68
return constructDialect ( dialectName );
65
69
}
66
70
else {
67
- return determineDialect ( connection );
71
+ return determineDialect ( resolutionInfoSource );
68
72
}
69
73
}
70
74
@@ -88,36 +92,29 @@ private Dialect constructDialect(String dialectName) {
88
92
/**
89
93
* Determine the appropriate Dialect to use given the connection.
90
94
*
91
- * @param connection The configured connection.
95
+ * @param resolutionInfoSource Access to DialectResolutionInfo used to resolve the Dialect.
96
+ *
92
97
* @return The appropriate dialect instance.
93
98
*
94
99
* @throws HibernateException No connection given or no resolver could make
95
100
* the determination from the given connection.
96
101
*/
97
- private Dialect determineDialect (Connection connection ) {
98
- if ( connection == null ) {
99
- throw new HibernateException ( "Connection cannot be null when 'hibernate.dialect' not set" );
102
+ private Dialect determineDialect (DialectResolutionInfoSource resolutionInfoSource ) {
103
+ if ( resolutionInfoSource == null ) {
104
+ throw new HibernateException ( "Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set" );
100
105
}
101
106
102
- try {
103
- final DatabaseMetaData databaseMetaData = connection .getMetaData ();
104
- final Dialect dialect = dialectResolver .resolveDialect ( databaseMetaData );
105
-
106
- if ( dialect == null ) {
107
- throw new HibernateException (
108
- "Unable to determine Dialect to use [name=" + databaseMetaData .getDatabaseProductName () +
109
- ", majorVersion=" + databaseMetaData .getDatabaseMajorVersion () +
110
- "]; user must register resolver or explicitly set 'hibernate.dialect'"
111
- );
112
- }
107
+ final DialectResolutionInfo info = resolutionInfoSource .getDialectResolutionInfo ();
108
+ final Dialect dialect = dialectResolver .resolveDialect ( info );
113
109
114
- return dialect ;
115
- }
116
- catch ( SQLException sqlException ) {
110
+ if ( dialect == null ) {
117
111
throw new HibernateException (
118
- "Unable to access java.sql.DatabaseMetaData to determine appropriate Dialect to use" ,
119
- sqlException
112
+ "Unable to determine Dialect to use [name=" + info .getDatabaseName () +
113
+ ", majorVersion=" + info .getDatabaseMajorVersion () +
114
+ "]; user must register resolver or explicitly set 'hibernate.dialect'"
120
115
);
121
116
}
117
+
118
+ return dialect ;
122
119
}
123
120
}
0 commit comments