2

EBean is a stateless ORM. http://ebean-orm.github.io/

EBean provides a Spring Boot artifact. http://ebean-orm.github.io/docs/setup/spring#spring-boot https://github.com/ebean-orm/avaje-ebeanorm-spring

But in that case EBean directly reads jdbc connection details, as follows.

ebean.db.ddl.generate=true
ebean.db.ddl.run=true

datasource.db.username=sa
datasource.db.password=
datasource.db.databaseUrl=jdbc:h2:mem:tests
datasource.db.databaseDriver=org.h2.Driver

I want to leverage my existing HikariCP DataSource with Spring Boot and EBean.

How do we do that ?

Rakesh Waghela
  • 2,227
  • 2
  • 26
  • 46

2 Answers2

1

you can check working EBean + Spring Boot example here:

https://github.com/bwajtr/java-persistence-frameworks-comparison

In that repository there is SpringBoot configuration here: https://github.com/bwajtr/java-persistence-frameworks-comparison/blob/master/src/main/java/com/clevergang/dbtests/DbTestsApplication.java

In that class you can see how EBean is configured and how DataSource is wired into it -> in fact you can use anything which implements DataSource interface, including HikariCP... So use that EBean configuration from DbTestsApplication and setup HikariCP for SpringBoot (check here how to do it: How do I configure HikariCP in my Spring Boot app in my application.properties files?)

Community
  • 1
  • 1
Břetislav Wajtr
  • 338
  • 1
  • 10
0

I was doing that, can now function correctly.

1.application.properties

# hikariCP
spring.datasource.type=com.zaxxer.HikariDataSource
spring.datasource.driver-class-name=@datasource.db.databaseDriver@
spring.datasource.url=@datasource.db.databaseUrl@
spring.datasource.username=@datasource.db.username@
spring.datasource.password=@datasource.db.password@
spring.datasource.poolName=SpringBootHikariCP
spring.datasource.maximumPoolSize=60
spring.datasource.minimumIdle=3
spring.datasource.maxLifetime=2000000
spring.datasource.connectionTimeout=30000
spring.datasource.idleTimeout=30000
spring.datasource.pool-prepared-statements=true
spring.datasource.max-open-prepared-statements=250
  1. Hikari config

    @Configuration
    @Component
    class DataSourceConfig {
    
    @Value("${spring.datasource.username}")
    private String user;
    
    @Value("${spring.datasource.password}")
    private String password;
    
    @Value("${spring.datasource.url}")
    private String dataSourceUrl;
    
    @Value("${spring.datasource.poolName}")
    private String poolName;
    
    @Value("${spring.datasource.connectionTimeout}")
    private int connectionTimeout;
    
    @Value("${spring.datasource.maxLifetime}")
    private int maxLifetime;
    
    @Value("${spring.datasource.maximumPoolSize}")
    private int maximumPoolSize;
    
    @Value("${spring.datasource.minimumIdle}")
    private int minimumIdle;
    @Value("${spring.datasource.idleTimeout}")
    private int idleTimeout;
    public DataSource primaryDataSource() {
    
        HikariConfig config = new HikariConfig();
        config.setPoolName(poolName);
        config.setJdbcUrl(dataSourceUrl);
        config.setUsername(user);
        config.setPassword(password);
        config.setConnectionTimeout(connectionTimeout);
        config.setMinimumIdle(minimumIdle);
        config.setIdleTimeout(idleTimeout);
        config.setMaximumPoolSize(maximumPoolSize);
        config.setMaxLifetime(idleTimeout);
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
    
        HikariDataSource ds = new HikariDataSource(config);
        return ds;
    }
    

    }

  2. ebean

     @Component
     public class EbeanFactoryBean implements FactoryBean<EbeanServer>, EnvironmentAware {
    
    @Autowired
    CurrentUserProv currentUser;
    
    
    @Autowired
    DataSourceConfig dataSourceConfig;
    
    
    /**
     * Properties used to configure EbeanServer instance
     * (loaded from spring boot application properties).
     */
    Properties properties = new Properties();
    
    public EbeanFactoryBean() {
    }
    
    @Override
    public EbeanServer getObject() throws Exception {
    
        ServerConfig config = new ServerConfig();
        config.setName("db");
        config.setCurrentUserProvider(currentUser);
        config.setDataSource(dataSourceConfig.primaryDataSource());
        config.setDefaultServer(true);
        config.setRegister(true);
        return EbeanServerFactory.create(config);
    }
    
    @Override
    public Class<?> getObjectType() {
        return EbeanServer.class;
    }
    
    @Override
    public boolean isSingleton() {
        return true;
    }
    
    @Override
    public void setEnvironment(Environment environment) {
    
        loadProperties((AbstractEnvironment) environment);
    }
    
    
    /**
     * Load into Properties (from Spring PropertySource implementations).
     */
    private void loadProperties(AbstractEnvironment environment) {
    
        MutablePropertySources propertySources = environment.getPropertySources();
    
        // reverse the order of the property sources
        List<MapPropertySource> props = new ArrayList<>();
        for (PropertySource propertySource : propertySources) {
            if (propertySource instanceof MapPropertySource) {
                props.add(0, (MapPropertySource) propertySource);
            }
        }
        // merge them into the single Properties
        for (MapPropertySource propertySource : props) {
            properties.putAll(propertySource.getSource());
        }
    }
    

    }

  • Could you be a bit more descriptive on that one please? Why did you chose these actions and such – therufa Jan 08 '17 at 14:48