Spring 4, Hibernate 5 startup issues

684 views
Skip to first unread message

Deno Vichas

unread,
Apr 23, 2017, 9:49:54 PM4/23/17
to HikariCP
All,

I'm trying to switch to Hikari from c3p0 in a fairly good size spring app.   I'm using JPA audits, envers, spring security and sessions.  When my app starts up spring seems to be picking up that Hikari are in the mix and tries to load another HikariConfig with no properties.  Not having any props it fails the validation call.    Watching stuff in the debugger I see my datasource and session manager created before the 2nd config creation.

I followed the config in the wiki along with setting hibernate.connection.provider_class to com.zaxxer.hikari.hibernate.HikariConnectionProvider

Anyone have any suggestions to where or what to look for?  


Thanks,

Brett Wooldridge

unread,
Apr 24, 2017, 6:28:57 AM4/24/17
to HikariCP
Start with using the HikariCPConnectionProvider class provided by Hibernate, instead of the old one provided by HikariCP.

Next, have you determined why two pools are getting constructed?

Deno Vichas

unread,
Apr 24, 2017, 4:42:07 PM4/24/17
to HikariCP
thanks for the pointer.  this 2nd pool is what's baffling me at the moment.  not sure if this is related but I have my dataSource defined at a bean and have have a 2nd one I use for spring security - thinking i may want to split the db in two and keep acl stuff somewhere else beside the db with my entities.  both dataSources have the same settings.

Deno Vichas

unread,
Apr 24, 2017, 4:49:47 PM4/24/17
to HikariCP
and my dataSources are defined as beans.

Deno Vichas

unread,
Apr 24, 2017, 5:58:05 PM4/24/17
to HikariCP
ok.... did some clean up and some more debugging.  


it looks like something is calling HikariCPConnectionProvider.configure

l

Deno Vichas

unread,
Apr 24, 2017, 6:16:36 PM4/24/17
to HikariCP
If I comment out 

properties.put(AvailableSettings.CONNECTION_PROVIDER, "org.hibernate.hikaricp.internal.HikariCPConnectionProvider");

I do not get this unwanted dataSource but I get the following error.  yes, the table is there..

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [author]


On Monday, April 24, 2017 at 2:58:05 PM UTC-7, Deno Vichas wrote:
ok.... did some clean up and some more debugging.  


it looks like something is calling HikariCPConnectionProvider.configure

Deno Vichas

unread,
Apr 24, 2017, 7:17:26 PM4/24/17
to HikariCP
i fixed the missing table issue. i fell victim of using both DataSourceClassName and jdbcUrl.  switching DataSourceClassName to DriverClassName fix it.

the following seems to be working if i comment out HikariCPConnectionProvider prop.  BUT... my app now seems to take forever to startup.   probably not a Hikari issue and rather upgrading from hibername 4.3 to 5.2 issue.


 
package org.magic.config;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.magic.audit.UsernameAuditorAware;
import org.magic.model.user.User;
import org.magic.repository.jpa.BaseRepositoryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Properties;

import static org.springframework.orm.jpa.vendor.Database.MYSQL;


@Configuration
@EnableJpaAuditing
@EnableJpaRepositories(basePackages = {"org.magic.repository.jpa"}, repositoryBaseClass = BaseRepositoryImpl.class)
@EnableTransactionManagement
public class JpaConfig {

    private final Logger log = LoggerFactory.getLogger(JpaConfig.class);
    private static final boolean SHOW_SQL = false;
    private static String[] packageToScan = {"org.magic.model", "org.magic.social", "org.magic.audit", "org.magic.security.model"};

    @Resource private Environment _env;



    public JpaConfig() {
        log.info("loading JPA config");
    }


    @Bean
    public AuditorAware<User> auditorProvider(){
        return new UsernameAuditorAware();
    }


    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter jpaAdapter = new HibernateJpaVendorAdapter();
        jpaAdapter.setDatabase(MYSQL);
        jpaAdapter.setShowSql(SHOW_SQL);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(jpaAdapter);
        factory.setPersistenceProvider(new HibernatePersistenceProvider());
        factory.setDataSource(dataSource());
        factory.setPackagesToScan(packageToScan);
        factory.setJpaProperties(jpaProperties());
        factory.afterPropertiesSet();

        return factory;
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }


    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }


    @Bean
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }


    private Properties jpaProperties() {
        Properties properties = new Properties();
        if(!Arrays.asList(_env.getActiveProfiles()).contains("testDb")) {
            log.info(" * Validating DDL for {} *", _env.getProperty("db.url"));
            properties.setProperty(AvailableSettings.HBM2DDL_AUTO, "validate");
        }

        properties.put(AvailableSettings.STATEMENT_BATCH_SIZE, "20");
        properties.put(AvailableSettings.IMPLICIT_NAMING_STRATEGY, "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl");
        properties.put(AvailableSettings.PHYSICAL_NAMING_STRATEGY, "org.magic.config.util.ImprovedNamingStrategy");
        properties.put(AvailableSettings.SHOW_SQL, SHOW_SQL ? "true" : "false");

        properties.put(AvailableSettings.CONNECTION_PROVIDER, "org.hibernate.hikaricp.internal.HikariCPConnectionProvider");
        properties.put(AvailableSettings.STORAGE_ENGINE ,"innodb");
        properties.put(AvailableSettings.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
        properties.put(AvailableSettings.ENABLE_LAZY_LOAD_NO_TRANS, "true");
        properties.put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "org.springframework.orm.hibernate5.SpringSessionContext");

        properties.put("org.hibernate.envers.audit_table_suffix", "_history");
        properties.put("org.hibernate.envers.audit_strategy_validity_store_revend_timestamp", "true");
        properties.put("org.hibernate.envers.audit_strategy", "org.magic.audit.MagicValidityAuditStrategy");
        properties.put("org.hibernate.envers.global_with_modified_flag", "true");  // default is false
        // default is false, if false need to use DefaultTrackingModifiedEntitiesRevisionEntity
        properties.put("org.hibernate.envers.track_entities_changed_in_revision", "true");

        return properties;
    }


    private DataSource dataSource() {
        return new HikariDataSource(hikariConfig());
    }


    private HikariConfig hikariConfig() {
        HikariConfig config = new HikariConfig();
        config.setPoolName("DB_POOL");
        config.setDriverClassName("com.mysql.jdbc.Driver");
        config.setJdbcUrl(_env.getProperty("db.url"));
        config.setUsername(_env.getProperty("db.user"));
        config.setPassword(_env.getProperty("db.pass"));
        config.setMaximumPoolSize(10);
        config.setConnectionTestQuery("SELECT 1");
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        return config;
    }

}
Reply all
Reply to author
Forward
0 new messages