尝试以编程方式验证架构
@Autowired
LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean;
//获取hibernate配置的代码
private Configuration getHibernateConfiguration() {
final PersistenceUnitInfo persistenceUnitInfo = localContainerEntityManagerFactoryBean.getPersistenceUnitInfo();
final Map<String, Object> jpaPropertyMap = localContainerEntityManagerFactoryBean.getJpaPropertyMap();
EntityManagerFactoryBuilderImpl builder = new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(persistenceUnitInfo), jpaPropertyMap) {
@Override
public Configuration buildHibernateConfiguration(ServiceRegistry serviceRegistry) {
Configuration configuration = super.buildHibernateConfiguration(serviceRegistry);
if (persistenceUnitInfo instanceof SmartPersistenceUnitInfo) {
for (String managedPackage : ((SmartPersistenceUnitInfo) persistenceUnitInfo).getManagedPackages()) {
configuration.addPackage(managedPackage);
}
}
return configuration;
}
};
serviceRegistry = builder.buildServiceRegistry();
return builder.buildHibernateConfiguration(serviceRegistry);
}
此方法尝试从Configuration类调用schemaValidator方法。
public void getSchemaValidationService(DataSource dataSource) {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", hibernateDialect);
Dialect dialect = Dialect.getDialect(hibernateProperties);
Configuration configuration = getHibernateConfiguration(dataSource);
Connection connection = null;
try {
connection = dataSource().getConnection();
DatabaseMetadata metadata = new DatabaseMetadata(connection, dialect, configuration);
configuration.validateSchema(dialect, metadata);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
new SqlExceptionHelper().logAndClearWarnings( connection );
connection.setAutoCommit( false );
connection.close();
logger.error("Connection Closed : "+connection.isClosed() );
connection = null;
StandardServiceRegistryBuilder.destroy(serviceRegistry);
} catch (SQLException e) {
logger.error("Datasource connection failed to close");
}
}
}
}
在多租户模式下使用hibernate,以便尝试以编程方式验证架构。
模式验证工作正常,但几分钟后它就会尝试抛出
MySQLNonTransientConnectionException以防验证失败。
请提出任何有价值的建议。
答案 0 :(得分:0)
是的,问题是当架构验证失败时,dataSource没有关闭