Consistent default connection release mode with Hibernate 5.1
Issue: SPR-14548
This commit is contained in:
		
							parent
							
								
									4ada571384
								
							
						
					
					
						commit
						7d1c2f191a
					
				|  | @ -42,7 +42,6 @@ import org.hibernate.boot.MetadataSources; | ||||||
| import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; | import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; | ||||||
| import org.hibernate.cfg.AvailableSettings; | import org.hibernate.cfg.AvailableSettings; | ||||||
| import org.hibernate.cfg.Configuration; | import org.hibernate.cfg.Configuration; | ||||||
| import org.hibernate.cfg.Environment; |  | ||||||
| import org.hibernate.context.spi.CurrentTenantIdentifierResolver; | import org.hibernate.context.spi.CurrentTenantIdentifierResolver; | ||||||
| import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; | import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; | ||||||
| import org.hibernate.engine.spi.SessionFactoryImplementor; | import org.hibernate.engine.spi.SessionFactoryImplementor; | ||||||
|  | @ -138,13 +137,27 @@ public class LocalSessionFactoryBuilder extends Configuration { | ||||||
| 	public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) { | 	public LocalSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader, MetadataSources metadataSources) { | ||||||
| 		super(metadataSources); | 		super(metadataSources); | ||||||
| 
 | 
 | ||||||
| 		getProperties().put(Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); | 		getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); | ||||||
| 		if (dataSource != null) { | 		if (dataSource != null) { | ||||||
| 			getProperties().put(Environment.DATASOURCE, dataSource); | 			getProperties().put(AvailableSettings.DATASOURCE, dataSource); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		// Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default) | 		// Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default) | ||||||
| 		getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); | 		try { | ||||||
|  | 			// Try Hibernate 5.2 | ||||||
|  | 			AvailableSettings.class.getField("CONNECTION_HANDLING"); | ||||||
|  | 			getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); | ||||||
|  | 		} | ||||||
|  | 		catch (NoSuchFieldException ex) { | ||||||
|  | 			// Try Hibernate 5.1 | ||||||
|  | 			try { | ||||||
|  | 				AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); | ||||||
|  | 				getProperties().put("hibernate.connection.release_mode", "ON_CLOSE"); | ||||||
|  | 			} | ||||||
|  | 			catch (NoSuchFieldException ex2) { | ||||||
|  | 				// on Hibernate 5.0.x or lower - no need to change the default there | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader())); | 		getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader())); | ||||||
| 		this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); | 		this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); | ||||||
|  | @ -191,8 +204,22 @@ public class LocalSessionFactoryBuilder extends Configuration { | ||||||
| 					"Unknown transaction manager type: " + jtaTransactionManager.getClass().getName()); | 					"Unknown transaction manager type: " + jtaTransactionManager.getClass().getName()); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		// Hibernate 5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default) | 		// Hibernate 5.1/5.2: manually enforce connection release mode AFTER_STATEMENT (the JTA default) | ||||||
| 		getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"); | 		try { | ||||||
|  | 			// Try Hibernate 5.2 | ||||||
|  | 			AvailableSettings.class.getField("CONNECTION_HANDLING"); | ||||||
|  | 			getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_RELEASE_AFTER_STATEMENT"); | ||||||
|  | 		} | ||||||
|  | 		catch (NoSuchFieldException ex) { | ||||||
|  | 			// Try Hibernate 5.1 | ||||||
|  | 			try { | ||||||
|  | 				AvailableSettings.class.getField("ACQUIRE_CONNECTIONS"); | ||||||
|  | 				getProperties().put("hibernate.connection.release_mode", "AFTER_STATEMENT"); | ||||||
|  | 			} | ||||||
|  | 			catch (NoSuchFieldException ex2) { | ||||||
|  | 				// on Hibernate 5.0.x or lower - no need to change the default there | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		return this; | 		return this; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -154,8 +154,22 @@ public class HibernateJpaVendorAdapter extends AbstractJpaVendorAdapter { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		if (this.jpaDialect.prepareConnection) { | 		if (this.jpaDialect.prepareConnection) { | ||||||
| 			// Hibernate 5.2: manually enforce connection release mode ON_CLOSE (the former default) | 			// Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default) | ||||||
| 			jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); | 			try { | ||||||
|  | 				// Try Hibernate 5.2 | ||||||
|  | 				Environment.class.getField("CONNECTION_HANDLING"); | ||||||
|  | 				jpaProperties.put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD"); | ||||||
|  | 			} | ||||||
|  | 			catch (NoSuchFieldException ex) { | ||||||
|  | 				// Try Hibernate 5.1 | ||||||
|  | 				try { | ||||||
|  | 					Environment.class.getField("ACQUIRE_CONNECTIONS"); | ||||||
|  | 					jpaProperties.put("hibernate.connection.release_mode", "ON_CLOSE"); | ||||||
|  | 				} | ||||||
|  | 				catch (NoSuchFieldException ex2) { | ||||||
|  | 					// on Hibernate 5.0.x or lower - no need to change the default there | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		return jpaProperties; | 		return jpaProperties; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue