title: Attempt for Two DataSources in JHipster
body: <p>I am attempting to establish two DataSources for a JHipster project, one for the application credentials (utilizing OAuth2 Authentication) and one for the actual data portion (entities). Below are my configuration files. I tried to mimic the original HikariDataSource connection JHipster comes with inheritly and I think my connection works. </p>
  
  <p>My problem arises from the creation of a new Hibernate Session and Jpa linking through the @Transactional annotation. It seems that the @Autowired variables that worked with normal JHipster do not work anymore due to something I wrote. </p>
  
  <p>I've been stuck figuring this problem out and I have come to a stand still. The error in the log states invalid column type but when I checked the schema.xml the timestamp type is correct. I'm unsure where the tinyblob check is coming from. </p>
  
  <p>Looking above I see that the beans aren't being created correctly. The entityManager I created doesn't seem to be configuring correctly. Any help will be appreciated!</p>
  
  <p>I also didn't know which files to include so I included the few below for reference. If others are needed, I will include them.</p>
  
  <p>I have also included some references I have used along my way to help aid my progress.</p>
  
  <p>UserDetailsService.java</p>
  
  <pre><code>package com.mycompany.testoauth2.security;
  
  import com.mycompany.testoauth2.domain.creds.User;
  import com.mycompany.testoauth2.repository.creds.UserRepository;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  
  import javax.inject.Inject;
  import java.util.List;
  import java.util.Locale;
  import java.util.Optional;
  import java.util.stream.Collectors;
  
  /**
   * Authenticate a user from the database.
   */
  @Component("userDetailsService")
  public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
  
      private final Logger log = LoggerFactory.getLogger(UserDetailsService.class);
  
      @Inject
      private UserRepository userRepository;
  
      @Override
      @Transactional("customEntityTransactionManager")
      public UserDetails loadUserByUsername(final String login) {
          log.debug("Authenticating {}", login);
          String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
          Optional&lt;User&gt; userFromDatabase = userRepository.findOneByLogin(lowercaseLogin);
          return userFromDatabase.map(user -&gt; {
              if (!user.getActivated()) {
                  throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated");
              }
              List&lt;GrantedAuthority&gt; grantedAuthorities = user.getAuthorities().stream()
                      .map(authority -&gt; new SimpleGrantedAuthority(authority.getName()))
                  .collect(Collectors.toList());
              return new org.springframework.security.core.userdetails.User(lowercaseLogin,
                  user.getPassword(),
                  grantedAuthorities);
          }).orElseThrow(() -&gt; new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the " +
          "database"));
      }
  }
  </code></pre>
  
  <p>SecurityConfiguration.java</p>
  
  <pre><code>package com.mycompany.testoauth2.config;
  
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpMethod;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.builders.WebSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.config.http.SessionCreationPolicy;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
  
  
  import javax.inject.Inject;
  
  @Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Inject
      private UserDetailsService userDetailsService;
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Inject
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth
              .userDetailsService(userDetailsService)
                  .passwordEncoder(passwordEncoder());
      }
  
      @Override
      public void configure(WebSecurity web) throws Exception {
          web.ignoring()
              .antMatchers(HttpMethod.OPTIONS, "/**")
              .antMatchers("/app/**/*.{js,html}")
              .antMatchers("/bower_components/**")
              .antMatchers("/i18n/**")
              .antMatchers("/content/**")
              .antMatchers("/swagger-ui/index.html")
              .antMatchers("/api/register")
              .antMatchers("/api/activate")
              .antMatchers("/api/account/reset_password/init")
              .antMatchers("/api/account/reset_password/finish")
              .antMatchers("/test/**");
      }
  
      @Override
      public void configure(HttpSecurity http) throws Exception {
          http
              .httpBasic().realmName("TestOAuth2")
              .and()
                  .sessionManagement()
                  .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
              .and()
                  .requestMatchers().antMatchers("/oauth/authorize")
              .and()
                  .authorizeRequests()
                  .antMatchers("/oauth/authorize").authenticated();
      }
  
      @Override
      @Bean
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Bean
      public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
          return new SecurityEvaluationContextExtension();
      }
  }
  </code></pre>
  
  <p>UserRepository.java</p>
  
  <pre><code>package com.mycompany.testoauth2.repository.creds;
  
  import com.mycompany.testoauth2.domain.creds.User;
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  import java.time.ZonedDateTime;
  import java.util.List;
  import java.util.Optional;
  
  /**
   * Spring Data JPA repository for the User entity.
   */
  @Repository
  public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
  
      Optional&lt;User&gt; findOneByActivationKey(String activationKey);
  
      List&lt;User&gt; findAllByActivatedIsFalseAndCreatedDateBefore(ZonedDateTime dateTime);
  
      Optional&lt;User&gt; findOneByResetKey(String resetKey);
  
      Optional&lt;User&gt; findOneByEmail(String email);
  
      Optional&lt;User&gt; findOneByLogin(String login);
  
      Optional&lt;User&gt; findOneById(Long userId);
  
      @Override
      void delete(User t);
  
  }
  </code></pre>
  
  <p>DatabaseConfiguration.java</p>
  
  <pre><code>package com.mycompany.testoauth2.config;
  
  import com.codahale.metrics.MetricRegistry;
  import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
  import com.zaxxer.hikari.HikariDataSource;
  import com.mycompany.testoauth2.config.liquibase.AsyncSpringLiquibase;
  import liquibase.integration.spring.SpringLiquibase;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
  import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.ApplicationContextException;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Primary;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  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.inject.Inject;
  import javax.sql.DataSource;
  import java.util.Arrays;
  import java.util.HashMap;
  
  @Configuration
  @PropertySource({ "classpath:persistence-multiple-db.properties" })
  @EnableJpaRepositories(
      basePackages = "com.mycompany.testoauth2.repository",
      entityManagerFactoryRef = "customEntityManager",
      transactionManagerRef = "customEntityTransactionManager")
  @EnableTransactionManagement
  @EnableJpaAuditing(auditorAwareRef = "springSecurityAuditorAware")
  public class DatabaseConfiguration {
  
      private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
      private HikariDataSource credsDb;
      private HikariDataSource dataDb;
  
      @Inject
      private Environment env;
  
      @Autowired(required = false)
      private MetricRegistry metricRegistry;
  
      @Primary
      @Bean(destroyMethod = "close")
      @ConditionalOnExpression("#{!environment.acceptsProfiles('" + Constants.SPRING_PROFILE_CLOUD + "') &amp;&amp; !environment.acceptsProfiles('" + Constants.SPRING_PROFILE_HEROKU + "')}")
      @ConfigurationProperties(prefix = "spring.datasource.hikari")
      public DataSource primaryDataSource(DataSourceProperties dataSourceProperties) {
          log.debug("Configuring Datasource");
          if (dataSourceProperties.getUrl() == null) {
              log.error("Your database connection pool configuration is incorrect! The application" +
                      " cannot start. Please check your Spring profile, current profiles are: {}",
                  Arrays.toString(env.getActiveProfiles()));
  
              throw new ApplicationContextException("Database connection pool is not configured correctly");
          }
  
          credsDb =  (HikariDataSource) DataSourceBuilder
              .create(dataSourceProperties.getClassLoader())
              .type(HikariDataSource.class)
              .driverClassName(dataSourceProperties.getDriverClassName())
              .url(dataSourceProperties.getUrl())
              .username(dataSourceProperties.getUsername())
              .password(dataSourceProperties.getPassword())
              .build();
  
          if (metricRegistry != null) {
              credsDb.setMetricRegistry(metricRegistry);
          }
          return credsDb;
      }
  
      @Bean(name = "customEntityManager")
      @Primary
      public LocalContainerEntityManagerFactoryBean customEntityManager() {
  
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(primaryDataSource(new DataSourceProperties()));
          em.setPackagesToScan("com.mycompany.testoauth2.domain.creds");
  
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          HashMap&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
          properties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
          properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
          em.setJpaPropertyMap(properties);
  
          return em;
  
      }
  
      @Bean(name = "customEntityTransactionManager")
      @Primary
      public PlatformTransactionManager customEntityTransactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(customEntityManager().getObject());
          return transactionManager;
      }
  
      @Bean
      public SpringLiquibase liquibase(DataSource dataSource, DataSourceProperties dataSourceProperties,
                                       LiquibaseProperties liquibaseProperties) {
  
          // Use liquibase.integration.spring.SpringLiquibase if you don't want Liquibase to start asynchronously
          SpringLiquibase liquibase = new AsyncSpringLiquibase();
          liquibase.setDataSource(dataSource);
          liquibase.setChangeLog("classpath:config/liquibase/master.xml");
          liquibase.setContexts(liquibaseProperties.getContexts());
          liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
          liquibase.setDropFirst(liquibaseProperties.isDropFirst());
          if (env.acceptsProfiles(Constants.SPRING_PROFILE_NO_LIQUIBASE)) {
              liquibase.setShouldRun(false);
          } else {
              liquibase.setShouldRun(liquibaseProperties.isEnabled());
              log.debug("Configuring Liquibase");
          }
  
          return liquibase;
      }
  
      @Bean
      public Hibernate4Module hibernate4Module() {
          return new Hibernate4Module();
      }
  
      public HikariDataSource getCredsDb() { return credsDb; }
      public HikariDataSource getDataDb() { return dataDb; }
  
  }
  </code></pre>
  
  <p>Error Log</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.security.core.userdetails.UserDetailsService com.mycompany.testoauth2.config.SecurityConfiguration.userDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.testoauth2.repository.creds.UserRepository com.mycompany.testoauth2.security.UserDetailsService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#8ef2c72' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#8ef2c72': Cannot resolve reference to bean 'customEntityManager' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customEntityManager' defined in class path resource [com/mycompany/testoauth2/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in app_cred.jhi_persistent_audit_event for column event_date. Found: timestamp, expected: tinyblob
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1060)
      at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
      at...
  </code></pre>
  
  <p>Multiple DataSources JHipster (Unanswered): <a href="https://stackoverflow.com/questions/36808461/jhipster-multiple-databases">@36808461</a>
  Two DataSource JHipster Reference: <a href="https://stackoverflow.com/questions/31240475/two-mysql-database-on-jhipster">@31240475</a></p>
  
  <p>Jpa Two Datasources Link: <a href="http://www.baeldung.com/spring-data-jpa-multiple-databases" rel="nofollow noreferrer">http://www.baeldung.com/spring-data-jpa-multiple-databases</a></p>