|
42 | 42 | import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; |
43 | 43 | import org.hibernate.cfg.AvailableSettings; |
44 | 44 | import org.hibernate.cfg.Configuration; |
45 | | -import org.hibernate.cfg.Environment; |
46 | 45 | import org.hibernate.context.spi.CurrentTenantIdentifierResolver; |
47 | 46 | import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; |
48 | 47 | import org.hibernate.engine.spi.SessionFactoryImplementor; |
@@ -138,13 +137,27 @@ public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resource |
138 | 137 | public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) { |
139 | 138 | super(metadataSources); |
140 | 139 |
|
141 | | - getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); |
| 140 | + getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); |
142 | 141 | if (dataSource != null) { |
143 | | - getProperties().put(Environment.DATASOURCE, dataSource); |
| 142 | + getProperties().put(AvailableSettings.DATASOURCE, dataSource); |
144 | 143 | } |
145 | 144 |
|
146 | | - // Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default) |
147 | | - getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); |
| 145 | + // Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default) |
| 146 | + try { |
| 147 | + // Try Hibernate 5.2 |
| 148 | + AvailableSettings.class.getField("CONNECTION_HANDLING"); |
| 149 | + getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); |
| 150 | + } |
| 151 | + catch (NoSuchFieldException ex) { |
| 152 | + // Try Hibernate 5.1 |
| 153 | + try { |
| 154 | + AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); |
| 155 | + getProperties().put("hibernate.connection.release_mode", "ON_CLOSE"); |
| 156 | + } |
| 157 | + catch (NoSuchFieldException ex2) { |
| 158 | + // on Hibernate 5.0.x or lower - no need to change the default there |
| 159 | + } |
| 160 | + } |
148 | 161 |
|
149 | 162 | getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader())); |
150 | 163 | this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); |
@@ -191,8 +204,22 @@ else if (jtaTransactionManager instanceof TransactionManager) { |
191 | 204 | "Unknown transaction manager type: " + jtaTransactionManager.getClass().getName()); |
192 | 205 | } |
193 | 206 |
|
194 | | - // Hibernate 5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default) |
195 | | - getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"); |
| 207 | + // Hibernate 5.1/5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default) |
| 208 | + try { |
| 209 | + // Try Hibernate 5.2 |
| 210 | + AvailableSettings.class.getField("CONNECTION_HANDLING"); |
| 211 | + getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"); |
| 212 | + } |
| 213 | + catch (NoSuchFieldException ex) { |
| 214 | + // Try Hibernate 5.1 |
| 215 | + try { |
| 216 | + AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); |
| 217 | + getProperties().put("hibernate.connection.release_mode", "AFTER_STATEMENT"); |
| 218 | + } |
| 219 | + catch (NoSuchFieldException ex2) { |
| 220 | + // on Hibernate 5.0.x or lower - no need to change the default there |
| 221 | + } |
| 222 | + } |
196 | 223 |
|
197 | 224 | return this; |
198 | 225 | } |
|
0 commit comments